diff --git a/controllers/almanach_edit.go b/controllers/almanach_edit.go index fd95a3b..93537f3 100644 --- a/controllers/almanach_edit.go +++ b/controllers/almanach_edit.go @@ -80,8 +80,10 @@ func (p *AlmanachEditPage) GET(engine *templating.Engine, app core.App) HandleFu } type AlmanachEditResult struct { - Next *dbmodels.Entry - Prev *dbmodels.Entry + NextByID *dbmodels.Entry + PrevByID *dbmodels.Entry + NextByTitle *dbmodels.Entry + PrevByTitle *dbmodels.Entry User *dbmodels.User AlmanachResult } @@ -102,17 +104,49 @@ func NewAlmanachEditResult(app core.App, id string, filters BeitraegeFilterParam } } - next := result.Entry.Next(app) - prev := result.Entry.Prev(app) + prevByID := result.Entry.Prev(app) + nextByID := result.Entry.Next(app) + prevByTitle, nextByTitle, err := entryNeighborsByPreferredTitle(app, result.Entry.Id) + if err != nil { + app.Logger().Error("Failed to load entry neighbors", "entry", result.Entry.Id, "error", err) + } return &AlmanachEditResult{ User: user, AlmanachResult: *result, - Next: next, - Prev: prev, + NextByID: nextByID, + PrevByID: prevByID, + NextByTitle: nextByTitle, + PrevByTitle: prevByTitle, }, nil } +func entryNeighborsByPreferredTitle(app core.App, entryID string) (*dbmodels.Entry, *dbmodels.Entry, error) { + entries := []*dbmodels.Entry{} + if err := app.RecordQuery(dbmodels.ENTRIES_TABLE).All(&entries); err != nil { + return nil, nil, err + } + if len(entries) == 0 { + return nil, nil, nil + } + dbmodels.Sort_Entries_Title_Year(entries) + for index, item := range entries { + if item.Id != entryID { + continue + } + var prev *dbmodels.Entry + var next *dbmodels.Entry + if index > 0 { + prev = entries[index-1] + } + if index+1 < len(entries) { + next = entries[index+1] + } + return prev, next, nil + } + return nil, nil, nil +} + func (p *AlmanachEditPage) POSTSave(engine *templating.Engine, app core.App) HandleFunc { return func(e *core.RequestEvent) error { id := e.Request.PathValue("id") diff --git a/controllers/almanach_new.go b/controllers/almanach_new.go index 9ba5ab5..6ebdae9 100644 --- a/controllers/almanach_new.go +++ b/controllers/almanach_new.go @@ -69,8 +69,10 @@ func (p *AlmanachNewPage) GET(engine *templating.Engine, app core.App) HandleFun Types: []string{}, HasScans: false, }, - Prev: nil, - Next: nil, + PrevByID: nil, + NextByID: nil, + PrevByTitle: nil, + NextByTitle: nil, } data["result"] = result diff --git a/views/routes/almanach/edit/body.gohtml b/views/routes/almanach/edit/body.gohtml index cd0a7f2..8eb9f2c 100644 --- a/views/routes/almanach/edit/body.gohtml +++ b/views/routes/almanach/edit/body.gohtml @@ -44,9 +44,9 @@ type AlmanachResult struct { ·