summaryrefslogtreecommitdiff
path: root/pkg/api/next.go
blob: c73af6247e5a0ff5260dabf6f668ad09813b6ae4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)
}