TaskFlow/db/files.go

130 lines
3.8 KiB
Go
Raw Normal View History

2016-01-31 14:05:46 +08:00
package db
2016-02-01 22:34:19 +08:00
/*
2016-02-06 14:58:00 +08:00
stores the functions related to file IO and category
2016-02-01 22:34:19 +08:00
*/
2016-01-31 14:05:46 +08:00
import (
"log"
2016-02-05 03:39:36 +08:00
"github.com/thewhitetulip/Tasks/types"
2016-01-31 14:05:46 +08:00
)
// 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, username string) error {
err := taskQuery("insert into files values(?,?,?,datetime())", fileName, token, username)
2016-01-31 14:05:46 +08:00
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
rows := database.query(sql, fileName)
defer rows.Close()
2016-01-31 14:05:46 +08:00
if rows.Next() {
err := rows.Scan(&fileName)
if err != nil {
log.Println(err)
return "", err
}
}
if err != nil {
return "", err
}
2016-01-31 14:05:46 +08:00
return fileName, nil
}
2016-02-03 01:40:44 +08:00
//GetCategories will return the list of categories to be
//rendered in the template
2016-05-14 15:26:24 +08:00
func GetCategories(username string) []types.CategoryCount {
userID, err := GetUserID(username)
if err != nil {
return nil
}
stmt := "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=?)"
rows := database.query(stmt, userID, userID, userID)
2016-02-05 03:39:36 +08:00
var categories []types.CategoryCount
var category types.CategoryCount
2016-02-03 01:40:44 +08:00
defer rows.Close()
2016-02-03 01:40:44 +08:00
for rows.Next() {
2016-02-05 03:39:36 +08:00
err := rows.Scan(&category.Name, &category.Count)
2016-02-03 01:40:44 +08:00
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 15:26:24 +08:00
func AddCategory(username, category string) error {
userID, err := GetUserID(username)
if err != nil {
return nil
}
log.Println("executing query to add category")
2016-05-14 15:26:24 +08:00
err = taskQuery("insert into category(name, user_id) values(?,?)", category, userID)
2016-02-03 01:40:44 +08:00
return err
}
2016-02-06 14:58:00 +08:00
// GetCategoryByName will return the ID of that category passed as args
2016-02-03 01:40:44 +08:00
// used while inserting tasks into the table
2016-05-14 15:26:24 +08:00
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-03 01:40:44 +08:00
var categoryID int
defer rows.Close()
2016-02-03 01:40:44 +08:00
for rows.Next() {
err := rows.Scan(&categoryID)
if err != nil {
log.Println(err)
}
}
return categoryID
}
2016-02-06 14:58:00 +08:00
//DeleteCategoryByName will be used to delete a category from the category page
2016-05-14 15:26:24 +08:00
func DeleteCategoryByName(username, category string) error {
2016-02-06 14:58:00 +08:00
//first we delete entries from task and then from category
2016-05-14 15:26:24 +08:00
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 14:58:00 +08:00
if err == nil {
2016-05-14 15:26:24 +08:00
err = taskQuery("delete from category where id=? and user_id=?", categoryID, userID)
2016-02-06 14:58:00 +08:00
if err != nil {
return err
}
}
return err
}
//UpdateCategoryByName will be used to delete a category from the category page
2016-05-14 15:26:24 +08:00
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 14:58:00 +08:00
log.Println(query)
2016-05-14 15:26:24 +08:00
err = taskQuery(query, newName, oldName, userID)
2016-02-06 14:58:00 +08:00
return err
2016-02-17 00:53:03 +08:00
}
2016-02-06 14:58:00 +08:00
2016-02-17 00:53:03 +08:00
//DeleteCommentByID will actually delete the comment from db
2016-05-14 15:26:24 +08:00
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-17 00:53:03 +08:00
return err
2016-02-06 14:58:00 +08:00
}