2021-10-01 19:19:31 +00:00
|
|
|
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 {
|
2021-10-16 19:47:45 +00:00
|
|
|
fmt.Printf("attempt to get quotes for invalid game id %s\n", gameRef)
|
2021-10-01 19:19:31 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(w, "game not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-16 19:47:45 +00:00
|
|
|
if usr.GetGameId() != gameRef && !usr.IsGamemaster() {
|
|
|
|
fmt.Printf("user's game id is %s\n", usr.GetGameId())
|
|
|
|
fmt.Printf("user not allowed to access game id %s\n", gameRef)
|
2021-10-01 19:19:31 +00:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
fmt.Fprintf(w, "forbidden")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-27 20:45:28 +00:00
|
|
|
quotesInfo := gm.GetQuotesInfoByUser(usr)
|
2021-10-01 19:19:31 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
jsonString, _ := json.Marshal(quotesInfo)
|
|
|
|
fmt.Fprintf(w, string(jsonString))
|
|
|
|
}
|