2015-11-13 17:04:42 +08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2016-01-31 20:13:34 +08:00
|
|
|
"fmt"
|
2015-11-13 17:04:42 +08:00
|
|
|
"strings"
|
2015-11-14 18:10:21 +08:00
|
|
|
"time"
|
2016-01-31 20:13:34 +08:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
|
|
|
|
"github.com/thewhitetulip/Tasks/types"
|
2015-11-13 17:04:42 +08:00
|
|
|
)
|
|
|
|
|
2016-01-31 20:13:34 +08:00
|
|
|
var database Database
|
2015-11-13 17:04:42 +08:00
|
|
|
var err error
|
|
|
|
|
2016-01-31 20:13:34 +08:00
|
|
|
//Database encapsulates database
|
|
|
|
type Database struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db Database) begin() (tx *sql.Tx) {
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db Database) prepare(q string) (stmt *sql.Stmt) {
|
|
|
|
stmt, err := db.db.Prepare(q)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db Database) query(q string, args ...interface{}) (rows *sql.Rows) {
|
|
|
|
rows, err := db.db.Query(q, args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:04:42 +08:00
|
|
|
func init() {
|
2016-01-31 20:13:34 +08:00
|
|
|
database = Database{}
|
|
|
|
database.db, err = sql.Open("sqlite3", "./tasks.db")
|
2015-11-13 17:04:42 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//Close function closes this database connection
|
2015-11-13 17:04:42 +08:00
|
|
|
func Close() {
|
2016-01-31 20:13:34 +08:00
|
|
|
database.db.Close()
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//GetTasks retrieves all the tasks depending on the
|
|
|
|
//status pending or trashed or completed
|
2015-11-21 21:20:51 +08:00
|
|
|
func GetTasks(status string) types.Context {
|
2015-11-13 17:04:42 +08:00
|
|
|
var task []types.Task
|
2015-11-21 21:20:51 +08:00
|
|
|
var context types.Context
|
2015-11-21 14:39:15 +08:00
|
|
|
var TaskID int
|
2015-11-13 17:04:42 +08:00
|
|
|
var TaskTitle string
|
|
|
|
var TaskContent string
|
2015-11-14 18:10:21 +08:00
|
|
|
var TaskCreated time.Time
|
2015-11-13 17:04:42 +08:00
|
|
|
var getTasksql string
|
2015-11-21 21:20:51 +08:00
|
|
|
|
2015-11-14 18:56:53 +08:00
|
|
|
if status == "pending" {
|
2016-01-31 20:13:34 +08:00
|
|
|
getTasksql = "select id, title, content, created_date from task where finish_date is null and is_deleted='N' order by created_date asc"
|
2015-11-21 21:20:51 +08:00
|
|
|
} else if status == "deleted" {
|
2016-01-31 20:13:34 +08:00
|
|
|
getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc"
|
2015-11-21 21:20:51 +08:00
|
|
|
} else if status == "completed" {
|
2016-01-31 20:13:34 +08:00
|
|
|
getTasksql = "select id, title, content, created_date from task where finish_date is not null order by created_date asc"
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
|
2016-01-31 20:13:34 +08:00
|
|
|
rows := database.query(getTasksql)
|
2015-11-13 17:04:42 +08:00
|
|
|
defer rows.Close()
|
2015-11-13 21:11:30 +08:00
|
|
|
for rows.Next() {
|
2016-01-31 20:13:34 +08:00
|
|
|
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated)
|
|
|
|
TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
2015-11-14 18:10:21 +08:00
|
|
|
TaskCreated = TaskCreated.Local()
|
2016-01-31 20:13:34 +08:00
|
|
|
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]}
|
2015-11-13 17:04:42 +08:00
|
|
|
task = append(task, a)
|
|
|
|
}
|
2015-11-21 21:20:51 +08:00
|
|
|
context = types.Context{Tasks: task, Navigation: status}
|
|
|
|
return context
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-22 11:51:29 +08:00
|
|
|
//GetTaskByID function gets the tasks from the ID passed to the function, used to populate EditTask
|
2015-11-22 10:59:39 +08:00
|
|
|
func GetTaskByID(id int) types.Context {
|
|
|
|
var tasks []types.Task
|
2015-11-13 17:04:42 +08:00
|
|
|
var task types.Task
|
2016-01-31 20:13:34 +08:00
|
|
|
var TaskID int
|
|
|
|
var TaskTitle string
|
|
|
|
var TaskContent string
|
|
|
|
getTasksql := "select id, title, content from task where id=?"
|
2016-01-23 21:47:34 +08:00
|
|
|
|
2016-01-31 20:13:34 +08:00
|
|
|
rows := database.query(getTasksql, id)
|
2015-11-13 17:04:42 +08:00
|
|
|
defer rows.Close()
|
2015-11-13 21:11:30 +08:00
|
|
|
if rows.Next() {
|
2016-01-31 20:13:34 +08:00
|
|
|
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
2016-01-31 20:13:34 +08:00
|
|
|
task = types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent}
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
2015-11-22 10:59:39 +08:00
|
|
|
tasks = append(tasks, task)
|
|
|
|
context := types.Context{Tasks: tasks, Navigation: "edit"}
|
|
|
|
return context
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//TrashTask is used to delete the task
|
2015-11-14 18:56:53 +08:00
|
|
|
func TrashTask(id int) error {
|
2016-01-31 20:13:34 +08:00
|
|
|
trashSQL := database.prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
|
|
|
|
tx := database.begin()
|
2015-11-21 14:39:15 +08:00
|
|
|
_, err = tx.Stmt(trashSQL).Exec(id)
|
2015-11-14 18:56:53 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println("doing rollback")
|
2015-11-14 18:56:53 +08:00
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//CompleteTask is used to mark tasks as complete
|
2015-11-14 18:56:53 +08:00
|
|
|
func CompleteTask(id int) error {
|
2016-01-31 20:13:34 +08:00
|
|
|
stmt := database.prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?")
|
|
|
|
tx := database.begin()
|
2015-11-13 17:04:42 +08:00
|
|
|
_, err = tx.Stmt(stmt).Exec(id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//DeleteAll is used to empty the trash
|
2015-11-13 17:04:42 +08:00
|
|
|
func DeleteAll() error {
|
2016-01-31 20:13:34 +08:00
|
|
|
stmt := database.prepare("delete from task where is_deleted='Y'")
|
|
|
|
tx := database.begin()
|
2015-11-13 17:04:42 +08:00
|
|
|
_, err = tx.Stmt(stmt).Exec()
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println("doing rollback")
|
2015-11-13 17:04:42 +08:00
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//RestoreTask is used to restore tasks from the Trash
|
2015-11-13 17:04:42 +08:00
|
|
|
func RestoreTask(id int) error {
|
2016-01-31 20:13:34 +08:00
|
|
|
restoreSQL := database.prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?")
|
|
|
|
tx := database.begin()
|
2015-11-21 14:39:15 +08:00
|
|
|
_, err = tx.Stmt(restoreSQL).Exec(id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println("doing rollback")
|
2016-01-09 13:03:35 +08:00
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//DeleteTask is used to delete the task from the database
|
2015-11-13 17:04:42 +08:00
|
|
|
func DeleteTask(id int) error {
|
2016-01-31 20:13:34 +08:00
|
|
|
deleteSQL := database.prepare("delete from task where id = ?")
|
|
|
|
tx := database.begin()
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
_, err = tx.Stmt(deleteSQL).Exec(id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//AddTask is used to add the task in the database
|
2016-01-31 20:13:34 +08:00
|
|
|
func AddTask(title, content string) error {
|
|
|
|
restoreSQL := database.prepare("insert into task(title, content, created_date, last_modified_at) values(?,?,datetime(), datetime())")
|
|
|
|
tx := database.begin()
|
|
|
|
_, err = tx.Stmt(restoreSQL).Exec(title, content)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//UpdateTask is used to update the tasks in the database
|
2015-11-13 21:11:30 +08:00
|
|
|
func UpdateTask(id int, title string, content string) error {
|
2016-01-31 20:13:34 +08:00
|
|
|
SQL := database.prepare("update task set title=?, content=? where id=?")
|
|
|
|
tx := database.begin()
|
2015-11-21 14:39:15 +08:00
|
|
|
_, err = tx.Stmt(SQL).Exec(title, content, id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(tx.Commit())
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:39:15 +08:00
|
|
|
//SearchTask is used to return the search results depending on the query
|
2015-11-21 22:12:20 +08:00
|
|
|
func SearchTask(query string) types.Context {
|
2015-11-21 19:03:34 +08:00
|
|
|
stmt := "select id, title, content, created_date from task where title like '%" + query + "%' or content like '%" + query + "%'"
|
2015-11-13 17:04:42 +08:00
|
|
|
var task []types.Task
|
2015-11-21 14:39:15 +08:00
|
|
|
var TaskID int
|
2015-11-13 17:04:42 +08:00
|
|
|
var TaskTitle string
|
|
|
|
var TaskContent string
|
2015-11-21 19:03:34 +08:00
|
|
|
var TaskCreated time.Time
|
2015-11-21 22:12:20 +08:00
|
|
|
var context types.Context
|
2015-11-13 17:04:42 +08:00
|
|
|
|
2016-01-31 20:13:34 +08:00
|
|
|
rows := database.query(stmt, query, query)
|
|
|
|
|
2015-11-13 21:11:30 +08:00
|
|
|
for rows.Next() {
|
2015-11-21 19:03:34 +08:00
|
|
|
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2016-01-31 20:13:34 +08:00
|
|
|
fmt.Println(err)
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
TaskTitle = strings.Replace(TaskTitle, query, "<span class='highlight'>"+query+"</span>", -1)
|
|
|
|
TaskContent = strings.Replace(TaskContent, query, "<span class='highlight'>"+query+"</span>", -1)
|
2015-11-21 19:03:34 +08:00
|
|
|
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]}
|
2015-11-13 17:04:42 +08:00
|
|
|
task = append(task, a)
|
|
|
|
}
|
2015-11-21 22:12:20 +08:00
|
|
|
context = types.Context{Tasks: task, Search: query}
|
|
|
|
return context
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|