Skip to content

Commit

Permalink
vingo: fix settings endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-dev committed Jul 20, 2024
1 parent e3cd90e commit 51fbb7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
20 changes: 13 additions & 7 deletions vingo/database/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package database

import "log"

func CreateSettings(user_id int) error {
_, err := db.Exec("INSERT INTO settings (user_id) VALUES ($1) ON CONFLICT DO NOTHING;", user_id)
return err
}

func GetSettings(user_id int) (*Settings, error) {
var user User
result := gorm_db.Preload("Settings").First(&user, user_id)
Expand All @@ -15,6 +10,17 @@ func GetSettings(user_id int) (*Settings, error) {
}

func UpdateSettings(user_id int, settings Settings) error {
_, err := db.Exec("UPDATE settings SET scan_in_out = $1, leaderboard = $2, public = $3 WHERE user_id = $4;", settings.ScanInOut, settings.Leaderboard, settings.Public, user_id)
return err
var user User
if err := gorm_db.Preload("Settings").First(&user, user_id).Error; err != nil {
return err
}

user.Settings.ScanInOut = settings.ScanInOut
user.Settings.Leaderboard = settings.Leaderboard
user.Settings.Public = settings.Public
if err := gorm_db.Save(&user.Settings).Error; err != nil {
return err
}

return nil
}
7 changes: 6 additions & 1 deletion vingo/handlers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ func (Settings) Update(c *fiber.Ctx) error {
return c.Status(400).SendString("Invalid payload")
}

err = database.UpdateSettings(user.Id, settings)
if err != nil {
logger.Println(err)
return c.Status(500).SendString("Error updating settings")
}

sess, _ := store.Get(c)
database.UpdateSettings(user.Id, settings)
user, _ = database.GetUser(user.Id)
sess.Set(STORE_USER, &user)
sess.Save()
Expand Down

0 comments on commit 51fbb7f

Please sign in to comment.