remove quote

This commit is contained in:
Settel 2021-10-16 16:16:37 +02:00
parent 680ec9c6ce
commit 6f974195b2
2 changed files with 21 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"path"
"sirlab.de/go/knyt/user" "sirlab.de/go/knyt/user"
) )
@ -22,10 +24,23 @@ 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 err := gm.RemoveQuote(usr.GetId(), quoteId); err != nil { 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())
quotesFileNameShort := quote.GetId() + ".json"
quotesFileName := path.Join(gameDirName, "quotes", quotesFileNameShort)
if err := os.Remove(quotesFileName); 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")
}
} }

View File

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