summaryrefslogtreecommitdiff
path: root/pkg/api/state.go
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-12-09 01:07:15 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-12-09 01:07:15 +0300
commit34ccc98a942098faefb5f4211b215ff9ccc7ad0e (patch)
tree7696ab4d7c8d9fb09c7e2575d482517f68824ae3 /pkg/api/state.go
Начальный
Diffstat (limited to 'pkg/api/state.go')
-rw-r--r--pkg/api/state.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/pkg/api/state.go b/pkg/api/state.go
new file mode 100644
index 0000000..5ddd0b3
--- /dev/null
+++ b/pkg/api/state.go
@@ -0,0 +1,45 @@
+package api
+
+import (
+ "time"
+
+ "git.neonxp.ru/neonxp/guessr/pkg/middleware"
+ "github.com/avito-tech/normalize"
+ "github.com/labstack/echo/v4"
+)
+
+func (h *Handler) GetState(c echo.Context) error {
+ if state := c.Get("state"); state == nil {
+ return c.JSON(200, &middleware.State{})
+ }
+
+ state := c.Get("state").(*middleware.State)
+
+ return c.JSON(200, state)
+}
+
+func (h *Handler) PostState(c echo.Context) error {
+ req := &postStateRequest{}
+ if err := c.Bind(req); err != nil {
+ return err
+ }
+
+ username := normalize.Normalize(req.Username)
+ if len(username) < 3 {
+ return echo.ErrBadRequest
+ }
+
+ state := &middleware.State{
+ Username: username,
+ }
+
+ if err := middleware.SetState(c, *state, int(365*24*time.Hour)); err != nil {
+ return err
+ }
+
+ return c.JSON(200, state)
+}
+
+type postStateRequest struct {
+ Username string `json:"username"`
+}