diff --git a/backend/db.go b/backend/db.go index 78f41f8..31ba5e3 100644 --- a/backend/db.go +++ b/backend/db.go @@ -160,6 +160,22 @@ func (db *Db) DeleteAllowance(userId int, allowanceId int) error { return nil } +func (db *Db) UpdateUserAllowance(userId int, allowance *UpdateAllowanceRequest) error { + tx, err := db.db.Begin() + if err != nil { + return err + } + defer tx.MustRollback() + + err = tx.Query("update users set weight=? where id = ?"). + Bind(allowance.Weight, userId). + Exec() + if err != nil { + return err + } + return tx.Commit() +} + func (db *Db) UpdateAllowance(userId int, allowanceId int, allowance *UpdateAllowanceRequest) error { // Check if the allowance exists for the user count := 0 diff --git a/backend/main.go b/backend/main.go index 16b20a3..ab99b33 100644 --- a/backend/main.go +++ b/backend/main.go @@ -268,7 +268,11 @@ func putUserAllowance(c *gin.Context) { return } - err = db.UpdateAllowance(userId, allowanceId, &allowanceRequest) + if allowanceId == 0 { + err = db.UpdateUserAllowance(userId, &allowanceRequest) + } else { + err = db.UpdateAllowance(userId, allowanceId, &allowanceRequest) + } if err != nil { log.Printf("Error updating allowance: %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})