2015-11-13 17:04:42 +08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/thewhitetulip/task/types"
|
|
|
|
"strings"
|
2015-11-14 18:10:21 +08:00
|
|
|
"time"
|
2015-11-13 17:04:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var database *sql.DB
|
|
|
|
var err error
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
database, err = sql.Open("sqlite3", "./tasks.db")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Close() {
|
|
|
|
database.Close()
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:56:53 +08:00
|
|
|
func GetTasks(status string) []types.Task {
|
2015-11-13 17:04:42 +08:00
|
|
|
var task []types.Task
|
|
|
|
var TaskId int
|
|
|
|
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-14 18:56:53 +08:00
|
|
|
if status == "pending" {
|
|
|
|
getTasksql = "select id, title, content, created_date from task where finish_date is null and is_deleted='N' order by created_date asc"
|
|
|
|
} else if status == "trashed" {
|
2015-11-14 18:10:21 +08:00
|
|
|
getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc"
|
2015-11-14 18:56:53 +08:00
|
|
|
} else if status == "complete" {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := database.Query(getTasksql)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2015-11-13 21:11:30 +08:00
|
|
|
for rows.Next() {
|
2015-11-14 18:10:21 +08:00
|
|
|
err := rows.Scan(&TaskId, &TaskTitle, &TaskContent, &TaskCreated)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2015-11-14 18:10:21 +08:00
|
|
|
TaskCreated = TaskCreated.Local()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
2015-11-13 21:11:30 +08:00
|
|
|
func GetTaskById(id int) types.Task {
|
2015-11-13 17:04:42 +08:00
|
|
|
var task types.Task
|
|
|
|
var TaskId int
|
|
|
|
var TaskTitle string
|
|
|
|
var TaskContent string
|
|
|
|
getTasksql := "select id, title, content from task where id=?"
|
|
|
|
|
|
|
|
rows, err := database.Query(getTasksql, id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2015-11-13 21:11:30 +08:00
|
|
|
if rows.Next() {
|
2015-11-13 17:04:42 +08:00
|
|
|
err := rows.Scan(&TaskId, &TaskTitle, &TaskContent)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2015-11-13 21:11:30 +08:00
|
|
|
task = types.Task{Id: TaskId, Title: TaskTitle, Content: TaskContent}
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:56:53 +08:00
|
|
|
func TrashTask(id int) error {
|
|
|
|
trashSql, err := database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
tx, err := database.Begin()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
_, err = tx.Stmt(trashSql).Exec(id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("doing rollback")
|
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompleteTask(id int) error {
|
2015-11-13 17:04:42 +08:00
|
|
|
stmt, err := database.Prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?")
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
tx, err := database.Begin()
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
_, err = tx.Stmt(stmt).Exec(id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteAll() error {
|
|
|
|
stmt, err := database.Prepare("delete from task where is_deleted='Y'")
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
tx, err := database.Begin()
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
_, err = tx.Stmt(stmt).Exec()
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println("doing rollback")
|
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func RestoreTask(id int) error {
|
|
|
|
restoreSql, err := database.Prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?")
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
tx, err := database.Begin()
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
_, err = tx.Stmt(restoreSql).Exec(id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println("doing rollback")
|
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteTask(id int) error {
|
|
|
|
deleteSQL, err := database.Prepare("delete from task where id = ?")
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
tx, err := database.Begin()
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
_, err = tx.Stmt(deleteSQL).Exec(id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddTask(title, content string) error {
|
|
|
|
restoreSql, err := database.Prepare("insert into task(title, content, created_date, last_modified_at) values(?,?,datetime(), datetime())")
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
tx, err := database.Begin()
|
|
|
|
_, err = tx.Stmt(restoreSql).Exec(title, content)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
tx.Commit()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-13 21:11:30 +08:00
|
|
|
func UpdateTask(id int, title string, content string) error {
|
2015-11-13 17:04:42 +08:00
|
|
|
Sql, err := database.Prepare("update task set title=?, content=? where id=?")
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
tx, err := database.Begin()
|
|
|
|
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
_, err = tx.Stmt(Sql).Exec(title, content, id)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
tx.Rollback()
|
|
|
|
} else {
|
|
|
|
fmt.Println(tx.Commit())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-13 21:11:30 +08:00
|
|
|
func SearchTask(query string) []types.Task {
|
|
|
|
stmt := "select id, title, content from task where title like '%" + query + "%' or content like '%" + query + "%'"
|
2015-11-13 17:04:42 +08:00
|
|
|
var task []types.Task
|
|
|
|
var TaskId int
|
|
|
|
var TaskTitle string
|
|
|
|
var TaskContent string
|
|
|
|
|
|
|
|
rows, err := database.Query(stmt, query, query)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2015-11-13 21:11:30 +08:00
|
|
|
for rows.Next() {
|
2015-11-13 17:04:42 +08:00
|
|
|
err := rows.Scan(&TaskId, &TaskTitle, &TaskContent)
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
TaskTitle = strings.Replace(TaskTitle, query, "<span class='highlight'>"+query+"</span>", -1)
|
|
|
|
TaskContent = strings.Replace(TaskContent, query, "<span class='highlight'>"+query+"</span>", -1)
|
2015-11-13 21:11:30 +08:00
|
|
|
a := types.Task{Id: TaskId, Title: TaskTitle, Content: TaskContent}
|
2015-11-13 17:04:42 +08:00
|
|
|
task = append(task, a)
|
|
|
|
}
|
|
|
|
return task
|
|
|
|
}
|