From 34ccc98a942098faefb5f4211b215ff9ccc7ad0e Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Mon, 9 Dec 2024 01:07:15 +0300 Subject: Начальный MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/api/guess.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/api/handler.go | 11 ++++++++++ pkg/api/next.go | 28 +++++++++++++++++++++++++ pkg/api/state.go | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 pkg/api/guess.go create mode 100644 pkg/api/handler.go create mode 100644 pkg/api/next.go create mode 100644 pkg/api/state.go (limited to 'pkg/api') diff --git a/pkg/api/guess.go b/pkg/api/guess.go new file mode 100644 index 0000000..25d08d6 --- /dev/null +++ b/pkg/api/guess.go @@ -0,0 +1,61 @@ +package api + +import ( + "time" + + "git.neonxp.ru/neonxp/guessr/pkg/middleware" + "github.com/labstack/echo/v4" +) + +func (h *Handler) PostGuess(c echo.Context) error { + req := &postGuessRequest{} + if err := c.Bind(req); err != nil { + return err + } + + if state := c.Get("state"); state == nil { + return echo.ErrNotFound + } + + state := c.Get("state").(*middleware.State) + + resp, err := h.places.Guess( + c.Request().Context(), + state.CurrentGUID, req.Lat, req.Lng) + if err != nil { + return err + } + + addPoints := 1000 - resp.Distance + if addPoints > 0 { + state.Points += addPoints + } + state.CurrentGUID = "" + state.Image = "" + if err := middleware.SetState(c, *state, int(365*24*time.Hour)); err != nil { + return err + } + + return c.JSON(200, postGuessResponse{ + GUID: resp.GUID, + Image: resp.Img, + Name: resp.Name, + Geojson: resp.Geojson, + Distance: resp.Distance, + State: state, + }) +} + +type postGuessRequest struct { + Lat float32 `json:"lat"` + Lng float32 `json:"lng"` +} + +type postGuessResponse struct { + GUID string `json:"guid"` + Image string `json:"image"` + Name string `json:"name"` + Geojson any `json:"geojson"` + Distance int `json:"distance"` + State *middleware.State `json:"state"` +} diff --git a/pkg/api/handler.go b/pkg/api/handler.go new file mode 100644 index 0000000..de1e8c3 --- /dev/null +++ b/pkg/api/handler.go @@ -0,0 +1,11 @@ +package api + +import "git.neonxp.ru/neonxp/guessr/pkg/service" + +type Handler struct { + places *service.Places +} + +func New(places *service.Places) *Handler { + return &Handler{places: places} +} diff --git a/pkg/api/next.go b/pkg/api/next.go new file mode 100644 index 0000000..c73af62 --- /dev/null +++ b/pkg/api/next.go @@ -0,0 +1,28 @@ +package api + +import ( + "time" + + "git.neonxp.ru/neonxp/guessr/pkg/middleware" + "github.com/labstack/echo/v4" +) + +func (h *Handler) PostNext(c echo.Context) error { + p, err := h.places.GetNext(c.Request().Context()) + if err != nil { + return err + } + + if state := c.Get("state"); state == nil { + return echo.ErrBadRequest + } + + state := c.Get("state").(*middleware.State) + state.CurrentGUID = p.GUID + state.Image = p.Img + if err := middleware.SetState(c, *state, int(365*24*time.Hour)); err != nil { + return err + } + + return c.JSON(200, state) +} 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"` +} -- cgit v1.2.3