diff --git a/server/data/games/067fb1b8-8303-4faa-95d2-1832770a791c/quotes/7f3a2876-b8b1-4b29-88be-70d86e1809e8.json b/server/data/games/067fb1b8-8303-4faa-95d2-1832770a791c/quotes/7f3a2876-b8b1-4b29-88be-70d86e1809e8.json new file mode 100644 index 0000000..0aa9f1f --- /dev/null +++ b/server/data/games/067fb1b8-8303-4faa-95d2-1832770a791c/quotes/7f3a2876-b8b1-4b29-88be-70d86e1809e8.json @@ -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." +} diff --git a/server/src/application/getQuotes.go b/server/src/application/getQuotes.go new file mode 100644 index 0000000..5350c9c --- /dev/null +++ b/server/src/application/getQuotes.go @@ -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)) +} diff --git a/server/src/game/getQuotesInfoByUser.go b/server/src/game/getQuotesInfoByUser.go new file mode 100644 index 0000000..61d1e8f --- /dev/null +++ b/server/src/game/getQuotesInfoByUser.go @@ -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 +} diff --git a/server/src/knowyt.go b/server/src/knowyt.go index 0986cc7..5e6cb4c 100644 --- a/server/src/knowyt.go +++ b/server/src/knowyt.go @@ -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/"))