diff --git a/db/files.go b/db/files.go
index 9e57bd8..e31272a 100644
--- a/db/files.go
+++ b/db/files.go
@@ -7,15 +7,9 @@ import (
// AddFile is used to add the md5 of a file name which is uploaded to our application
// this will enable us to randomize the URL without worrying about the file names
func AddFile(fileName, token string) error {
- SQL, err := database.Prepare("insert into files values(?,?)")
- if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
+ SQL := database.prepare("insert into files values(?,?)")
- if err != nil {
- log.Println(err)
- }
+ tx := database.begin()
_, err = tx.Stmt(SQL).Exec(fileName, token)
if err != nil {
log.Println(err)
@@ -30,7 +24,7 @@ func AddFile(fileName, token string) error {
func GetFileName(token string) (string, error) {
sql := "select name from files where autoName=?"
var fileName string
- rows, err := database.Query(sql, fileName)
+ rows := database.query(sql, fileName)
if rows.Next() {
err := rows.Scan(&fileName)
if err != nil {
@@ -38,9 +32,6 @@ func GetFileName(token string) (string, error) {
return "", err
}
}
- if err != nil {
- return "", err
- }
return fileName, nil
}
diff --git a/db/tasks.go b/db/tasks.go
index 03d3767..7f85ecd 100644
--- a/db/tasks.go
+++ b/db/tasks.go
@@ -2,27 +2,57 @@ package db
import (
"database/sql"
- _ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
- md "github.com/shurcooL/github_flavored_markdown"
- "github.com/thewhitetulip/Tasks/types"
- "log"
+ "fmt"
"strings"
"time"
+
+ _ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
+ "github.com/thewhitetulip/Tasks/types"
)
-var database *sql.DB
+var database Database
var err error
-func init() {
- database, err = sql.Open("sqlite3", "./tasks.db")
+//Database encapsulates database
+type Database struct {
+ db *sql.DB
+}
+
+func (db Database) begin() (tx *sql.Tx) {
+ tx, err := db.db.Begin()
if err != nil {
- log.Println(err)
+ 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
+}
+
+func init() {
+ database = Database{}
+ database.db, err = sql.Open("sqlite3", "./tasks.db")
+ if err != nil {
+ fmt.Println(err)
}
}
//Close function closes this database connection
func Close() {
- database.Close()
+ database.db.Close()
}
//GetTasks retrieves all the tasks depending on the
@@ -34,32 +64,26 @@ func GetTasks(status string) types.Context {
var TaskTitle string
var TaskContent string
var TaskCreated time.Time
- var TaskPriority string
var getTasksql string
- basicSQL := "select id, title, content, created_date, priority from task "
if status == "pending" {
- getTasksql = basicSQL + " where finish_date is null and is_deleted='N' order by priority desc, created_date asc"
+ 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 == "deleted" {
- getTasksql = basicSQL + " where is_deleted='Y' order by priority desc, created_date asc"
+ getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc"
} else if status == "completed" {
- getTasksql = basicSQL + " where finish_date is not null order by priority desc, created_date asc"
+ getTasksql = "select id, title, content, created_date from task where finish_date is not null order by created_date asc"
}
- rows, err := database.Query(getTasksql)
- if err != nil {
- log.Println(err)
- }
+ rows := database.query(getTasksql)
defer rows.Close()
for rows.Next() {
- err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated, &TaskPriority)
- TaskContent = string(md.Markdown([]byte(TaskContent)))
- // TaskContent = strings.Replace(TaskContent, "\n", "
", -1)
+ err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated)
+ TaskContent = strings.Replace(TaskContent, "\n", "
", -1)
if err != nil {
- log.Println(err)
+ fmt.Println(err)
}
TaskCreated = TaskCreated.Local()
- a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20], Priority: TaskPriority}
+ a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]}
task = append(task, a)
}
context = types.Context{Tasks: task, Navigation: status}
@@ -70,20 +94,19 @@ func GetTasks(status string) types.Context {
func GetTaskByID(id int) types.Context {
var tasks []types.Task
var task types.Task
+ var TaskID int
+ var TaskTitle string
+ var TaskContent string
+ getTasksql := "select id, title, content from task where id=?"
- getTasksql := "select id, title, content, priority from task where id=?"
-
- rows, err := database.Query(getTasksql, id)
- if err != nil {
- log.Println(err)
- }
+ rows := database.query(getTasksql, id)
defer rows.Close()
if rows.Next() {
- err := rows.Scan(&task.Id, &task.Title, &task.Content, &task.Priority)
+ err := rows.Scan(&TaskID, &TaskTitle, &TaskContent)
if err != nil {
- log.Println(err)
- //send email to respective people
+ fmt.Println(err)
}
+ task = types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent}
}
tasks = append(tasks, task)
context := types.Context{Tasks: tasks, Navigation: "edit"}
@@ -92,17 +115,11 @@ func GetTaskByID(id int) types.Context {
//TrashTask is used to delete the task
func TrashTask(id int) error {
- trashSQL, err := database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
- if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
- if err != nil {
- log.Println(err)
- }
+ trashSQL := database.prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
+ tx := database.begin()
_, err = tx.Stmt(trashSQL).Exec(id)
if err != nil {
- log.Println("doing rollback")
+ fmt.Println("doing rollback")
tx.Rollback()
} else {
tx.Commit()
@@ -112,17 +129,11 @@ func TrashTask(id int) error {
//CompleteTask is used to mark tasks as complete
func CompleteTask(id int) error {
- stmt, err := database.Prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?")
- if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
- if err != nil {
- log.Println(err)
- }
+ stmt := database.prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?")
+ tx := database.begin()
_, err = tx.Stmt(stmt).Exec(id)
if err != nil {
- log.Println(err)
+ fmt.Println(err)
tx.Rollback()
} else {
tx.Commit()
@@ -132,17 +143,11 @@ func CompleteTask(id int) error {
//DeleteAll is used to empty the trash
func DeleteAll() error {
- stmt, err := database.Prepare("delete from task where is_deleted='Y'")
- if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
- if err != nil {
- log.Println(err)
- }
+ stmt := database.prepare("delete from task where is_deleted='Y'")
+ tx := database.begin()
_, err = tx.Stmt(stmt).Exec()
if err != nil {
- log.Println("doing rollback")
+ fmt.Println("doing rollback")
tx.Rollback()
} else {
tx.Commit()
@@ -152,37 +157,11 @@ func DeleteAll() error {
//RestoreTask is used to restore tasks from the Trash
func RestoreTask(id int) error {
- restoreSQL, err := database.Prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?")
- if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
- if err != nil {
- log.Println(err)
- }
+ restoreSQL := database.prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?")
+ tx := database.begin()
_, err = tx.Stmt(restoreSQL).Exec(id)
if err != nil {
- log.Println("doing rollback")
- tx.Rollback()
- } else {
- tx.Commit()
- }
- return err
-}
-
-//RestoreTask is used to restore tasks from the Trash
-func RestoreTaskFromComplete(id int) error {
- restoreSQL, err := database.Prepare("update task set finish_date=null,last_modified_at=datetime() where id=?")
- if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
- if err != nil {
- log.Println(err)
- }
- _, err = tx.Stmt(restoreSQL).Exec(id)
- if err != nil {
- log.Println("doing rollback")
+ fmt.Println("doing rollback")
tx.Rollback()
} else {
tx.Commit()
@@ -192,17 +171,14 @@ func RestoreTaskFromComplete(id int) error {
//DeleteTask is used to delete the task from the database
func DeleteTask(id int) error {
- deleteSQL, err := database.Prepare("delete from task where id = ?")
+ deleteSQL := database.prepare("delete from task where id = ?")
+ tx := database.begin()
if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
- if err != nil {
- log.Println(err)
+ fmt.Println(err)
}
_, err = tx.Stmt(deleteSQL).Exec(id)
if err != nil {
- log.Println(err)
+ fmt.Println(err)
tx.Rollback()
} else {
tx.Commit()
@@ -211,15 +187,12 @@ func DeleteTask(id int) error {
}
//AddTask is used to add the task in the database
-func AddTask(title, content string, taskPriority int) error {
- restoreSQL, err := database.Prepare("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())")
+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)
if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
- _, err = tx.Stmt(restoreSQL).Exec(title, content, taskPriority)
- if err != nil {
- log.Println(err)
+ fmt.Println(err)
tx.Rollback()
} else {
tx.Commit()
@@ -229,21 +202,14 @@ func AddTask(title, content string, taskPriority int) error {
//UpdateTask is used to update the tasks in the database
func UpdateTask(id int, title string, content string) error {
- SQL, err := database.Prepare("update task set title=?, content=? where id=?")
- if err != nil {
- log.Println(err)
- }
- tx, err := database.Begin()
-
- if err != nil {
- log.Println(err)
- }
+ SQL := database.prepare("update task set title=?, content=? where id=?")
+ tx := database.begin()
_, err = tx.Stmt(SQL).Exec(title, content, id)
if err != nil {
- log.Println(err)
+ fmt.Println(err)
tx.Rollback()
} else {
- log.Println(tx.Commit())
+ fmt.Println(tx.Commit())
}
return err
}
@@ -258,18 +224,15 @@ func SearchTask(query string) types.Context {
var TaskCreated time.Time
var context types.Context
- rows, err := database.Query(stmt, query, query)
- if err != nil {
- log.Println(err)
- }
+ rows := database.query(stmt, query, query)
+
for rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated)
if err != nil {
- log.Println(err)
+ fmt.Println(err)
}
TaskTitle = strings.Replace(TaskTitle, query, ""+query+"", -1)
TaskContent = strings.Replace(TaskContent, query, ""+query+"", -1)
- TaskContent = string(md.Markdown([]byte(TaskContent)))
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]}
task = append(task, a)
}