2015-11-13 17:04:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the main file for the Task application
|
|
|
|
* License: MIT
|
|
|
|
**/
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2015-11-16 09:55:43 +08:00
|
|
|
"github.com/thewhitetulip/Tasks/viewmodels"
|
2015-11-18 00:32:14 +08:00
|
|
|
"io/ioutil"
|
2015-11-13 17:04:42 +08:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2015-11-18 00:32:14 +08:00
|
|
|
"strings"
|
2015-11-13 17:04:42 +08:00
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
var homeTemplate *template.Template
|
|
|
|
var deletedTemplate *template.Template
|
2015-11-14 18:56:53 +08:00
|
|
|
var completedTemplate *template.Template
|
2015-11-13 17:04:42 +08:00
|
|
|
var editTemplate *template.Template
|
|
|
|
var searchTemplate *template.Template
|
2015-11-18 00:32:14 +08:00
|
|
|
var templates *template.Template
|
2015-11-13 17:04:42 +08:00
|
|
|
var err error
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
defer viewmodels.Close()
|
2015-11-18 00:32:14 +08:00
|
|
|
var allFiles []string
|
|
|
|
files, err := ioutil.ReadDir("./templates")
|
|
|
|
for _, file := range files {
|
|
|
|
filename := file.Name()
|
|
|
|
if strings.HasSuffix(filename, ".gtpl") {
|
|
|
|
allFiles = append(allFiles, "./templates/"+filename)
|
|
|
|
}
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
|
2015-11-14 18:56:53 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2015-11-18 00:32:14 +08:00
|
|
|
templates, err = template.ParseFiles(allFiles...)
|
|
|
|
homeTemplate = templates.Lookup("home.gtpl")
|
|
|
|
deletedTemplate = templates.Lookup("deleted.gtpl")
|
|
|
|
|
|
|
|
editTemplate = templates.Lookup("edit.gtpl")
|
|
|
|
searchTemplate = templates.Lookup("search.gtpl")
|
|
|
|
completedTemplate = templates.Lookup("completed.gtpl")
|
2015-11-14 18:56:53 +08:00
|
|
|
|
2015-11-13 17:04:42 +08:00
|
|
|
router := httprouter.New()
|
|
|
|
router.GET("/", ShowAllTasks)
|
2015-11-14 18:56:53 +08:00
|
|
|
router.GET("/complete/:id", CompleteTask)
|
2015-11-13 17:04:42 +08:00
|
|
|
router.GET("/delete/:id", DeleteTask)
|
2015-11-14 18:56:53 +08:00
|
|
|
router.GET("/deleted/", ShowTrashTask)
|
|
|
|
router.GET("/trash/:id", TrashTask)
|
2015-11-13 17:04:42 +08:00
|
|
|
router.GET("/edit/:id", EditTask)
|
2015-11-14 18:56:53 +08:00
|
|
|
router.GET("/complete/", ShowCompleteTasks)
|
2015-11-13 17:04:42 +08:00
|
|
|
router.GET("/restore/:id", RestoreTask)
|
|
|
|
router.POST("/add/", AddTask)
|
|
|
|
router.POST("/update/", UpdateTask)
|
|
|
|
router.POST("/search/", SearchTask)
|
|
|
|
router.NotFound = http.FileServer(http.Dir("public"))
|
|
|
|
fmt.Println("running on 8080")
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", router))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowAllTasks(w http.ResponseWriter, r *http.Request, parm httprouter.Params) {
|
2015-11-14 18:56:53 +08:00
|
|
|
context := viewmodels.GetTasks("pending") //true when you want non deleted notes
|
2015-11-13 17:04:42 +08:00
|
|
|
homeTemplate.Execute(w, context)
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:56:53 +08:00
|
|
|
func ShowTrashTask(w http.ResponseWriter, r *http.Request, parm httprouter.Params) {
|
|
|
|
context := viewmodels.GetTasks("trashed") //false when you want deleted notes
|
|
|
|
deletedTemplate.Execute(w, context)
|
|
|
|
}
|
|
|
|
|
2015-11-13 21:11:30 +08:00
|
|
|
func SearchTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2015-11-13 17:04:42 +08:00
|
|
|
r.ParseForm()
|
|
|
|
query := r.Form.Get("query")
|
|
|
|
context := viewmodels.SearchTask(query)
|
|
|
|
searchTemplate.Execute(w, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
r.ParseForm()
|
|
|
|
title := r.Form.Get("title")
|
|
|
|
content := r.Form.Get("content")
|
|
|
|
truth := viewmodels.AddTask(title, content)
|
|
|
|
if truth == true {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:56:53 +08:00
|
|
|
func ShowCompleteTasks(w http.ResponseWriter, r *http.Request, parm httprouter.Params) {
|
|
|
|
context := viewmodels.GetTasks("complete") //false when you want finished notes
|
|
|
|
completedTemplate.Execute(w, context)
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func EditTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
|
|
|
id, err := strconv.Atoi(param.ByName("id"))
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
task := viewmodels.GetTaskById(id)
|
|
|
|
editTemplate.Execute(w, task)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:56:53 +08:00
|
|
|
func CompleteTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
2015-11-13 17:04:42 +08:00
|
|
|
id, err := strconv.Atoi(param.ByName("id"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
2015-11-14 18:56:53 +08:00
|
|
|
viewmodels.CompleteTask(id)
|
2015-11-13 17:04:42 +08:00
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
|
|
|
id := param.ByName("id")
|
2015-11-13 21:11:30 +08:00
|
|
|
if id == "all" {
|
2015-11-13 17:04:42 +08:00
|
|
|
viewmodels.DeleteAll()
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
} else {
|
|
|
|
id, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
viewmodels.DeleteTask(id)
|
2015-11-14 18:56:53 +08:00
|
|
|
http.Redirect(w, r, "/deleted/", http.StatusFound)
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:56:53 +08:00
|
|
|
func TrashTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
|
|
|
id, err := strconv.Atoi(param.ByName("id"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
fmt.Println("deleting ", id)
|
|
|
|
viewmodels.TrashTask(id)
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:04:42 +08:00
|
|
|
func RestoreTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
|
|
|
id, err := strconv.Atoi(param.ByName("id"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
viewmodels.RestoreTask(id)
|
2015-11-14 18:56:53 +08:00
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2015-11-13 17:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
|
|
|
r.ParseForm()
|
|
|
|
id, err := strconv.Atoi(r.Form.Get("id"))
|
2015-11-13 21:11:30 +08:00
|
|
|
if err != nil {
|
2015-11-13 17:04:42 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
title := r.Form.Get("title")
|
|
|
|
content := r.Form.Get("content")
|
|
|
|
viewmodels.UpdateTask(id, title, content)
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|