Skip to content

Commit

Permalink
squashed
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Jan 9, 2025
1 parent a2b0ab2 commit 84da3d4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
55 changes: 37 additions & 18 deletions cmd/goatak_server/database/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,27 @@ func (mm *DatabaseManager) Create(s any) error {
return nil
}

return mm.db.Create(s).Error
err := mm.db.Create(s).Error

if err != nil {
mm.logger.Error("error create object", slog.Any("error", err))
}

return err
}

func (mm *DatabaseManager) Save(s any) error {
if mm == nil || mm.db == nil {
return nil
}

return mm.db.Save(s).Error
err := mm.db.Save(s).Error

if err != nil {
mm.logger.Error("error saving object", slog.Any("error", err))
}

return err
}

func (mm *DatabaseManager) MissionQuery() *MissionQuery {
Expand Down Expand Up @@ -109,35 +121,42 @@ func (mm *DatabaseManager) AddMissionPoint(mission *model.Mission, msg *cot.CotM

now := time.Now()

var point *model.MissionPoint
for _, p := range mission.Points {
if p.UID == msg.GetUID() {
mm.logger.Info("update existing point " + p.UID)
mm.logger.Info("update existing point " + p.Callsign + " " + p.UID)
p.UpdateFromMsg(msg)
mm.db.Save(p)

return nil
if err := mm.Save(p); err != nil {
return nil
}
point = p
break
}
}

mm.logger.Info("add new " + msg.GetUID())
i := &model.MissionPoint{
UID: msg.GetUID(),
}
if point == nil {
mm.logger.Info("add new point " + msg.GetCallsign() + " " + msg.GetUID())
point = &model.MissionPoint{
UID: msg.GetUID(),
}

i.UpdateFromMsg(msg)
point.UpdateFromMsg(msg)

mission.Points = append(mission.Points, i)
mission.UpdatedAt = now
mission.Points = append(mission.Points, point)
mission.UpdatedAt = now

mm.db.Save(mission)
if err := mm.Save(mission); err != nil {
return nil
}
}

// todo: use sender uid, not parent
p, _ := msg.GetParent()
parent, _ := msg.GetParent()

c := &model.Change{
Type: "ADD_CONTENT",
MissionID: mission.ID,
CreatorUID: p,
CreatorUID: parent,
ContentUID: msg.GetUID(),
CotType: msg.GetType(),
Callsign: msg.GetCallsign(),
Expand All @@ -147,7 +166,7 @@ func (mm *DatabaseManager) AddMissionPoint(mission *model.Mission, msg *cot.CotM
Lon: msg.GetLon(),
}

mm.db.Create(c)
_ = mm.Create(c)

return c
}
Expand Down Expand Up @@ -184,7 +203,7 @@ func (mm *DatabaseManager) DeleteMissionPoint(missionId uint, uid string, author
Lon: mp.Lon,
}

mm.db.Create(c)
_ = mm.Create(c)

return c
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (app *App) route(msg *cot.CotMessage) bool {
}

func (app *App) processMissionPoint(missionName string, msg *cot.CotMessage) {
m := app.dbm.MissionQuery().Scope(msg.Scope).Name(missionName).One()
m := app.dbm.MissionQuery().Scope(msg.Scope).Name(missionName).Full().One()

if m == nil {
return
Expand Down
31 changes: 31 additions & 0 deletions cmd/goatak_server/mission_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/kdudkov/goatak/internal/model"
"github.com/kdudkov/goatak/pkg/cot"
"github.com/kdudkov/goatak/pkg/tools"
)

const (
Expand Down Expand Up @@ -300,6 +301,36 @@ func getMissionChangesHandler(app *App) fiber.Handler {

ch := app.dbm.GetChanges(mission.ID, d1)

if ctx.QueryBool("squashed") {
uids := tools.NewStringSet()

n := 0

for i := range ch {
idx := len(ch) - i - 1
if uids.Has(ch[idx].ContentUID) {
ch[idx] = nil
continue
}
uids.Add(ch[idx].ContentUID)

if ch[idx].Type == "REMOVE_CONTENT" {
ch[idx] = nil
}
n++
}

ch1 := make([]*model.Change, 0, n)

for _, c := range ch {
if c != nil {
ch1 = append(ch, c)
}
}

ch = ch1
}

result := make([]*model.MissionChangeDTO, len(ch))

for i, c := range ch {
Expand Down

0 comments on commit 84da3d4

Please sign in to comment.