Tasks/views/views.go

76 lines
2.1 KiB
Go
Raw Normal View History

2015-11-21 15:58:23 +08:00
package views
2016-02-01 22:34:01 +08:00
/*Holds the fetch related view handlers*/
2015-11-21 15:58:23 +08:00
import (
"net/http"
"text/template"
2016-01-18 10:28:08 +08:00
"time"
2016-02-01 21:45:04 +08:00
"github.com/thewhitetulip/Tasks/db"
2015-11-21 15:58:23 +08:00
)
var homeTemplate *template.Template
var deletedTemplate *template.Template
var completedTemplate *template.Template
var editTemplate *template.Template
var searchTemplate *template.Template
var templates *template.Template
2015-11-22 11:51:29 +08:00
var message string //message will store the message to be shown as notification
2015-11-21 15:58:23 +08:00
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" {
2016-02-01 21:45:04 +08:00
context, err := db.GetTasks("pending")
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
2015-11-22 11:51:29 +08:00
if message != "" {
context.Message = message
}
2016-01-18 10:28:08 +08:00
context.CSRFToken = "abcd"
2015-11-22 11:51:29 +08:00
message = ""
2016-01-18 09:02:15 +08:00
expiration := time.Now().Add(365 * 24 * time.Hour)
2016-01-18 10:28:08 +08:00
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
2016-01-18 09:02:15 +08:00
http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context)
2015-11-22 11:51:29 +08:00
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//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" {
2016-02-01 21:45:04 +08:00
context, err := db.GetTasks("deleted")
if err != nil {
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
}
2015-11-22 11:51:29 +08:00
if message != "" {
context.Message = message
message = ""
}
2015-11-21 15:58:23 +08:00
deletedTemplate.Execute(w, context)
2015-11-22 11:51:29 +08:00
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2016-02-01 21:45:04 +08:00
context, err := db.GetTasks("completed")
if err != nil {
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
}
2015-11-21 15:58:23 +08:00
completedTemplate.Execute(w, context)
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}