Started to extract out the common query which is performed by task related actions.

This commit is contained in:
Gergely Brautigam 2016-01-31 14:56:14 +01:00
parent 63278e72f3
commit cfcec48ca4
1 changed files with 13 additions and 69 deletions

View File

@ -116,112 +116,56 @@ func GetTaskByID(id int) types.Context {
//TrashTask is used to delete the task //TrashTask is used to delete the task
func TrashTask(id int) error { func TrashTask(id int) error {
trashSQL := database.prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?") err := taskQuery("update task set is_deleted='Y',last_modified_at=datetime() where id=?", id)
tx := database.begin()
_, err = tx.Stmt(trashSQL).Exec(id)
if err != nil {
log.Println("doing rollback")
tx.Rollback()
} else {
tx.Commit()
}
return err return err
} }
//CompleteTask is used to mark tasks as complete //CompleteTask is used to mark tasks as complete
func CompleteTask(id int) error { func CompleteTask(id int) error {
stmt := database.prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?") err := taskQuery("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?", id)
tx := database.begin()
_, err = tx.Stmt(stmt).Exec(id)
if err != nil {
log.Println(err)
tx.Rollback()
} else {
tx.Commit()
}
return err return err
} }
//DeleteAll is used to empty the trash //DeleteAll is used to empty the trash
func DeleteAll() error { func DeleteAll() error {
stmt := database.prepare("delete from task where is_deleted='Y'") err := taskQuery("delete from task where is_deleted='Y'")
tx := database.begin()
_, err = tx.Stmt(stmt).Exec()
if err != nil {
log.Println("doing rollback")
tx.Rollback()
} else {
tx.Commit()
}
return err return err
} }
//RestoreTask is used to restore tasks from the Trash //RestoreTask is used to restore tasks from the Trash
func RestoreTask(id int) error { func RestoreTask(id int) error {
restoreSQL := database.prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?") err := taskQuery("update task set is_deleted='N',last_modified_at=datetime() where id=?", id)
tx := database.begin()
_, err = tx.Stmt(restoreSQL).Exec(id)
if err != nil {
log.Println("doing rollback")
tx.Rollback()
} else {
tx.Commit()
}
return err return err
} }
//RestoreTaskFromComplete is used to restore tasks from the Trash //RestoreTaskFromComplete is used to restore tasks from the Trash
func RestoreTaskFromComplete(id int) error { func RestoreTaskFromComplete(id int) error {
restoreSQL := database.prepare("update task set finish_date=null,last_modified_at=datetime() where id=?") err := taskQuery("update task set finish_date=null,last_modified_at=datetime() where id=?", id)
tx := database.begin()
_, err = tx.Stmt(restoreSQL).Exec(id)
if err != nil {
log.Println("doing rollback")
tx.Rollback()
} else {
tx.Commit()
}
return err return err
} }
//DeleteTask is used to delete the task from the database //DeleteTask is used to delete the task from the database
func DeleteTask(id int) error { func DeleteTask(id int) error {
deleteSQL := database.prepare("delete from task where id = ?") err := taskQuery("delete from task where id = ?", id)
tx := database.begin()
_, err = tx.Stmt(deleteSQL).Exec(id)
if err != nil {
log.Println(err)
tx.Rollback()
} else {
tx.Commit()
}
return err return err
} }
//AddTask is used to add the task in the database //AddTask is used to add the task in the database
func AddTask(title, content string, taskPriority int) error { func AddTask(title, content string, taskPriority int) error {
restoreSQL := database.prepare("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())") err := taskQuery("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())", title, content, taskPriority)
tx := database.begin()
_, err = tx.Stmt(restoreSQL).Exec(title, content, taskPriority)
if err != nil {
log.Println(err)
tx.Rollback()
} else {
tx.Commit()
}
return err return err
} }
//UpdateTask is used to update the tasks in the database //UpdateTask is used to update the tasks in the database
func UpdateTask(id int, title string, content string) error { func UpdateTask(id int, title string, content string) error {
err := taskQuery("update task set title=?, content=? where id=?", title, content)
return err
}
func taskQuery(sql string, args ...interface{}) error {
SQL := database.prepare("update task set title=?, content=? where id=?") SQL := database.prepare("update task set title=?, content=? where id=?")
tx := database.begin() tx := database.begin()
_, err = tx.Stmt(SQL).Exec(title, content, id) _, err = tx.Stmt(SQL).Exec(args)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
tx.Rollback() tx.Rollback()