diff --git a/vingo/handlers/cards.go b/vingo/handlers/cards.go index e7687db..157df8e 100644 --- a/vingo/handlers/cards.go +++ b/vingo/handlers/cards.go @@ -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) { @@ -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) @@ -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}) +} diff --git a/vingo/main.go b/vingo/main.go index 703683d..13607c0 100644 --- a/vingo/main.go +++ b/vingo/main.go @@ -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)