2016-01-31 11:35:46 +05:30
package db
2016-02-01 20:04:19 +05:30
/ *
2016-02-06 12:28:00 +05:30
stores the functions related to file IO and category
2016-02-01 20:04:19 +05:30
* /
2016-01-31 11:35:46 +05:30
import (
"log"
2016-02-05 01:09:36 +05:30
"github.com/thewhitetulip/Tasks/types"
2016-01-31 11:35:46 +05:30
)
// 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
2016-05-15 22:23:24 +01:00
func AddFile ( fileName , token , username string ) error {
2016-05-17 09:42:39 +01:00
userID , err := GetUserID ( username )
if err != nil {
return err
}
err = taskQuery ( "insert into files values(?,?,?,datetime())" , fileName , token , userID )
2016-01-31 11:35:46 +05:30
return err
}
// GetFileName is used to fetch the name according to the md5 checksum from the db
func GetFileName ( token string ) ( string , error ) {
sql := "select name from files where autoName=?"
var fileName string
2016-01-31 14:48:19 +01:00
rows := database . query ( sql , fileName )
2016-05-14 15:04:42 +05:30
defer rows . Close ( )
2016-01-31 11:35:46 +05:30
if rows . Next ( ) {
err := rows . Scan ( & fileName )
if err != nil {
log . Println ( err )
return "" , err
}
}
2016-01-31 18:00:10 +05:30
if err != nil {
return "" , err
}
2016-01-31 11:35:46 +05:30
return fileName , nil
}
2016-02-02 23:10:44 +05:30
//GetCategories will return the list of categories to be
//rendered in the template
2016-05-14 12:56:24 +05:30
func GetCategories ( username string ) [ ] types . CategoryCount {
userID , err := GetUserID ( username )
if err != nil {
return nil
}
2016-05-16 20:57:09 +05:30
stmt := "select 'UNCATEGORIZED' as name, count(1) from task where cat_id=0 union select c.name, count(*) from category c left outer join task t join status s on c.id = t.cat_id and t.task_status_id=s.id where s.status!='DELETED' and c.user_id=? group by name union select name, 0 from category c, user u where c.user_id=? and name not in (select distinct name from task t join category c join status s on s.id = t.task_status_id and t.cat_id = c.id and s.status!='DELETED' and c.user_id=?)"
2016-05-14 12:56:24 +05:30
rows := database . query ( stmt , userID , userID , userID )
2016-02-05 01:09:36 +05:30
var categories [ ] types . CategoryCount
var category types . CategoryCount
2016-02-02 23:10:44 +05:30
2016-05-14 15:04:42 +05:30
defer rows . Close ( )
2016-02-02 23:10:44 +05:30
for rows . Next ( ) {
2016-02-05 01:09:36 +05:30
err := rows . Scan ( & category . Name , & category . Count )
2016-02-02 23:10:44 +05:30
if err != nil {
log . Println ( err )
}
categories = append ( categories , category )
}
return categories
}
//AddCategory is used to add the task in the database
2016-05-14 12:56:24 +05:30
func AddCategory ( username , category string ) error {
userID , err := GetUserID ( username )
if err != nil {
return nil
}
2016-05-14 15:04:42 +05:30
log . Println ( "executing query to add category" )
2016-05-14 12:56:24 +05:30
err = taskQuery ( "insert into category(name, user_id) values(?,?)" , category , userID )
2016-02-02 23:10:44 +05:30
return err
}
2016-02-06 12:28:00 +05:30
// GetCategoryByName will return the ID of that category passed as args
2016-02-02 23:10:44 +05:30
// used while inserting tasks into the table
2016-05-14 12:56:24 +05:30
func GetCategoryByName ( username , category string ) int {
stmt := "select id from category where name=? and user_id = (select id from user where username=?)"
rows := database . query ( stmt , category , username )
2016-02-02 23:10:44 +05:30
var categoryID int
2016-05-14 15:04:42 +05:30
defer rows . Close ( )
2016-02-02 23:10:44 +05:30
for rows . Next ( ) {
err := rows . Scan ( & categoryID )
if err != nil {
log . Println ( err )
}
}
return categoryID
}
2016-02-06 12:28:00 +05:30
//DeleteCategoryByName will be used to delete a category from the category page
2016-05-14 12:56:24 +05:30
func DeleteCategoryByName ( username , category string ) error {
2016-02-06 12:28:00 +05:30
//first we delete entries from task and then from category
2016-05-14 12:56:24 +05:30
categoryID := GetCategoryByName ( username , category )
userID , err := GetUserID ( username )
if err != nil {
return err
}
query := "update task set cat_id = null where id =? and user_id = ?"
err = taskQuery ( query , categoryID , userID )
2016-02-06 12:28:00 +05:30
if err == nil {
2016-05-14 12:56:24 +05:30
err = taskQuery ( "delete from category where id=? and user_id=?" , categoryID , userID )
2016-02-06 12:28:00 +05:30
if err != nil {
return err
}
}
return err
}
//UpdateCategoryByName will be used to delete a category from the category page
2016-05-14 12:56:24 +05:30
func UpdateCategoryByName ( username , oldName , newName string ) error {
userID , err := GetUserID ( username )
if err != nil {
return err
}
query := "update category set name = ? where name=? and user_id=?"
2016-02-06 12:28:00 +05:30
log . Println ( query )
2016-05-14 12:56:24 +05:30
err = taskQuery ( query , newName , oldName , userID )
2016-02-06 12:28:00 +05:30
return err
2016-02-16 22:23:03 +05:30
}
2016-02-06 12:28:00 +05:30
2016-02-16 22:23:03 +05:30
//DeleteCommentByID will actually delete the comment from db
2016-05-14 12:56:24 +05:30
func DeleteCommentByID ( username string , id int ) error {
userID , err := GetUserID ( username )
if err != nil {
return err
}
query := "delete from comments where id=? and user_id = ?"
err = taskQuery ( query , id , userID )
2016-02-16 22:23:03 +05:30
return err
2016-02-06 12:28:00 +05:30
}