-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
134 lines (121 loc) · 3.96 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package db
/*
stores the functions related to file IO and category
*/
import (
"log"
"github.com/thewhitetulip/Tasks-vue/types"
)
// 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 {
userID, err := GetUserID(username)
if err != nil {
return err
}
err = taskQuery("insert into files values(?,?,?,datetime())", fileName, token, userID)
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()
if rows.Next() {
err := rows.Scan(&fileName)
if err != nil {
log.Println(err)
return "", err
}
}
if err != nil {
return "", err
}
return fileName, nil
}
//GetCategories will return the list of categories to be
//rendered in the template
func GetCategories(username string) ([]types.Category, error) {
var categories []types.Category
var category types.Category
userID, err := GetUserID(username)
if err != nil {
return categories, nil
}
stmt := "select c.id, 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='PENDING' and c.user_id=? group by name union select c.id, 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='PENDING' and c.user_id=?)"
rows := database.query(stmt, userID, userID, userID)
defer rows.Close()
for rows.Next() {
err := rows.Scan(&category.ID, &category.Name, &category.TaskCount)
if err != nil {
return categories, err
}
categories = append(categories, category)
}
return categories, nil
}
//AddCategory is used to add the task in the database
func AddCategory(username, category string) error {
userID, err := GetUserID(username)
if err != nil {
return nil
}
log.Println("executing query to add category")
err = taskQuery("insert into category(name, user_id) values(?,?)", category, userID)
return err
}
// GetCategoryByName will return the ID of that category passed as args
// used while inserting tasks into the table
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)
var categoryID int
defer rows.Close()
for rows.Next() {
err := rows.Scan(&categoryID)
if err != nil {
log.Println(err)
}
}
return categoryID
}
//DeleteCategoryByName will be used to delete a category from the category page
func DeleteCategoryByName(username, category string) error {
//first we delete entries from task and then from category
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)
if err == nil {
err = taskQuery("delete from category where id=? and user_id=?", categoryID, userID)
if err != nil {
return err
}
}
return err
}
//UpdateCategoryByName will be used to delete a category from the category page
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=?"
log.Println(query)
err = taskQuery(query, newName, oldName, userID)
return err
}
//DeleteCommentByID will actually delete the comment from db
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)
return err
}