mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 02:25:30 +00:00
Basic new
This commit is contained in:
172
controllers/almanach_new.go
Normal file
172
controllers/almanach_new.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/middleware"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
const (
|
||||
URL_ALMANACH_NEW = "/almanach-new/"
|
||||
URL_ALMANACH_NEW_SAVE = "save"
|
||||
)
|
||||
|
||||
func init() {
|
||||
anp := &AlmanachNewPage{
|
||||
StaticPage: pagemodels.StaticPage{
|
||||
Name: pagemodels.P_ALMANACH_NEW_NAME,
|
||||
URL: URL_ALMANACH_NEW,
|
||||
Template: TEMPLATE_ALMANACH_EDIT,
|
||||
Layout: pagemodels.LAYOUT_LOGIN_PAGES,
|
||||
},
|
||||
}
|
||||
app.Register(anp)
|
||||
}
|
||||
|
||||
type AlmanachNewPage struct {
|
||||
pagemodels.StaticPage
|
||||
}
|
||||
|
||||
func (p *AlmanachNewPage) Setup(router *router.Router[*core.RequestEvent], app core.App, engine *templating.Engine) error {
|
||||
rg := router.Group(URL_ALMANACH_NEW)
|
||||
rg.BindFunc(middleware.IsAdminOrEditor())
|
||||
rg.GET("", p.GET(engine, app))
|
||||
rg.POST(URL_ALMANACH_NEW_SAVE, p.POSTSave(engine, app))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *AlmanachNewPage) GET(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
req := templating.NewRequest(e)
|
||||
data := make(map[string]any)
|
||||
|
||||
entry, err := newEmptyEntry(app)
|
||||
if err != nil {
|
||||
return engine.Response500(e, err, data)
|
||||
}
|
||||
|
||||
filters := NewBeitraegeFilterParameters(e)
|
||||
result := &AlmanachEditResult{
|
||||
User: nil,
|
||||
AlmanachResult: AlmanachResult{
|
||||
Entry: entry,
|
||||
Places: []*dbmodels.Place{},
|
||||
Series: []*dbmodels.Series{},
|
||||
Contents: []*dbmodels.Content{},
|
||||
Items: []*dbmodels.Item{},
|
||||
Agents: map[string]*dbmodels.Agent{},
|
||||
EntriesSeries: map[string]*dbmodels.REntriesSeries{},
|
||||
EntriesAgents: []*dbmodels.REntriesAgents{},
|
||||
ContentsAgents: map[string][]*dbmodels.RContentsAgents{},
|
||||
Types: []string{},
|
||||
HasScans: false,
|
||||
},
|
||||
Prev: nil,
|
||||
Next: nil,
|
||||
}
|
||||
|
||||
data["result"] = result
|
||||
data["filters"] = filters
|
||||
data["csrf_token"] = req.Session().Token
|
||||
data["item_types"] = dbmodels.ITEM_TYPE_VALUES
|
||||
data["agent_relations"] = dbmodels.AGENT_RELATIONS
|
||||
data["series_relations"] = dbmodels.SERIES_RELATIONS
|
||||
data["is_new"] = true
|
||||
|
||||
abbrs, err := pagemodels.GetAbks(app)
|
||||
if err == nil {
|
||||
data["abbrs"] = abbrs
|
||||
}
|
||||
|
||||
return engine.Response200(e, p.Template, data, p.Layout)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *AlmanachNewPage) POSTSave(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
req := templating.NewRequest(e)
|
||||
|
||||
payload := almanachEditPayload{}
|
||||
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(),
|
||||
})
|
||||
}
|
||||
|
||||
if err := payload.Validate(); err != nil {
|
||||
return e.JSON(http.StatusBadRequest, map[string]any{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
var entry *dbmodels.Entry
|
||||
user := req.User()
|
||||
if err := app.RunInTransaction(func(tx core.App) error {
|
||||
collection, err := tx.FindCollectionByNameOrId(dbmodels.ENTRIES_TABLE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newEntry := dbmodels.NewEntry(core.NewRecord(collection))
|
||||
nextID, err := nextEntryMusenalmID(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newEntry.SetMusenalmID(nextID)
|
||||
|
||||
if err := applyEntryChanges(tx, newEntry, &payload, user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := applyItemsChanges(tx, newEntry, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := applySeriesRelations(tx, newEntry, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := applyAgentRelations(tx, newEntry, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
entry = newEntry
|
||||
return nil
|
||||
}); err != nil {
|
||||
app.Logger().Error("Failed to create almanach entry", "error", err)
|
||||
return e.JSON(http.StatusInternalServerError, map[string]any{
|
||||
"error": "Speichern fehlgeschlagen.",
|
||||
})
|
||||
}
|
||||
|
||||
redirect := "/"
|
||||
if entry != nil {
|
||||
redirect = "/almanach/" + strconv.Itoa(entry.MusenalmID()) + "/edit?saved_message=" + url.QueryEscape("Änderungen gespeichert.")
|
||||
}
|
||||
|
||||
return e.JSON(http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"redirect": redirect,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newEmptyEntry(app core.App) (*dbmodels.Entry, error) {
|
||||
collection, err := app.FindCollectionByNameOrId(dbmodels.ENTRIES_TABLE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entry := dbmodels.NewEntry(core.NewRecord(collection))
|
||||
entry.SetEditState("Unknown")
|
||||
return entry, nil
|
||||
}
|
||||
54
controllers/musenalm_id.go
Normal file
54
controllers/musenalm_id.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
func nextEntryMusenalmID(app core.App) (int, error) {
|
||||
var entry dbmodels.Entry
|
||||
err := app.RecordQuery(dbmodels.ENTRIES_TABLE).
|
||||
OrderBy(dbmodels.MUSENALMID_FIELD + " DESC").
|
||||
Limit(1).
|
||||
One(&entry)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return entry.MusenalmID() + 1, nil
|
||||
}
|
||||
|
||||
func nextSeriesMusenalmID(app core.App) (int, error) {
|
||||
var series dbmodels.Series
|
||||
err := app.RecordQuery(dbmodels.SERIES_TABLE).
|
||||
OrderBy(dbmodels.MUSENALMID_FIELD + " DESC").
|
||||
Limit(1).
|
||||
One(&series)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return series.MusenalmID() + 1, nil
|
||||
}
|
||||
|
||||
func nextAgentMusenalmID(app core.App) (int, error) {
|
||||
var agent dbmodels.Agent
|
||||
err := app.RecordQuery(dbmodels.AGENTS_TABLE).
|
||||
OrderBy(dbmodels.MUSENALMID_FIELD + " DESC").
|
||||
Limit(1).
|
||||
One(&agent)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return agent.MusenalmID() + 1, nil
|
||||
}
|
||||
@@ -247,6 +247,23 @@ type personEditForm struct {
|
||||
Comment string `form:"edit_comment"`
|
||||
}
|
||||
|
||||
func applyPersonForm(agent *dbmodels.Agent, formdata personEditForm, name string, status string, user *dbmodels.FixedUser) {
|
||||
agent.SetName(name)
|
||||
agent.SetPseudonyms(strings.TrimSpace(formdata.Pseudonyms))
|
||||
agent.SetBiographicalData(strings.TrimSpace(formdata.BiographicalData))
|
||||
agent.SetProfession(strings.TrimSpace(formdata.Profession))
|
||||
agent.SetReferences(strings.TrimSpace(formdata.References))
|
||||
agent.SetAnnotation(strings.TrimSpace(formdata.Annotation))
|
||||
agent.SetURI(strings.TrimSpace(formdata.URI))
|
||||
agent.SetCorporateBody(formdata.CorporateBody)
|
||||
agent.SetFictional(formdata.Fictional)
|
||||
agent.SetEditState(status)
|
||||
agent.SetComment(strings.TrimSpace(formdata.Comment))
|
||||
if user != nil {
|
||||
agent.SetEditor(user.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PersonEditPage) POST(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
id := e.Request.PathValue("id")
|
||||
@@ -288,20 +305,7 @@ func (p *PersonEditPage) POST(engine *templating.Engine, app core.App) HandleFun
|
||||
|
||||
user := req.User()
|
||||
if err := app.RunInTransaction(func(tx core.App) error {
|
||||
agent.SetName(name)
|
||||
agent.SetPseudonyms(strings.TrimSpace(formdata.Pseudonyms))
|
||||
agent.SetBiographicalData(strings.TrimSpace(formdata.BiographicalData))
|
||||
agent.SetProfession(strings.TrimSpace(formdata.Profession))
|
||||
agent.SetReferences(strings.TrimSpace(formdata.References))
|
||||
agent.SetAnnotation(strings.TrimSpace(formdata.Annotation))
|
||||
agent.SetURI(strings.TrimSpace(formdata.URI))
|
||||
agent.SetCorporateBody(formdata.CorporateBody)
|
||||
agent.SetFictional(formdata.Fictional)
|
||||
agent.SetEditState(status)
|
||||
agent.SetComment(strings.TrimSpace(formdata.Comment))
|
||||
if user != nil {
|
||||
agent.SetEditor(user.Id)
|
||||
}
|
||||
applyPersonForm(agent, formdata, name, status, user)
|
||||
return tx.Save(agent)
|
||||
}); err != nil {
|
||||
app.Logger().Error("Failed to save agent", "agent_id", agent.Id, "error", err)
|
||||
|
||||
144
controllers/person_new.go
Normal file
144
controllers/person_new.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/middleware"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
const (
|
||||
URL_PERSONEN_NEW = "/personen/new/"
|
||||
)
|
||||
|
||||
func init() {
|
||||
pnp := &PersonNewPage{
|
||||
StaticPage: pagemodels.StaticPage{
|
||||
Name: pagemodels.P_PERSON_NEW_NAME,
|
||||
URL: URL_PERSONEN_NEW,
|
||||
Template: TEMPLATE_PERSON_EDIT,
|
||||
Layout: pagemodels.LAYOUT_LOGIN_PAGES,
|
||||
},
|
||||
}
|
||||
app.Register(pnp)
|
||||
}
|
||||
|
||||
type PersonNewPage struct {
|
||||
pagemodels.StaticPage
|
||||
}
|
||||
|
||||
func (p *PersonNewPage) Setup(router *router.Router[*core.RequestEvent], app core.App, engine *templating.Engine) error {
|
||||
rg := router.Group(URL_PERSONEN_NEW)
|
||||
rg.BindFunc(middleware.IsAdminOrEditor())
|
||||
rg.GET("", p.GET(engine, app))
|
||||
rg.POST("", p.POST(engine, app))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PersonNewPage) GET(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
req := templating.NewRequest(e)
|
||||
return p.renderPage(engine, app, e, req, "")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PersonNewPage) renderPage(engine *templating.Engine, app core.App, e *core.RequestEvent, req *templating.Request, message string) error {
|
||||
data := make(map[string]any)
|
||||
|
||||
collection, err := app.FindCollectionByNameOrId(dbmodels.AGENTS_TABLE)
|
||||
if err != nil {
|
||||
return engine.Response500(e, err, data)
|
||||
}
|
||||
agent := dbmodels.NewAgent(core.NewRecord(collection))
|
||||
agent.SetEditState("Unknown")
|
||||
|
||||
result := &PersonEditResult{
|
||||
Agent: agent,
|
||||
User: nil,
|
||||
Prev: nil,
|
||||
Next: nil,
|
||||
Entries: []*dbmodels.Entry{},
|
||||
EntryTypes: map[string][]string{},
|
||||
Contents: []*dbmodels.Content{},
|
||||
ContentEntries: map[string]*dbmodels.Entry{},
|
||||
ContentTypes: map[string][]string{},
|
||||
}
|
||||
|
||||
data["result"] = result
|
||||
data["csrf_token"] = req.Session().Token
|
||||
data["is_new"] = true
|
||||
if message != "" {
|
||||
data["error"] = message
|
||||
}
|
||||
|
||||
return engine.Response200(e, p.Template, data, p.Layout)
|
||||
}
|
||||
|
||||
func (p *PersonNewPage) POST(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
req := templating.NewRequest(e)
|
||||
|
||||
formdata := personEditForm{}
|
||||
if err := e.BindBody(&formdata); err != nil {
|
||||
return p.renderPage(engine, app, e, req, "Formulardaten ungültig.")
|
||||
}
|
||||
|
||||
if err := req.CheckCSRF(formdata.CSRFToken); err != nil {
|
||||
return p.renderPage(engine, app, e, req, err.Error())
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(formdata.Name)
|
||||
if name == "" {
|
||||
return p.renderPage(engine, app, e, req, "Name ist erforderlich.")
|
||||
}
|
||||
|
||||
status := strings.TrimSpace(formdata.Status)
|
||||
if status == "" || !slices.Contains(dbmodels.EDITORSTATE_VALUES, status) {
|
||||
return p.renderPage(engine, app, e, req, "Ungültiger Status.")
|
||||
}
|
||||
|
||||
var createdAgent *dbmodels.Agent
|
||||
user := req.User()
|
||||
if err := app.RunInTransaction(func(tx core.App) error {
|
||||
collection, err := tx.FindCollectionByNameOrId(dbmodels.AGENTS_TABLE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
agent := dbmodels.NewAgent(core.NewRecord(collection))
|
||||
nextID, err := nextAgentMusenalmID(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
agent.SetMusenalmID(nextID)
|
||||
applyPersonForm(agent, formdata, name, status, user)
|
||||
if err := tx.Save(agent); err != nil {
|
||||
return err
|
||||
}
|
||||
createdAgent = agent
|
||||
return nil
|
||||
}); err != nil {
|
||||
app.Logger().Error("Failed to create agent", "error", err)
|
||||
return p.renderPage(engine, app, e, req, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
if createdAgent == nil {
|
||||
return p.renderPage(engine, app, e, req, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
redirect := fmt.Sprintf(
|
||||
"/person/%s/edit?saved_message=%s",
|
||||
createdAgent.Id,
|
||||
url.QueryEscape("Änderungen gespeichert."),
|
||||
)
|
||||
return e.Redirect(http.StatusSeeOther, redirect)
|
||||
}
|
||||
}
|
||||
@@ -344,6 +344,19 @@ type reiheEditForm struct {
|
||||
Comment string `form:"edit_comment"`
|
||||
}
|
||||
|
||||
func applySeriesForm(series *dbmodels.Series, formdata reiheEditForm, title string, status string, user *dbmodels.FixedUser) {
|
||||
series.SetTitle(title)
|
||||
series.SetPseudonyms(strings.TrimSpace(formdata.Pseudonyms))
|
||||
series.SetAnnotation(strings.TrimSpace(formdata.Annotation))
|
||||
series.SetReferences(strings.TrimSpace(formdata.References))
|
||||
series.SetFrequency(strings.TrimSpace(formdata.Frequency))
|
||||
series.SetEditState(status)
|
||||
series.SetComment(strings.TrimSpace(formdata.Comment))
|
||||
if user != nil {
|
||||
series.SetEditor(user.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ReiheEditPage) POST(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
id := e.Request.PathValue("id")
|
||||
@@ -385,16 +398,7 @@ func (p *ReiheEditPage) POST(engine *templating.Engine, app core.App) HandleFunc
|
||||
|
||||
user := req.User()
|
||||
if err := app.RunInTransaction(func(tx core.App) error {
|
||||
series.SetTitle(title)
|
||||
series.SetPseudonyms(strings.TrimSpace(formdata.Pseudonyms))
|
||||
series.SetAnnotation(strings.TrimSpace(formdata.Annotation))
|
||||
series.SetReferences(strings.TrimSpace(formdata.References))
|
||||
series.SetFrequency(strings.TrimSpace(formdata.Frequency))
|
||||
series.SetEditState(status)
|
||||
series.SetComment(strings.TrimSpace(formdata.Comment))
|
||||
if user != nil {
|
||||
series.SetEditor(user.Id)
|
||||
}
|
||||
applySeriesForm(series, formdata, title, status, user)
|
||||
return tx.Save(series)
|
||||
}); err != nil {
|
||||
app.Logger().Error("Failed to save series", "series_id", series.Id, "error", err)
|
||||
|
||||
144
controllers/reihe_new.go
Normal file
144
controllers/reihe_new.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/middleware"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
const (
|
||||
URL_REIHEN_NEW = "/reihen/new/"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rnp := &ReiheNewPage{
|
||||
StaticPage: pagemodels.StaticPage{
|
||||
Name: pagemodels.P_REIHE_NEW_NAME,
|
||||
URL: URL_REIHEN_NEW,
|
||||
Template: TEMPLATE_REIHE_EDIT,
|
||||
Layout: pagemodels.LAYOUT_LOGIN_PAGES,
|
||||
},
|
||||
}
|
||||
app.Register(rnp)
|
||||
}
|
||||
|
||||
type ReiheNewPage struct {
|
||||
pagemodels.StaticPage
|
||||
}
|
||||
|
||||
func (p *ReiheNewPage) Setup(router *router.Router[*core.RequestEvent], app core.App, engine *templating.Engine) error {
|
||||
rg := router.Group(URL_REIHEN_NEW)
|
||||
rg.BindFunc(middleware.IsAdminOrEditor())
|
||||
rg.GET("", p.GET(engine, app))
|
||||
rg.POST("", p.POST(engine, app))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ReiheNewPage) GET(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
req := templating.NewRequest(e)
|
||||
return p.renderPage(engine, app, e, req, "")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ReiheNewPage) renderPage(engine *templating.Engine, app core.App, e *core.RequestEvent, req *templating.Request, message string) error {
|
||||
data := make(map[string]any)
|
||||
|
||||
collection, err := app.FindCollectionByNameOrId(dbmodels.SERIES_TABLE)
|
||||
if err != nil {
|
||||
return engine.Response500(e, err, data)
|
||||
}
|
||||
series := dbmodels.NewSeries(core.NewRecord(collection))
|
||||
series.SetEditState("Unknown")
|
||||
|
||||
result := &ReiheEditResult{
|
||||
Series: series,
|
||||
User: nil,
|
||||
Prev: nil,
|
||||
Next: nil,
|
||||
Entries: []*dbmodels.Entry{},
|
||||
Contents: []*dbmodels.Content{},
|
||||
ContentEntries: map[string]*dbmodels.Entry{},
|
||||
ContentTypes: map[string][]string{},
|
||||
PreferredEntries: []*dbmodels.Entry{},
|
||||
}
|
||||
|
||||
data["result"] = result
|
||||
data["csrf_token"] = req.Session().Token
|
||||
data["is_new"] = true
|
||||
if message != "" {
|
||||
data["error"] = message
|
||||
}
|
||||
|
||||
return engine.Response200(e, p.Template, data, p.Layout)
|
||||
}
|
||||
|
||||
func (p *ReiheNewPage) POST(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
req := templating.NewRequest(e)
|
||||
|
||||
formdata := reiheEditForm{}
|
||||
if err := e.BindBody(&formdata); err != nil {
|
||||
return p.renderPage(engine, app, e, req, "Formulardaten ungültig.")
|
||||
}
|
||||
|
||||
if err := req.CheckCSRF(formdata.CSRFToken); err != nil {
|
||||
return p.renderPage(engine, app, e, req, err.Error())
|
||||
}
|
||||
|
||||
title := strings.TrimSpace(formdata.Title)
|
||||
if title == "" {
|
||||
return p.renderPage(engine, app, e, req, "Reihentitel ist erforderlich.")
|
||||
}
|
||||
|
||||
status := strings.TrimSpace(formdata.Status)
|
||||
if status == "" || !slices.Contains(dbmodels.EDITORSTATE_VALUES, status) {
|
||||
return p.renderPage(engine, app, e, req, "Ungültiger Status.")
|
||||
}
|
||||
|
||||
var createdSeries *dbmodels.Series
|
||||
user := req.User()
|
||||
if err := app.RunInTransaction(func(tx core.App) error {
|
||||
collection, err := tx.FindCollectionByNameOrId(dbmodels.SERIES_TABLE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
series := dbmodels.NewSeries(core.NewRecord(collection))
|
||||
nextID, err := nextSeriesMusenalmID(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
series.SetMusenalmID(nextID)
|
||||
applySeriesForm(series, formdata, title, status, user)
|
||||
if err := tx.Save(series); err != nil {
|
||||
return err
|
||||
}
|
||||
createdSeries = series
|
||||
return nil
|
||||
}); err != nil {
|
||||
app.Logger().Error("Failed to create series", "error", err)
|
||||
return p.renderPage(engine, app, e, req, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
if createdSeries == nil {
|
||||
return p.renderPage(engine, app, e, req, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
redirect := fmt.Sprintf(
|
||||
"/reihe/%d/edit?saved_message=%s",
|
||||
createdSeries.MusenalmID(),
|
||||
url.QueryEscape("Änderungen gespeichert."),
|
||||
)
|
||||
return e.Redirect(http.StatusSeeOther, redirect)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user