knowyt/server/src/application/saveQuote.go

46 lines
977 B
Go
Raw Normal View History

2021-10-15 21:07:41 +00:00
package application
import (
"fmt"
"net/http"
2021-10-16 14:57:28 +00:00
"path"
2021-10-15 21:07:41 +00:00
"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
}
quoteId := r.URL.Query().Get("id")
quoteText := r.URL.Query().Get("quote")
2021-10-16 14:57:28 +00:00
gameDirName := path.Join(app.config.DataDir, "games", gm.GetId())
quoteFileNameShort := quoteId + ".json"
quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort)
err2 := gm.SaveQuote(
quoteFileName,
usr.GetId(),
quoteId,
quoteText,
)
if err2 != nil {
2021-10-15 21:07:41 +00:00
fmt.Printf("%s\n", err)
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
2021-10-16 14:57:28 +00:00
fmt.Fprintf(w, "ok")
2021-10-15 21:07:41 +00:00
}