2015-11-13 14:34:42 +05:30
package db
2016-02-01 20:04:19 +05:30
/ *
Stores the database functions related to tasks like
GetTaskByID ( id int )
GetTasks ( status string )
DeleteAll ( )
* /
2015-11-13 14:34:42 +05:30
import (
"database/sql"
2016-01-31 19:52:00 +05:30
"log"
"strings"
"time"
2016-02-01 19:15:04 +05:30
_ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
md "github.com/shurcooL/github_flavored_markdown"
"github.com/thewhitetulip/Tasks/types"
2015-11-13 14:34:42 +05:30
)
2016-01-31 14:48:19 +01:00
var database Database
2016-05-14 12:56:24 +05:30
var taskStatus map [ string ] int
2015-11-13 14:34:42 +05:30
var err error
2016-01-31 14:48:19 +01:00
//Database encapsulates database
type Database struct {
db * sql . DB
}
2016-05-14 12:56:24 +05:30
//Begins a transaction
2016-01-31 14:48:19 +01:00
func ( db Database ) begin ( ) ( tx * sql . Tx ) {
tx , err := db . db . Begin ( )
if err != nil {
log . Println ( err )
2016-02-01 19:15:04 +05:30
return nil
2016-01-31 14:48:19 +01:00
}
return tx
}
func ( db Database ) prepare ( q string ) ( stmt * sql . Stmt ) {
stmt , err := db . db . Prepare ( q )
if err != nil {
log . Println ( err )
2016-02-01 19:15:04 +05:30
return nil
2016-01-31 14:48:19 +01:00
}
return stmt
}
func ( db Database ) query ( q string , args ... interface { } ) ( rows * sql . Rows ) {
2016-01-31 15:00:31 +01:00
rows , err := db . db . Query ( q , args ... )
2016-01-31 14:48:19 +01:00
if err != nil {
log . Println ( err )
2016-02-01 19:15:04 +05:30
return nil
2016-01-31 14:48:19 +01:00
}
return rows
}
2015-11-13 14:34:42 +05:30
func init ( ) {
2016-01-31 14:48:19 +01:00
database . db , err = sql . Open ( "sqlite3" , "./tasks.db" )
2016-05-14 16:07:20 +05:30
taskStatus = map [ string ] int { "COMPLETE" : 1 , "PENDING" : 2 , "DELETED" : 3 }
2015-11-13 14:34:42 +05:30
if err != nil {
2016-02-01 19:15:04 +05:30
log . Fatal ( err )
2015-11-13 14:34:42 +05:30
}
}
2015-11-21 12:09:15 +05:30
//Close function closes this database connection
2015-11-13 14:34:42 +05:30
func Close ( ) {
2016-01-31 14:48:19 +01:00
database . db . Close ( )
2015-11-13 14:34:42 +05:30
}
2015-11-21 12:09:15 +05:30
//GetTasks retrieves all the tasks depending on the
//status pending or trashed or completed
2016-05-14 12:56:24 +05:30
func GetTasks ( username , status , category string ) ( types . Context , error ) {
log . Println ( "getting tasks for " , status )
2016-02-14 23:16:22 +05:30
var tasks [ ] types . Task
var task types . Task
2015-11-14 15:40:21 +05:30
var TaskCreated time . Time
2016-02-14 23:16:22 +05:30
var context types . Context
2016-05-14 12:56:24 +05:30
var getTaskSQL string
2016-02-02 23:10:44 +05:30
var rows * sql . Rows
2015-11-21 18:50:51 +05:30
2016-05-14 12:56:24 +05:30
comments , err := GetComments ( username )
2016-02-16 22:23:03 +05:30
if err != nil {
return context , err
}
2016-02-14 23:16:22 +05:30
2016-05-14 12:56:24 +05:30
basicSQL := "select t.id, title, content, created_date, priority, c.name from task t, category c, status s, user u where u.username=? and s.id=t.task_status_id and c.id=t.cat_id and u.id=t.user_id"
if category == "" {
switch status {
case "pending" :
2016-05-14 16:07:20 +05:30
getTaskSQL = basicSQL + " and s.status='PENDING'"
2016-05-14 12:56:24 +05:30
case "deleted" :
getTaskSQL = basicSQL + " and s.status='DELETED' "
case "completed" :
getTaskSQL = basicSQL + " and s.status='COMPLETE'"
}
2015-11-13 14:34:42 +05:30
2016-05-14 12:56:24 +05:30
basicSQL += " order by priority desc, created_date asc"
rows = database . query ( getTaskSQL , username )
} else {
2016-02-02 23:10:44 +05:30
status = category
2016-05-14 19:54:17 +05:30
getTaskSQL = basicSQL + " and name = ? and s.status='PENDING' order by priority desc, created_date asc, finish_date asc"
2016-05-14 12:56:24 +05:30
rows , err = database . db . Query ( getTaskSQL , username , category )
log . Print ( getTaskSQL )
2016-02-29 07:35:38 +05:30
2016-02-02 23:10:44 +05:30
if err != nil {
2016-05-14 12:56:24 +05:30
log . Println ( "tasks.go: something went wrong while getting query fetch tasks by category" )
2016-02-02 23:10:44 +05:30
}
}
2016-05-14 12:56:24 +05:30
2015-11-13 14:34:42 +05:30
defer rows . Close ( )
2015-11-13 18:41:30 +05:30
for rows . Next ( ) {
2016-02-14 23:16:22 +05:30
task = types . Task { }
2016-02-29 07:35:38 +05:30
err = rows . Scan ( & task . Id , & task . Title , & task . Content , & TaskCreated , & task . Priority , & task . Category )
2016-02-14 23:16:22 +05:30
task . Content = string ( md . Markdown ( [ ] byte ( task . Content ) ) )
2016-01-31 18:00:10 +05:30
// TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1)
2015-11-13 18:41:30 +05:30
if err != nil {
2016-01-31 13:16:44 +01:00
log . Println ( err )
2015-11-13 14:34:42 +05:30
}
2016-02-14 23:16:22 +05:30
if comments [ task . Id ] != nil {
task . Comments = comments [ task . Id ]
}
2015-11-14 15:40:21 +05:30
TaskCreated = TaskCreated . Local ( )
2016-03-01 00:09:48 +05:30
if task . Priority != "1" { // if priority is not 1 then calculate, else why bother?
CurrentTime := time . Now ( ) . Local ( )
diff := CurrentTime . Sub ( TaskCreated ) . Hours ( )
if diff > 168 {
task . IsOverdue = true // If one week then overdue by default
}
2016-02-29 23:56:41 +05:30
}
2016-03-01 00:17:14 +05:30
task . Created = TaskCreated . Format ( "Jan 2 2006" )
2016-02-14 23:16:22 +05:30
tasks = append ( tasks , task )
2015-11-13 14:34:42 +05:30
}
2016-02-14 23:16:22 +05:30
context = types . Context { Tasks : tasks , Navigation : status }
2016-02-01 19:15:04 +05:30
return context , nil
2015-11-13 14:34:42 +05:30
}
2015-11-22 09:21:29 +05:30
//GetTaskByID function gets the tasks from the ID passed to the function, used to populate EditTask
2016-05-14 12:56:24 +05:30
func GetTaskByID ( username string , id int ) ( types . Context , error ) {
2015-11-22 08:29:39 +05:30
var tasks [ ] types . Task
2015-11-13 14:34:42 +05:30
var task types . Task
2016-01-23 19:17:34 +05:30
2016-05-14 12:56:24 +05:30
getTaskSQL := "select t.id, t.title, t.content, t.priority, c.name from task t join user u left outer join category c where c.id = t.cat_id and t.id=? and u.username=?"
2016-01-31 18:00:10 +05:30
2016-05-14 12:56:24 +05:30
rows := database . query ( getTaskSQL , id , username )
2015-11-13 14:34:42 +05:30
defer rows . Close ( )
2015-11-13 18:41:30 +05:30
if rows . Next ( ) {
2016-02-05 00:06:00 +05:30
err := rows . Scan ( & task . Id , & task . Title , & task . Content , & task . Priority , & task . Category )
2015-11-13 18:41:30 +05:30
if err != nil {
2016-01-31 13:16:44 +01:00
log . Println ( err )
2016-01-31 18:00:10 +05:30
//send email to respective people
2015-11-13 14:34:42 +05:30
}
}
2015-11-22 08:29:39 +05:30
tasks = append ( tasks , task )
context := types . Context { Tasks : tasks , Navigation : "edit" }
2016-02-01 19:15:04 +05:30
return context , nil
2015-11-13 14:34:42 +05:30
}
2015-11-21 12:09:15 +05:30
//TrashTask is used to delete the task
2016-05-14 12:56:24 +05:30
func TrashTask ( username string , id int ) error {
err := taskQuery ( "update task set task_status_id=?,last_modified_at=datetime() where user_id=(select id from user where username=?) and id=?" , taskStatus [ "DELETED" ] , username , id )
2015-11-14 16:26:53 +05:30
return err
}
2015-11-21 12:09:15 +05:30
//CompleteTask is used to mark tasks as complete
2016-05-14 12:56:24 +05:30
func CompleteTask ( username string , id int ) error {
err := taskQuery ( "update task set task_status_id=?, finish_date=datetime(),last_modified_at=datetime() where id=? and user_id=(select id from user where username=?) " , taskStatus [ "COMPLETE" ] , id , username )
2015-11-13 14:34:42 +05:30
return err
}
2015-11-21 12:09:15 +05:30
//DeleteAll is used to empty the trash
2016-05-14 12:56:24 +05:30
func DeleteAll ( username string ) error {
err := taskQuery ( "delete from task where task_status_id=? where user_id=(select id from user where username=?)" , taskStatus [ "DELETED" ] , username )
2015-11-13 14:34:42 +05:30
return err
}
2015-11-21 12:09:15 +05:30
//RestoreTask is used to restore tasks from the Trash
2016-05-14 12:56:24 +05:30
func RestoreTask ( username string , id int ) error {
2016-05-14 16:07:20 +05:30
err := taskQuery ( "update task set task_status_id=?,last_modified_at=datetime(),finish_date=null where id=? and user_id=(select id from user where username=?)" , taskStatus [ "PENDING" ] , id , username )
2016-01-31 18:00:10 +05:30
return err
}
2016-01-31 14:48:19 +01:00
//RestoreTaskFromComplete is used to restore tasks from the Trash
2016-05-14 12:56:24 +05:30
func RestoreTaskFromComplete ( username string , id int ) error {
2016-05-14 16:07:20 +05:30
err := taskQuery ( "update task set finish_date=null,last_modified_at=datetime(), task_status_id=? where id=? and user_id=(select id from user where username=?)" , taskStatus [ "PENDING" ] , id , username )
2016-01-09 10:33:35 +05:30
return err
}
2015-11-21 12:09:15 +05:30
//DeleteTask is used to delete the task from the database
2016-05-14 12:56:24 +05:30
func DeleteTask ( username string , id int ) error {
err := taskQuery ( "delete from task where id = ? and user_id=(select id from user where username=?)" , id , username )
2015-11-13 14:34:42 +05:30
return err
}
2015-11-21 12:09:15 +05:30
//AddTask is used to add the task in the database
2016-05-14 12:56:24 +05:30
func AddTask ( title , content , category string , taskPriority int , username string ) error {
log . Println ( "AddTask: started function" )
2016-02-02 23:10:44 +05:30
var err error
2016-05-14 12:56:24 +05:30
userID , err := GetUserID ( username )
if err != nil {
return err
}
2016-02-02 23:10:44 +05:30
if category == "" {
2016-05-14 16:07:20 +05:30
err = taskQuery ( "insert into task(title, content, priority, task_status_id, created_date, last_modified_at, user_id) values(?,?,?,?,datetime(), datetime(),?)" , title , content , taskPriority , taskStatus [ "PENDING" ] , userID )
2016-02-02 23:10:44 +05:30
} else {
2016-05-14 12:56:24 +05:30
categoryID := GetCategoryByName ( username , category )
2016-05-14 16:07:20 +05:30
err = taskQuery ( "insert into task(title, content, priority, created_date, last_modified_at, cat_id, task_status_id, user_id) values(?,?,?,datetime(), datetime(), ?,?,?)" , title , content , taskPriority , categoryID , taskStatus [ "PENDING" ] , userID )
2016-02-02 23:10:44 +05:30
}
2015-11-13 14:34:42 +05:30
return err
}
2016-05-14 12:56:24 +05:30
//GetCategoryIDByName will return the category ID for the category, used in the edit task
2016-02-05 23:39:09 +05:30
//function where we need to be able to update the categoryID of the task
2016-05-14 12:56:24 +05:30
func GetCategoryIDByName ( username string , category string ) int {
2016-02-05 23:39:09 +05:30
var categoryID int
2016-05-14 12:56:24 +05:30
getTaskSQL := "select c.id from category c , user u where u.id = c.user_id and name=? and u.username=?"
2016-02-05 23:39:09 +05:30
2016-05-14 12:56:24 +05:30
rows := database . query ( getTaskSQL , category , username )
2016-02-05 23:39:09 +05:30
defer rows . Close ( )
if rows . Next ( ) {
err := rows . Scan ( & categoryID )
if err != nil {
log . Println ( err )
//send email to respective people
}
}
return categoryID
}
2015-11-21 12:09:15 +05:30
//UpdateTask is used to update the tasks in the database
2016-05-14 12:56:24 +05:30
func UpdateTask ( id int , title , content , category string , priority int , username string ) error {
categoryID := GetCategoryIDByName ( username , category )
userID , err := GetUserID ( username )
if err != nil {
return err
}
err = taskQuery ( "update task set title=?, content=?, cat_id=?, priority = ? where id=? and user_id=?" , title , content , categoryID , priority , id , userID )
2016-01-31 14:56:14 +01:00
return err
}
2016-01-31 19:52:00 +05:30
//taskQuery encapsulates running multiple queries which don't do much things
2016-01-31 14:56:14 +01:00
func taskQuery ( sql string , args ... interface { } ) error {
2016-05-14 15:04:42 +05:30
log . Print ( "inside task query" )
2016-01-31 19:40:15 +05:30
SQL := database . prepare ( sql )
2016-01-31 14:48:19 +01:00
tx := database . begin ( )
2016-01-31 15:00:31 +01:00
_ , err = tx . Stmt ( SQL ) . Exec ( args ... )
2015-11-13 18:41:30 +05:30
if err != nil {
2016-05-14 12:56:24 +05:30
log . Println ( "taskQuery: " , err )
2015-11-13 14:34:42 +05:30
tx . Rollback ( )
} else {
2016-05-14 15:04:42 +05:30
err = tx . Commit ( )
if err != nil {
log . Println ( err )
return err
}
log . Println ( "Commit successful" )
2015-11-13 14:34:42 +05:30
}
return err
}
2015-11-21 12:09:15 +05:30
//SearchTask is used to return the search results depending on the query
2016-05-14 12:56:24 +05:30
func SearchTask ( username , query string ) ( types . Context , error ) {
2016-02-29 07:35:38 +05:30
var tasks [ ] types . Task
var task types . Task
2015-11-21 16:33:34 +05:30
var TaskCreated time . Time
2015-11-21 19:42:20 +05:30
var context types . Context
2015-11-13 14:34:42 +05:30
2016-05-14 12:56:24 +05:30
comments , err := GetComments ( username )
2016-02-29 07:35:38 +05:30
if err != nil {
log . Println ( "SearchTask: something went wrong in finding comments" )
}
2016-05-14 12:56:24 +05:30
userID , err := GetUserID ( username )
if err != nil {
return context , err
}
stmt := "select t.id, title, content, created_date, priority, c.name from task t, category c where t.user_id=? and c.id = t.cat_id and (title like '%" + query + "%' or content like '%" + query + "%') order by created_date desc"
2016-02-29 07:35:38 +05:30
2016-05-14 12:56:24 +05:30
rows := database . query ( stmt , userID , query , query )
2016-05-14 15:04:42 +05:30
defer rows . Close ( )
2015-11-13 18:41:30 +05:30
for rows . Next ( ) {
2016-02-29 07:35:38 +05:30
err := rows . Scan ( & task . Id , & task . Title , & task . Content , & TaskCreated , & task . Priority , & task . Category )
2015-11-13 18:41:30 +05:30
if err != nil {
2016-01-31 13:16:44 +01:00
log . Println ( err )
2015-11-13 14:34:42 +05:30
}
2016-02-29 07:35:38 +05:30
if comments [ task . Id ] != nil {
task . Comments = comments [ task . Id ]
}
task . Title = strings . Replace ( task . Title , query , "<span class='highlight'>" + query + "</span>" , - 1 )
task . Content = strings . Replace ( task . Content , query , "<span class='highlight'>" + query + "</span>" , - 1 )
task . Content = string ( md . Markdown ( [ ] byte ( task . Content ) ) )
TaskCreated = TaskCreated . Local ( )
2016-02-29 23:56:41 +05:30
CurrentTime := time . Now ( ) . Local ( )
week := TaskCreated . AddDate ( 0 , 0 , 7 )
if ( week . String ( ) < CurrentTime . String ( ) ) && ( task . Priority != "1" ) {
task . IsOverdue = true // If one week then overdue by default
}
2016-03-01 00:17:14 +05:30
task . Created = TaskCreated . Format ( "Jan 2 2006" )
2016-02-29 07:35:38 +05:30
tasks = append ( tasks , task )
2015-11-13 14:34:42 +05:30
}
2016-02-29 07:35:38 +05:30
context = types . Context { Tasks : tasks , Search : query , Navigation : "search" }
2016-05-14 12:56:24 +05:30
return context , nil
2015-11-13 14:34:42 +05:30
}
2016-02-14 23:16:22 +05:30
//GetComments is used to get comments, all of them.
//We do not want 100 different pages to show tasks, we want to use as few pages as possible
//so we are going to populate everything on the damn home pages
2016-05-14 12:56:24 +05:30
func GetComments ( username string ) ( map [ int ] [ ] types . Comment , error ) {
2016-02-14 23:16:22 +05:30
commentMap := make ( map [ int ] [ ] types . Comment )
2016-02-16 22:23:03 +05:30
var taskID int
var comment types . Comment
var created time . Time
2016-02-14 23:16:22 +05:30
2016-05-14 12:56:24 +05:30
userID , err := GetUserID ( username )
if err != nil {
return commentMap , err
}
2016-05-15 18:00:19 +05:30
stmt := "select c.id, c.taskID, c.content, c.created, u.username from comments c, task t, user u where t.id=c.taskID and c.user_id=t.user_id and t.user_id=u.id and u.id=?"
2016-05-14 12:56:24 +05:30
rows := database . query ( stmt , userID )
2016-02-14 23:16:22 +05:30
2016-05-14 15:04:42 +05:30
defer rows . Close ( )
2016-02-14 23:16:22 +05:30
for rows . Next ( ) {
2016-05-15 18:00:19 +05:30
err := rows . Scan ( & comment . ID , & taskID , & comment . Content , & created , & comment . Username )
2016-02-14 23:16:22 +05:30
if err != nil {
2016-02-16 22:23:03 +05:30
return commentMap , err
2016-02-14 23:16:22 +05:30
}
2016-02-16 22:23:03 +05:30
// comment.Content = string(md.Markdown([]byte(comment.Content))) ## have to fix the <p> issue markdown support
created = created . Local ( )
2016-03-01 00:17:14 +05:30
comment . Created = created . Format ( "Jan 2 2006 15:04:05" )
2016-02-16 22:23:03 +05:30
commentMap [ taskID ] = append ( commentMap [ taskID ] , comment )
2016-02-14 23:16:22 +05:30
}
2016-02-16 22:23:03 +05:30
return commentMap , nil
2016-02-14 23:16:22 +05:30
}
//AddComments will be used to add comments in the database
2016-05-14 12:56:24 +05:30
func AddComments ( username string , id int , comment string ) error {
userID , err := GetUserID ( username )
if err != nil {
return err
}
stmt := "insert into comments(taskID, content, created, user_id) values (?,?,datetime(),?)"
err = taskQuery ( stmt , id , comment , userID )
2016-02-14 23:16:22 +05:30
if err != nil {
return err
}
log . Println ( "added comment to task ID " , id )
return nil
}