Tasks/views/otherViews.go

164 lines
4.2 KiB
Go
Raw Normal View History

2016-01-31 14:20:01 +08:00
package views
2016-02-01 22:34:19 +08:00
/*
Holds the non insert/update/delete related view handlers
*/
2016-01-31 14:20:01 +08:00
import (
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"text/template"
2016-02-01 21:45:04 +08:00
"github.com/thewhitetulip/Tasks/db"
2016-05-14 15:26:24 +08:00
"github.com/thewhitetulip/Tasks/sessions"
2016-02-06 18:02:46 +08:00
"github.com/thewhitetulip/Tasks/utils"
2016-01-31 14:20:01 +08:00
)
//PopulateTemplates is used to parse all templates present in
//the templates folder
func PopulateTemplates() {
var allFiles []string
templatesDir := "./templates/"
2016-01-31 14:20:01 +08:00
files, err := ioutil.ReadDir(templatesDir)
if err != nil {
log.Println(err)
os.Exit(1) // No point in running app if templates aren't read
}
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".html") {
allFiles = append(allFiles, templatesDir+filename)
}
}
if err != nil {
log.Println(err)
os.Exit(1)
}
templates, err = template.ParseFiles(allFiles...)
if err != nil {
log.Println(err)
os.Exit(1)
}
homeTemplate = templates.Lookup("home.html")
deletedTemplate = templates.Lookup("deleted.html")
editTemplate = templates.Lookup("edit.html")
searchTemplate = templates.Lookup("search.html")
completedTemplate = templates.Lookup("completed.html")
2016-05-09 10:11:05 +08:00
loginTemplate = templates.Lookup("login.html")
2016-01-31 14:20:01 +08:00
}
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url
func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
2016-05-12 01:19:32 +08:00
if r.Method == "GET" {
redirectURL := utils.GetRedirectUrl(r.Referer())
id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
if err != nil {
log.Println(err)
} else {
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
err = db.CompleteTask(username, id)
2016-01-31 14:20:01 +08:00
if err != nil {
2016-05-12 01:19:32 +08:00
message = "Complete task failed"
2016-01-31 14:20:01 +08:00
} else {
2016-05-12 01:19:32 +08:00
message = "Task marked complete"
2016-01-31 14:20:01 +08:00
}
2016-05-12 01:19:32 +08:00
http.Redirect(w, r, redirectURL, http.StatusFound)
2016-01-31 14:20:01 +08:00
}
}
}
//SearchTaskFunc is used to handle the /search/ url, handles the search function
func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
2016-05-12 01:19:32 +08:00
if r.Method == "POST" {
r.ParseForm()
query := r.Form.Get("query")
2016-02-29 10:04:58 +08:00
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
context, err := db.SearchTask(username, query)
if err != nil {
log.Println("error fetching search results")
}
2016-02-29 10:04:58 +08:00
2016-05-14 15:26:24 +08:00
categories := db.GetCategories(username)
2016-05-12 01:19:32 +08:00
context.Categories = categories
2016-02-29 10:04:58 +08:00
2016-05-12 01:19:32 +08:00
searchTemplate.Execute(w, context)
2016-01-31 14:20:01 +08:00
}
}
//UpdateTaskFunc is used to update a task, handes "/update/" URL
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
2016-05-12 01:19:32 +08:00
if r.Method == "POST" {
r.ParseForm()
id, err := strconv.Atoi(r.Form.Get("id"))
if err != nil {
log.Println(err)
}
category := r.Form.Get("category")
title := r.Form.Get("title")
content := r.Form.Get("content")
priority, err := strconv.Atoi(r.Form.Get("priority"))
if err != nil {
log.Println(err)
2016-01-31 14:20:01 +08:00
}
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
err = db.UpdateTask(id, title, content, category, priority, username)
2016-05-12 01:19:32 +08:00
if err != nil {
message = "Error updating task"
} else {
message = "Task updated"
log.Println(message)
}
http.Redirect(w, r, "/", http.StatusFound)
2016-01-31 14:20:01 +08:00
}
2016-01-31 22:22:00 +08:00
}
2016-02-06 14:58:00 +08:00
//UpdateCategoryFunc is used to update a task, handes "/upd-category/" URL
func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
2016-05-12 01:19:32 +08:00
if r.Method == "POST" {
var redirectURL string
r.ParseForm()
oldName := r.URL.Path[len("/upd-category/"):]
newName := r.Form.Get("catname")
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
err := db.UpdateCategoryByName(username, oldName, newName)
2016-05-12 01:19:32 +08:00
if err != nil {
message = "error updating category"
log.Println("not updated category " + oldName)
redirectURL = "/category/" + oldName
} else {
message = "cat " + oldName + " -> " + newName
redirectURL = "/category/" + newName
2016-02-06 14:58:00 +08:00
}
2016-05-12 01:19:32 +08:00
log.Println("redirecting to " + redirectURL)
http.Redirect(w, r, redirectURL, http.StatusFound)
2016-02-06 14:58:00 +08:00
}
}
//SignUpFunc will enable new users to sign up to our service
func SignUpFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
username := r.Form.Get("username")
password := r.Form.Get("password")
email := r.Form.Get("email")
log.Println(username, password, email)
err := db.CreateUser(username, password, email)
if err != nil {
http.Error(w, "Unable to sign user up", http.StatusInternalServerError)
} else {
http.Redirect(w, r, "/login/", 302)
}
}
}