Accept answers

This commit is contained in:
Vojtěch Káně
2021-02-07 18:29:36 +01:00
parent 753b164c58
commit 4fc9aa50d1
5 changed files with 104 additions and 3 deletions

View File

@@ -126,3 +126,33 @@ func (app *application) nextQuestion(w http.ResponseWriter, r *http.Request, par
return
}
}
func (app *application) answer(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
var playerUid uuid.UUID
if uid, err := uuid.Parse(params.ByName("playerUid")); err == nil {
playerUid = uid
} else {
app.clientError(w, http.StatusBadRequest)
return
}
var choiceUid uuid.UUID
if uid, err := uuid.Parse(params.ByName("choiceUid")); err == nil {
choiceUid = uid
} else {
app.clientError(w, http.StatusBadRequest)
return
}
if _, err := app.model.SaveAnswer(playerUid, choiceUid, r.Context()); err == nil {
// TODO notify organisers
w.WriteHeader(http.StatusCreated) // TODO or StatusNoContent?
return
} else if errors.Is(err, model.NoSuchEntity) {
app.clientError(w, http.StatusNotFound)
return
} else {
app.serverError(w, err)
return
}
}

View File

@@ -69,6 +69,7 @@ func main() {
mux.POST("/organise/:code", app.play)
mux.GET("/game/:playerUid", app.game)
mux.POST("/game/:playerUid/rpc/next", app.nextQuestion)
mux.POST("/game/:playerUid/answers/:choiceUid", app.answer)
mux.GET("/ws/:playerUid", app.processWebSocket)