mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 10:35:30 +00:00
Almanach list
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
|
||||
const (
|
||||
URL_REIHE_EDIT = "edit"
|
||||
URL_REIHE_DELETE = "edit/delete"
|
||||
TEMPLATE_REIHE_EDIT = "/reihe/edit/"
|
||||
)
|
||||
|
||||
@@ -43,12 +44,20 @@ func (p *ReiheEditPage) Setup(router *router.Router[*core.RequestEvent], app cor
|
||||
rg.BindFunc(middleware.IsAdminOrEditor())
|
||||
rg.GET(URL_REIHE_EDIT, p.GET(engine, app))
|
||||
rg.POST(URL_REIHE_EDIT, p.POST(engine, app))
|
||||
rg.POST(URL_REIHE_DELETE, p.POSTDelete(engine, app))
|
||||
return nil
|
||||
}
|
||||
|
||||
type ReiheEditResult struct {
|
||||
Series *dbmodels.Series
|
||||
User *dbmodels.User
|
||||
Prev *dbmodels.Series
|
||||
Next *dbmodels.Series
|
||||
Entries []*dbmodels.Entry
|
||||
Contents []*dbmodels.Content
|
||||
ContentEntries map[string]*dbmodels.Entry
|
||||
ContentTypes map[string][]string
|
||||
PreferredEntries []*dbmodels.Entry
|
||||
}
|
||||
|
||||
func NewReiheEditResult(app core.App, id string) (*ReiheEditResult, error) {
|
||||
@@ -67,9 +76,45 @@ func NewReiheEditResult(app core.App, id string) (*ReiheEditResult, error) {
|
||||
}
|
||||
}
|
||||
|
||||
prev, next, err := seriesNeighbors(app, series.Id)
|
||||
if err != nil {
|
||||
app.Logger().Error("Failed to load series neighbors", "series", series.Id, "error", err)
|
||||
}
|
||||
|
||||
entries, _, err := Entries_Series_IDs(app, []any{series.Id})
|
||||
if err != nil {
|
||||
app.Logger().Error("Failed to load series entries", "series", series.Id, "error", err)
|
||||
}
|
||||
if len(entries) > 0 {
|
||||
dbmodels.Sort_Entries_Year_Title(entries)
|
||||
}
|
||||
|
||||
contents, contentEntries, contentTypes, err := seriesContentsDetails(app, entries)
|
||||
if err != nil {
|
||||
app.Logger().Error("Failed to load series contents", "series", series.Id, "error", err)
|
||||
}
|
||||
if len(contents) > 0 {
|
||||
dbmodels.Sort_Contents_Numbering(contents)
|
||||
}
|
||||
|
||||
preferredEntries, err := preferredSeriesEntries(app, series.Id)
|
||||
if err != nil {
|
||||
app.Logger().Error("Failed to load preferred series entries", "series", series.Id, "error", err)
|
||||
}
|
||||
if len(preferredEntries) > 0 {
|
||||
dbmodels.Sort_Entries_Year_Title(preferredEntries)
|
||||
}
|
||||
|
||||
return &ReiheEditResult{
|
||||
Series: series,
|
||||
User: user,
|
||||
Series: series,
|
||||
User: user,
|
||||
Prev: prev,
|
||||
Next: next,
|
||||
Entries: entries,
|
||||
Contents: contents,
|
||||
ContentEntries: contentEntries,
|
||||
ContentTypes: contentTypes,
|
||||
PreferredEntries: preferredEntries,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -110,6 +155,183 @@ func (p *ReiheEditPage) renderError(engine *templating.Engine, app core.App, e *
|
||||
return engine.Response200(e, p.Template, data, p.Layout)
|
||||
}
|
||||
|
||||
type reiheDeletePayload struct {
|
||||
CSRFToken string `json:"csrf_token"`
|
||||
LastEdited string `json:"last_edited"`
|
||||
}
|
||||
|
||||
func (p *ReiheEditPage) POSTDelete(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
id := e.Request.PathValue("id")
|
||||
req := templating.NewRequest(e)
|
||||
|
||||
payload := reiheDeletePayload{}
|
||||
if err := e.BindBody(&payload); err != nil {
|
||||
return e.JSON(http.StatusBadRequest, map[string]any{
|
||||
"error": "Ungültige Formulardaten.",
|
||||
})
|
||||
}
|
||||
|
||||
if err := req.CheckCSRF(payload.CSRFToken); err != nil {
|
||||
return e.JSON(http.StatusBadRequest, map[string]any{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
series, err := dbmodels.Series_MusenalmID(app, id)
|
||||
if err != nil {
|
||||
return e.JSON(http.StatusNotFound, map[string]any{
|
||||
"error": "Reihe wurde nicht gefunden.",
|
||||
})
|
||||
}
|
||||
|
||||
if payload.LastEdited != "" {
|
||||
lastEdited, err := types.ParseDateTime(payload.LastEdited)
|
||||
if err != nil {
|
||||
return e.JSON(http.StatusBadRequest, map[string]any{
|
||||
"error": "Ungültiger Bearbeitungszeitstempel.",
|
||||
})
|
||||
}
|
||||
if !series.Updated().Time().Equal(lastEdited.Time()) {
|
||||
return e.JSON(http.StatusConflict, map[string]any{
|
||||
"error": "Die Reihe wurde inzwischen geändert. Bitte Seite neu laden.",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
preferredEntries, err := preferredSeriesEntries(app, series.Id)
|
||||
if err != nil {
|
||||
return e.JSON(http.StatusInternalServerError, map[string]any{
|
||||
"error": "Löschen fehlgeschlagen.",
|
||||
})
|
||||
}
|
||||
|
||||
if err := app.RunInTransaction(func(tx core.App) error {
|
||||
for _, entry := range preferredEntries {
|
||||
if err := deleteEntryRelations(tx, entry.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := deleteEntryItems(tx, entry.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := deleteEntryContents(tx, entry.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
record, err := tx.FindRecordById(dbmodels.ENTRIES_TABLE, entry.Id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if err := tx.Delete(record); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
relations, err := dbmodels.REntriesSeries_Seriess(tx, []any{series.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
relationsTable := dbmodels.RelationTableName(dbmodels.ENTRIES_TABLE, dbmodels.SERIES_TABLE)
|
||||
for _, relation := range relations {
|
||||
record, err := tx.FindRecordById(relationsTable, relation.Id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if err := tx.Delete(record); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
record, err := tx.FindRecordById(dbmodels.SERIES_TABLE, series.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(record)
|
||||
}); err != nil {
|
||||
app.Logger().Error("Failed to delete series", "series_id", series.Id, "error", err)
|
||||
return e.JSON(http.StatusInternalServerError, map[string]any{
|
||||
"error": "Löschen fehlgeschlagen.",
|
||||
})
|
||||
}
|
||||
|
||||
return e.JSON(http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"redirect": "/reihen",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func seriesNeighbors(app core.App, currentID string) (*dbmodels.Series, *dbmodels.Series, error) {
|
||||
series := []*dbmodels.Series{}
|
||||
if err := app.RecordQuery(dbmodels.SERIES_TABLE).All(&series); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(series) == 0 {
|
||||
return nil, nil, nil
|
||||
}
|
||||
dbmodels.Sort_Series_Title(series)
|
||||
for index, item := range series {
|
||||
if item.Id != currentID {
|
||||
continue
|
||||
}
|
||||
var prev *dbmodels.Series
|
||||
var next *dbmodels.Series
|
||||
if index > 0 {
|
||||
prev = series[index-1]
|
||||
}
|
||||
if index+1 < len(series) {
|
||||
next = series[index+1]
|
||||
}
|
||||
return prev, next, nil
|
||||
}
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
func seriesContentsDetails(app core.App, entries []*dbmodels.Entry) ([]*dbmodels.Content, map[string]*dbmodels.Entry, map[string][]string, error) {
|
||||
if len(entries) == 0 {
|
||||
return []*dbmodels.Content{}, map[string]*dbmodels.Entry{}, map[string][]string{}, nil
|
||||
}
|
||||
entryMap := make(map[string]*dbmodels.Entry, len(entries))
|
||||
for _, entry := range entries {
|
||||
entryMap[entry.Id] = entry
|
||||
}
|
||||
|
||||
contents := []*dbmodels.Content{}
|
||||
typeMap := make(map[string][]string)
|
||||
for _, entry := range entries {
|
||||
entryContents, err := dbmodels.Contents_Entry(app, entry.Id)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
for _, content := range entryContents {
|
||||
contents = append(contents, content)
|
||||
typeMap[content.Id] = append(typeMap[content.Id], content.MusenalmType()...)
|
||||
}
|
||||
}
|
||||
|
||||
return contents, entryMap, typeMap, nil
|
||||
}
|
||||
|
||||
func preferredSeriesEntries(app core.App, seriesID string) ([]*dbmodels.Entry, error) {
|
||||
relations, err := dbmodels.REntriesSeries_Seriess(app, []any{seriesID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(relations) == 0 {
|
||||
return []*dbmodels.Entry{}, nil
|
||||
}
|
||||
entryIDs := []any{}
|
||||
for _, relation := range relations {
|
||||
if strings.TrimSpace(relation.Type()) != preferredSeriesRelationType {
|
||||
continue
|
||||
}
|
||||
entryIDs = append(entryIDs, relation.Entry())
|
||||
}
|
||||
if len(entryIDs) == 0 {
|
||||
return []*dbmodels.Entry{}, nil
|
||||
}
|
||||
return dbmodels.Entries_IDs(app, entryIDs)
|
||||
}
|
||||
|
||||
type reiheEditForm struct {
|
||||
CSRFToken string `form:"csrf_token"`
|
||||
LastEdited string `form:"last_edited"`
|
||||
|
||||
Reference in New Issue
Block a user