implemented guess endpoint
This commit is contained in:
47
internal/rest_handler/guess_letter.go
Normal file
47
internal/rest_handler/guess_letter.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package rest_handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"gitea.elara.ws/Hazel/hangman/internal/game"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func GuessLetter(c echo.Context) error {
|
||||
session := game.GetSession(c.Param("session"))
|
||||
|
||||
sig := c.Request().Header.Get("signature")
|
||||
body, _ := io.ReadAll(c.Request().Body)
|
||||
|
||||
u := session.VerifySignature(sig, body)
|
||||
if u == nil {
|
||||
return c.String(http.StatusBadRequest, "This user was not fount in the current session.")
|
||||
}
|
||||
|
||||
if !session.CurrentUser.PublicKey.Equal(u.PublicKey) {
|
||||
return c.String(http.StatusBadRequest, "It's not the turn of user "+u.Name+". It's the turn of "+session.CurrentUser.Name+".")
|
||||
}
|
||||
|
||||
c.Request().Body = io.NopCloser(bytes.NewBuffer(body))
|
||||
|
||||
type BodyContent struct {
|
||||
Guess string
|
||||
}
|
||||
var bodyContent BodyContent
|
||||
|
||||
err := c.Bind(&bodyContent)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return c.String(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
_, err = session.GuessLetter(bodyContent.Guess)
|
||||
if err != nil {
|
||||
return c.String(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, session)
|
||||
}
|
||||
Reference in New Issue
Block a user