save quote

This commit is contained in:
Settel 2021-10-16 16:57:28 +02:00
parent 6f974195b2
commit b50c6847d6
5 changed files with 41 additions and 10 deletions

View File

@ -1,4 +1,4 @@
{ {
"source": "7de2fcfa-6ac0-4ff6-8337-a224effc826d", "quote": "Scher dich zum Donnerdrummel!",
"quote": "Ein Diktumanuist ist eine Person, die Wörter erfindet und sie als Selbstzitat getarnt im Internet veröffentlicht." "source": "7de2fcfa-6ac0-4ff6-8337-a224effc826d"
} }

View File

@ -31,10 +31,10 @@ func (app *Application) RemoveQuote(usr *user.User, w http.ResponseWriter, r *ht
return return
} else { } else {
gameDirName := path.Join(app.config.DataDir, "games", gm.GetId()) gameDirName := path.Join(app.config.DataDir, "games", gm.GetId())
quotesFileNameShort := quote.GetId() + ".json" quoteFileNameShort := quote.GetId() + ".json"
quotesFileName := path.Join(gameDirName, "quotes", quotesFileNameShort) quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort)
if err := os.Remove(quotesFileName); err != nil { if err := os.Remove(quoteFileName); err != nil {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden") fmt.Fprintf(w, "forbidden")

View File

@ -3,6 +3,7 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"path"
"sirlab.de/go/knyt/user" "sirlab.de/go/knyt/user"
) )
@ -23,10 +24,22 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
quoteId := r.URL.Query().Get("id") quoteId := r.URL.Query().Get("id")
quoteText := r.URL.Query().Get("quote") quoteText := r.URL.Query().Get("quote")
if err := gm.SaveQuote(usr.GetId(), quoteId, quoteText); err != nil { 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 {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden") fmt.Fprintf(w, "forbidden")
return return
} }
fmt.Fprintf(w, "ok")
} }

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
) )
func (gm *Game) SaveQuote(usrId string, quoteId string, quoteText string) error { func (gm *Game) SaveQuote(fileName, usrId, quoteId, quoteText string) error {
quote, err := gm.getQuoteById(quoteId) quote, err := gm.getQuoteById(quoteId)
if err != nil { if err != nil {
return err return err
@ -14,7 +14,7 @@ func (gm *Game) SaveQuote(usrId string, quoteId string, quoteText string) error
return fmt.Errorf("usrId does not match quote source id") return fmt.Errorf("usrId does not match quote source id")
} }
fmt.Printf("%v\n", quote) quote.SetQuote(quoteText)
return fmt.Errorf("Game::SaveQuote() not yet implemented") return quote.Save(fileName)
} }

View File

@ -31,6 +31,20 @@ func NewQuoteFromFile(fileName string) (*Quote, error) {
} }
} }
func (qu *Quote) Save(fileName string) error {
quJson := QuoteJson{
Quote: qu.quote,
SourceId: qu.sourceId,
}
quoteJson, err := json.MarshalIndent(quJson, "", "\t")
if err != nil {
return err
}
return os.WriteFile(fileName, quoteJson, 0777)
}
func (qu *Quote) GetId() string { func (qu *Quote) GetId() string {
return qu.id return qu.id
} }
@ -43,6 +57,10 @@ func (qu *Quote) GetQuote() string {
return qu.quote return qu.quote
} }
func (qu *Quote) SetQuote(quote string) {
qu.quote = quote
}
func (qu *Quote) IsPlayed() bool { func (qu *Quote) IsPlayed() bool {
return qu.isPlayed return qu.isPlayed
} }