-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathviews.go
109 lines (99 loc) · 3.19 KB
/
views.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
package views
/*Holds the fetch related view handlers*/
import (
"html/template"
"log"
"net/http"
"time"
"github.com/thewhitetulip/Tasks/db"
"github.com/thewhitetulip/Tasks/sessions"
)
var homeTemplate *template.Template
var deletedTemplate *template.Template
var completedTemplate *template.Template
var editTemplate *template.Template
var searchTemplate *template.Template
var templates *template.Template
var loginTemplate *template.Template
var message string //message will store the message to be shown as notification
var err error
//ShowAllTasksFunc is used to handle the "/" URL which is the default ons
//TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
username := sessions.GetCurrentUserName(r)
context, err := db.GetTasks(username, "pending", "")
log.Println(context)
categories := db.GetCategories(username)
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
} else {
if message != "" {
context.Message = message
}
context.CSRFToken = "abcd"
context.Categories = categories
message = ""
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context)
}
}
}
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
username := sessions.GetCurrentUserName(r)
categories := db.GetCategories(username)
context, err := db.GetTasks(username, "deleted", "")
context.Categories = categories
if err != nil {
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
}
if message != "" {
context.Message = message
message = ""
}
err = deletedTemplate.Execute(w, context)
if err != nil {
log.Fatal(err)
}
}
}
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
username := sessions.GetCurrentUserName(r)
categories := db.GetCategories(username)
context, err := db.GetTasks(username, "completed", "")
context.Categories = categories
if err != nil {
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
}
completedTemplate.Execute(w, context)
}
}
//ShowCategoryFunc will populate the /category/<id> URL which shows all the tasks related
// to that particular category
func ShowCategoryFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) {
category := r.URL.Path[len("/category/"):]
username := sessions.GetCurrentUserName(r)
context, err := db.GetTasks(username, "", category)
categories := db.GetCategories(username)
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
if message != "" {
context.Message = message
}
context.CSRFToken = "abcd"
context.Categories = categories
message = ""
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context)
}
}