Add delete goal endpoint (#27)
Closes #18 Reviewed-on: #27
This commit was merged in pull request #27.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user