fetch list of my quotes
This commit is contained in:
parent
748d6991a4
commit
48cd97ad5a
@ -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."
|
||||
}
|
36
server/src/application/getQuotes.go
Normal file
36
server/src/application/getQuotes.go
Normal 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))
|
||||
}
|
32
server/src/game/getQuotesInfoByUser.go
Normal file
32
server/src/game/getQuotesInfoByUser.go
Normal 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
|
||||
}
|
@ -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/"))
|
||||
|
Loading…
Reference in New Issue
Block a user