handled incorrect req methods

This commit is contained in:
thewhitetulip 2015-11-21 17:08:08 +05:30
parent 8eea6f51f5
commit a5f50060aa
1 changed files with 47 additions and 23 deletions

View File

@ -71,7 +71,7 @@ func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
context := db.SearchTask(query) context := db.SearchTask(query)
searchTemplate.Execute(w, context) searchTemplate.Execute(w, context)
} else { } else {
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
@ -88,6 +88,8 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
} else { } else {
fmt.Println(err) fmt.Println(err)
} }
} else {
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
@ -96,17 +98,23 @@ func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context := db.GetTasks("complete") //false when you want finished notes context := db.GetTasks("complete") //false when you want finished notes
completedTemplate.Execute(w, context) completedTemplate.Execute(w, context)
} else {
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//EditTaskFunc is used to edit tasks, handles "/edit/" URL //EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) { func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):]) if r.Method == "GET" {
if err != nil { id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
fmt.Println(err) if err != nil {
fmt.Println(err)
} else {
task := db.GetTaskByID(id)
editTemplate.Execute(w, task)
}
} else { } else {
task := db.GetTaskByID(id) http.Redirect(w, r, "/", http.StatusFound)
editTemplate.Execute(w, task)
} }
} }
@ -124,6 +132,8 @@ func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
fmt.Println("redirecting to home") fmt.Println("redirecting to home")
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
} else {
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
@ -143,42 +153,56 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/deleted/", http.StatusFound) http.Redirect(w, r, "/deleted/", http.StatusFound)
} }
} }
} else {
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//TrashTaskFunc is used to populate the "/trash/" URL //TrashTaskFunc is used to populate the "/trash/" URL
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) { func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) if r.Method == "GET" {
if err != nil { id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
fmt.Println(err) if err != nil {
fmt.Println(err)
} else {
db.TrashTask(id)
http.Redirect(w, r, "/", http.StatusFound)
}
} else { } else {
db.TrashTask(id)
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL //RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) { func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):]) if r.Method == "GET" {
if err != nil { id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
fmt.Println(err) if err != nil {
fmt.Println(err)
} else {
db.RestoreTask(id)
http.Redirect(w, r, "/deleted/", http.StatusFound)
}
} else { } else {
db.RestoreTask(id) http.Redirect(w, r, "/", http.StatusFound)
http.Redirect(w, r, "/deleted/", http.StatusFound)
} }
} }
//UpdateTaskFunc is used to update a task, handes "/update/" URL //UpdateTaskFunc is used to update a task, handes "/update/" URL
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) { func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
r.ParseForm() if r.Method == "POST"{
id, err := strconv.Atoi(r.Form.Get("id")) r.ParseForm()
if err != nil { id, err := strconv.Atoi(r.Form.Get("id"))
fmt.Println(err) if err != nil {
fmt.Println(err)
}
title := r.Form.Get("title")
content := r.Form.Get("content")
db.UpdateTask(id, title, content)
http.Redirect(w, r, "/", http.StatusFound)
} else {
http.Redirect(w, r, "/", http.StatusFound)
} }
title := r.Form.Get("title")
content := r.Form.Get("content")
db.UpdateTask(id, title, content)
http.Redirect(w, r, "/", http.StatusFound)
} }
//ServeStaticFunc is used to serve static files //ServeStaticFunc is used to serve static files