Skip to content

Commit

Permalink
vingo: ability to add name to card
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-dev committed Jul 16, 2024
1 parent 371b89f commit 2f3737c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 2 additions & 3 deletions vingo/database/cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func GetCardsForUser(user_id int) ([]Card, error) {
return cards, result.Error
}

func SetCardName(id int, name string, user_id int) error {
err := gorm_db.Model(&Card{}).Where("id = ? AND user_id = ?", id, user_id).Update("name", name).Error
return err
func UpdateCardName(id int, name string, user_id int) error {
return gorm_db.Model(&Card{}).Where("id = ? AND user_id = ?", id, user_id).Update("name", name).Error
}
23 changes: 23 additions & 0 deletions vingo/handlers/cards.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"strconv"
"time"
"vingo/database"

Expand Down Expand Up @@ -40,3 +41,25 @@ func CardRegisterStatus(c *fiber.Ctx) error {
is_current_user := registering_user == user.Id
return c.JSON(map[string]bool{"registering": register_ongoing, "isCurrentUser": is_current_user})
}

func CardNameUpdate(c *fiber.Ctx) error {
user := getUserFromStore(c)
card_id, err := strconv.Atoi(c.Params("id"))
if err != nil {
logger.Println(err)
return c.Status(400).SendString("Invalid card id")
}

payload := struct {
Name string `json:"name"`
}{}
c.BodyParser(&payload)

err = database.UpdateCardName(card_id, payload.Name, user.Id)
if err != nil {
logger.Println(err)
return c.Status(500).SendString("Error updating card name")
}

return c.SendString("Card name updated")
}
1 change: 1 addition & 0 deletions vingo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
api.Get("/leaderboard", handlers.Leaderboard)
api.Get("/scans", handlers.Scans)
api.Get("/cards", handlers.Cards)
api.Patch("/cards/:id", handlers.CardNameUpdate)
api.Get("/cards/register", handlers.CardRegisterStatus)
api.Post("/cards/register", handlers.StartCardRegister)
api.Get("/settings", handlers.Settings)
Expand Down

0 comments on commit 2f3737c

Please sign in to comment.