Rename endpoints (#42)
Closes #39 Reviewed-on: #42
This commit was merged in pull request #42.
This commit is contained in:
@@ -67,27 +67,27 @@ func (db *Db) UserExists(userId int) (bool, error) {
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (db *Db) GetUserGoals(userId int) ([]Goal, error) {
|
||||
goals := make([]Goal, 0)
|
||||
func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) {
|
||||
allowances := make([]Allowance, 0)
|
||||
var err error
|
||||
|
||||
for row := range db.db.Query("select id, name, target, progress, weight from goals where user_id = ?").
|
||||
for row := range db.db.Query("select id, name, target, progress, weight from allowances where user_id = ?").
|
||||
Bind(userId).Range(&err) {
|
||||
goal := Goal{}
|
||||
err = row.Scan(&goal.ID, &goal.Name, &goal.Target, &goal.Progress, &goal.Weight)
|
||||
allowance := Allowance{}
|
||||
err = row.Scan(&allowance.ID, &allowance.Name, &allowance.Target, &allowance.Progress, &allowance.Weight)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
goals = append(goals, goal)
|
||||
allowances = append(allowances, allowance)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return goals, nil
|
||||
return allowances, nil
|
||||
}
|
||||
|
||||
func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
|
||||
// Check if user exists before attempting to create a goal
|
||||
func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (int, error) {
|
||||
// Check if user exists before attempting to create an allowance
|
||||
exists, err := db.UserExists(userId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -102,9 +102,9 @@ func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
|
||||
}
|
||||
defer tx.MustRollback()
|
||||
|
||||
// Insert the new goal
|
||||
err = tx.Query("insert into goals (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)").
|
||||
Bind(userId, goal.Name, goal.Target, goal.Weight).
|
||||
// Insert the new allowance
|
||||
err = tx.Query("insert into allowances (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)").
|
||||
Bind(userId, allowance.Name, allowance.Target, allowance.Weight).
|
||||
Exec()
|
||||
|
||||
if err != nil {
|
||||
@@ -127,21 +127,21 @@ 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
|
||||
func (db *Db) DeleteAllowance(userId int, allowanceId int) error {
|
||||
// Check if the allowance exists for the user
|
||||
count := 0
|
||||
err := db.db.Query("select count(*) from goals where id = ? and user_id = ?").
|
||||
Bind(goalId, userId).ScanSingle(&count)
|
||||
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("goal not found")
|
||||
return errors.New("allowance not found")
|
||||
}
|
||||
|
||||
// Delete the goal
|
||||
err = db.db.Query("delete from goals where id = ? and user_id = ?").
|
||||
Bind(goalId, userId).Exec()
|
||||
// Delete the allowance
|
||||
err = db.db.Query("delete from allowances where id = ? and user_id = ?").
|
||||
Bind(allowanceId, userId).Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -236,7 +236,7 @@ func (db *Db) UpdateTask(id int, task *CreateTaskRequest) error {
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (db *Db) AddAllowance(userId int, allowance *PostAllowance) error {
|
||||
func (db *Db) AddHistory(userId int, allowance *PostHistory) error {
|
||||
tx, err := db.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -252,13 +252,13 @@ func (db *Db) AddAllowance(userId int, allowance *PostAllowance) error {
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (db *Db) GetHistory(userId int) ([]Allowance, error) {
|
||||
history := make([]Allowance, 0)
|
||||
func (db *Db) GetHistory(userId int) ([]History, error) {
|
||||
history := make([]History, 0)
|
||||
var err error
|
||||
|
||||
for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc").
|
||||
Bind(userId).Range(&err) {
|
||||
allowance := Allowance{}
|
||||
allowance := History{}
|
||||
var timestamp int64
|
||||
err = row.Scan(&allowance.Allowance, ×tamp)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user