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)
searchTemplate.Execute(w, context)
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
@ -88,6 +88,8 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
} else {
fmt.Println(err)
}
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
@ -96,11 +98,14 @@ func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
context := db.GetTasks("complete") //false when you want finished notes
completedTemplate.Execute(w, context)
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil {
fmt.Println(err)
@ -108,6 +113,9 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
task := db.GetTaskByID(id)
editTemplate.Execute(w, task)
}
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url
@ -124,6 +132,8 @@ func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
fmt.Println("redirecting to home")
http.Redirect(w, r, "/", http.StatusFound)
}
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
@ -143,11 +153,14 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/deleted/", http.StatusFound)
}
}
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
//TrashTaskFunc is used to populate the "/trash/" URL
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
if err != nil {
fmt.Println(err)
@ -155,10 +168,14 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
db.TrashTask(id)
http.Redirect(w, r, "/", http.StatusFound)
}
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
if err != nil {
fmt.Println(err)
@ -166,10 +183,14 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
db.RestoreTask(id)
http.Redirect(w, r, "/deleted/", http.StatusFound)
}
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
//UpdateTaskFunc is used to update a task, handes "/update/" URL
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST"{
r.ParseForm()
id, err := strconv.Atoi(r.Form.Get("id"))
if err != nil {
@ -179,6 +200,9 @@ func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
content := r.Form.Get("content")
db.UpdateTask(id, title, content)
http.Redirect(w, r, "/", http.StatusFound)
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
//ServeStaticFunc is used to serve static files