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 ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"path" "path"
"sirlab.de/go/knyt/user" "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") quoteId := r.URL.Query().Get("id")
if quote, err := gm.RemoveQuote(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()) gameDirName := path.Join(app.config.DataDir, "games", gm.GetId())
quoteFileNameShort := quote.GetId() + ".json" quoteFileNameShort := quoteId + ".json"
quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort) quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort)
if err := os.Remove(quoteFileName); err != nil { if err := gm.RemoveQuote(quoteFileName, usr.GetId(), quoteId); 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")
return return
} }
fmt.Fprintf(w, "ok") fmt.Fprintf(w, "ok")
} }
}

View File

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