Add delete goal endpoint (#27)

Closes #18

Reviewed-on: #27
This commit was merged in pull request #27.
This commit is contained in:
2025-05-08 15:46:23 +02:00
parent d251d41650
commit 0668228139
3 changed files with 148 additions and 19 deletions

View File

@@ -128,3 +128,25 @@ func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
return lastId, nil
}
func (db *Db) DeleteGoal(userId int, goalId int) error {
// Check if the goal exists for the user
count := 0
err := db.db.Query("select count(*) from goals where id = ? and user_id = ?").
Bind(goalId, userId).ScanSingle(&count)
if err != nil {
return err
}
if count == 0 {
return errors.New("goal not found")
}
// Delete the goal
err = db.db.Query("delete from goals where id = ? and user_id = ?").
Bind(goalId, userId).Exec()
if err != nil {
return err
}
return nil
}