Implement PUT /user/{userId}/allowance/{allowanceId} (#52)

Closes #17

Reviewed-on: #52
This commit was merged in pull request #52.
This commit is contained in:
2025-05-17 16:20:05 +02:00
parent d1774c1ce0
commit 238aedb5c9
5 changed files with 109 additions and 1 deletions

View File

@@ -160,6 +160,33 @@ func (db *Db) DeleteAllowance(userId int, allowanceId int) error {
return nil
}
func (db *Db) UpdateAllowance(userId int, allowanceId int, allowance *UpdateAllowanceRequest) error {
// Check if the allowance exists for the user
count := 0
err := db.db.Query("select count(*) from allowances where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&count)
if err != nil {
return err
}
if count == 0 {
return errors.New("allowance not found")
}
tx, err := db.db.Begin()
if err != nil {
return err
}
defer tx.MustRollback()
err = tx.Query("update allowances set name=?, target=?, weight=? where id = ? and user_id = ?").
Bind(allowance.Name, allowance.Target, allowance.Weight, allowanceId, userId).
Exec()
if err != nil {
return err
}
return tx.Commit()
}
func (db *Db) CreateTask(task *CreateTaskRequest) (int, error) {
tx, err := db.db.Begin()
if err != nil {