fetch list of my quotes

This commit is contained in:
Settel 2021-10-01 21:19:31 +02:00
parent 748d6991a4
commit 48cd97ad5a
4 changed files with 73 additions and 0 deletions

View File

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

View File

@ -0,0 +1,36 @@
package application
import (
"encoding/json"
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
func (app *Application) GetQuotes(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
}
quotesInfo, err := gm.GetQuotesInfoByUser(usr)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(quotesInfo)
fmt.Fprintf(w, string(jsonString))
}

View File

@ -0,0 +1,32 @@
package game
import (
"sirlab.de/go/knyt/user"
)
type Quote struct {
Id string `json:"id"`
Quote string `json:"quote"`
}
type QuotesInfo struct {
Quotes []Quote `json:"quotes"`
}
func (gm *Game) GetQuotesInfoByUser(usr *user.User) (*QuotesInfo, error) {
gm.mu.Lock()
defer gm.mu.Unlock()
usrId := usr.GetId()
quotes := make([]Quote, 0)
for _, quote := range gm.quotes {
if quote.GetSourceId() == usrId {
quotes = append(quotes, Quote{
Id: quote.GetId(),
Quote: quote.GetQuote(),
})
}
}
return &QuotesInfo{Quotes: quotes}, nil
}

View File

@ -29,6 +29,7 @@ func main() {
mux.PrivateHandleFunc("/api/continueGame", app.ContinueGame)
mux.PrivateHandleFunc("/api/finishGame", app.FinishGame)
mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection)
mux.PrivateHandleFunc("/api/getQuotes", app.GetQuotes)
// default handler
fsHandler := http.FileServer(http.Dir("../../client/dist/"))