Skip to content

Commit

Permalink
vingo: card register status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-dev committed Jul 16, 2024
1 parent c42c99e commit 371b89f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
26 changes: 8 additions & 18 deletions vingo/handlers/cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gofiber/fiber/v2"
)

func StartCardRegisterAPI(c *fiber.Ctx) error {
func StartCardRegister(c *fiber.Ctx) error {
user := getUserFromStore(c)

if time.Now().Before(registering_end) {
Expand All @@ -23,23 +23,6 @@ func StartCardRegisterAPI(c *fiber.Ctx) error {
return c.Status(200).JSON(map[string]bool{})
}

func StartCardRegister(c *fiber.Ctx) error {
// keep track of the user that initiated the request in global state
// since only one user can be registering a card at a time
user := getUserFromStore(c)

if time.Now().Before(registering_end) {
return c.Status(400).SendString("Another user is already registering a card")
}

registering_user = user.Id
registering_end = time.Now().Add(time.Minute)

logger.Println("Card registration started by user", registering_user)

return c.Status(200).Redirect("/cards")
}

func Cards(c *fiber.Ctx) error {
user := getUserFromStore(c)
cards, err := database.GetCardsForUser(user.Id)
Expand All @@ -50,3 +33,10 @@ func Cards(c *fiber.Ctx) error {

return c.JSON(cards)
}

func CardRegisterStatus(c *fiber.Ctx) error {
user := getUserFromStore(c)
register_ongoing := time.Now().Before(registering_end)
is_current_user := registering_user == user.Id
return c.JSON(map[string]bool{"registering": register_ongoing, "isCurrentUser": is_current_user})
}
3 changes: 2 additions & 1 deletion vingo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func main() {
api.Get("/leaderboard", handlers.Leaderboard)
api.Get("/scans", handlers.Scans)
api.Get("/cards", handlers.Cards)
api.Post("/cards/register", handlers.StartCardRegisterAPI)
api.Get("/cards/register", handlers.CardRegisterStatus)
api.Post("/cards/register", handlers.StartCardRegister)
api.Get("/settings", handlers.Settings)

admin := api.Group("/admin", handlers.IsAdmin)
Expand Down

0 comments on commit 371b89f

Please sign in to comment.