save and remove quote

This commit is contained in:
Settel 2021-10-16 19:36:39 +02:00
parent df5f3f8250
commit 1df31a6e35
2 changed files with 19 additions and 26 deletions

View File

@ -3,7 +3,6 @@ package application
import (
"fmt"
"net/http"
"os"
"path"
"sirlab.de/go/knyt/user"
)
@ -24,23 +23,15 @@ func (app *Application) RemoveQuote(usr *user.User, w http.ResponseWriter, r *ht
}
quoteId := r.URL.Query().Get("id")
if quote, err := gm.RemoveQuote(usr.GetId(), quoteId); err != nil {
gameDirName := path.Join(app.config.DataDir, "games", gm.GetId())
quoteFileNameShort := quoteId + ".json"
quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort)
if err := gm.RemoveQuote(quoteFileName, usr.GetId(), quoteId); err != nil {
fmt.Printf("%s\n", err)
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
} else {
gameDirName := path.Join(app.config.DataDir, "games", gm.GetId())
quoteFileNameShort := quote.GetId() + ".json"
quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort)
if err := os.Remove(quoteFileName); err != nil {
fmt.Printf("%s\n", err)
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
fmt.Fprintf(w, "ok")
}
fmt.Fprintf(w, "ok")
}

View File

@ -2,20 +2,22 @@ package game
import (
"fmt"
"sirlab.de/go/knyt/quote"
"os"
)
func (gm *Game) RemoveQuote(usrId string, quoteId string) (*quote.Quote, error) {
quote, err := gm.getQuoteById(quoteId)
func (gm *Game) RemoveQuote(fileName, usrId, quoteId string) error {
if quote, err := gm.getQuoteById(quoteId); err != nil {
return err
} else {
if quote.GetSourceId() != usrId {
return fmt.Errorf("usrId does not match quote source id")
}
}
err := os.Remove(fileName)
if err != nil {
return nil, err
return err
}
if quote.GetSourceId() != usrId {
return nil, fmt.Errorf("usrId does not match quote source id")
}
fmt.Printf("%v\n", quote)
return quote, nil
return nil
}