package application import ( "fmt" "github.com/google/uuid" "net/http" "path" "sirlab.de/go/knyt/game" "sirlab.de/go/knyt/user" ) func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http.Request) { gameRef := r.URL.Query().Get("g") gm, err := app.GetGameById(gameRef) if err != nil { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "game not found") return } if usr.GetGameId() != gameRef && !usr.IsGamemaster() { w.WriteHeader(http.StatusForbidden) fmt.Fprintf(w, "forbidden") return } quoteText := r.URL.Query().Get("quote") quoteId := r.URL.Query().Get("id") if quoteId == ":new:" { quoteNewUuid := uuid.NewString() err = gm.CreateQuote( app.getQuoteFileNameFromId(gm, quoteNewUuid), usr.GetId(), quoteNewUuid, quoteText, ) } else { err = gm.SaveQuote( app.getQuoteFileNameFromId(gm, quoteId), usr.GetId(), quoteId, quoteText, ) } if err != nil { fmt.Printf("%s", err) w.WriteHeader(http.StatusForbidden) fmt.Fprintf(w, "forbidden") return } fmt.Fprintf(w, "ok") } func (app *Application) getQuoteFileNameFromId(gm *game.Game, id string) string { gameDirName := path.Join(app.config.DataDir, "games", gm.GetId()) quoteFileNameShort := id + ".json" return path.Join(gameDirName, "quotes", quoteFileNameShort) }