diff --git a/main.go b/main.go index 72b6f45..bfcbca8 100644 --- a/main.go +++ b/main.go @@ -77,7 +77,7 @@ func FetchTasks(w http.ResponseWriter, r *http.Request) { tmpl.ExecuteTemplate(w, "todoList", todos) } -func getTaskForm(w http.ResponseWriter, r *https.Request) { +func getTaskForm(w http.ResponseWriter, r *http.Request) { tmpl.ExecuteTemplate(w, "addTaskForm", nil) } @@ -97,4 +97,57 @@ func addTask(w http.ResponseWriter, r *http.Request){ if executeErr != nil { log.Fatal(executeErr) } + + //return list of todos + todos, _ := getTasks(db) + //You can also just send back the single task and append it + //I like returning the whole list just to get everything fresh, but this might not be the best strategy + tmpl.ExecuteTemplate(w, "todolist", todos) +} + +func getTaskUpdateForm(w http.ResponseWriter, r *http.Request) { + + vars := mux.Vars(r) + //conver string id from url to integer + taskId, _ := strconv.Atoi(vars["id"]) + task, err := getTaskByID(db, taskId) + if err != nil { + http.Error(w, err.Error(), httpStatusInternalServerError) + } + tmpl.ExecuteTemplate(w, "updateTaskForm", task) +} + +func updateTask(w http.ResponseWriter, r *http.Request ) { + vars := mux.Vars(r) + + taskItem := r.FormValue("task") + + //taskStatus, _ := strconv.ParseBool(r.FormValue("done")) + var taskStatus bool + fmt.Println(r.FormValue("done")) + + //check string value of the checkbox + switch strings.ToLower(r.FormValue("done")) { + case "yes", "on": + taskStatus := true + case "no", "off": + taskStatus := false + default: + taskStatus := false + } + taskID, _ := strconv.Atoi(vars["id"]) + task := Task{ + taskID, taskItem, taskStatus, + } + updateErr := updateTaskById(db, task) + if updateErr != nil { + log.Fatal(updateErr) + } + + todos, _ := getTasks(db) + tmpl.ExecuteTemplate(w, "todolist", todos) +} + +func deleteTask(w http.ResponseWriter, r *http.Request) { + }