Implement session creation

This commit is contained in:
Vojtěch Káně
2021-03-29 20:37:52 +02:00
parent 782f96ebbd
commit a5bc7c6984
10 changed files with 180 additions and 4 deletions

View File

@@ -71,6 +71,38 @@ func (app *application) play(w http.ResponseWriter, r *http.Request, params http
}
func (app *application) createSession(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if err := r.ParseForm(); err != nil {
app.clientError(w, http.StatusBadRequest)
return
}
var code = strings.TrimSpace(r.PostForm.Get("code"))
var player = strings.ToLower(strings.TrimSpace(r.PostForm.Get("organiser")))
if len(player) < 1 {
app.clientError(w, http.StatusBadRequest)
return
}
if s, p, err := app.model.CreateSession(player, code, time.Now(), r.Context()); err == nil {
if su, err := app.model.GetPlayersStateUpdate(s.ID, r.Context()); err == nil {
app.rtClients.SendToAll(s.ID, su)
http.Redirect(w, r, "/game/"+url.PathEscape(p.ID.String()), http.StatusSeeOther)
return
} else {
app.serverError(w, err)
return
}
} else if errors.Is(err, model.NoSuchEntity) {
app.clientError(w, http.StatusNotFound)
return
} else {
app.serverError(w, err)
return
}
}
func (app *application) game(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
var playerUid uuid.UUID
if uid, err := uuid.Parse(params.ByName("playerUid")); err == nil {

View File

@@ -66,7 +66,7 @@ func main() {
mux := httprouter.New()
mux.GET("/", app.home)
mux.POST("/play/:code", app.play)
mux.POST("/organise/:code", app.play)
mux.POST("/session", app.createSession)
mux.GET("/game/:playerUid", app.game)
mux.POST("/game/:playerUid/rpc/next", app.nextQuestion)
mux.POST("/game/:playerUid/answers/:choiceUid", app.answer)