109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"todo/templates"
|
|
"todo/models"
|
|
"strconv"
|
|
"github.com/go-chi/chi"
|
|
)
|
|
|
|
var store = MustNewDB()
|
|
|
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
todos, err := store.GetTodos()
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"Error al intentar obtener todos los TODO", err.Error()})
|
|
return
|
|
}
|
|
|
|
templates.WritePage(w, &templates.IndexPage{todos})
|
|
}
|
|
|
|
func createHandler(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
todo := models.Todo{Id: 0, Todo: r.FormValue("todo"), Done: false}
|
|
err := store.CreateTodo(todo)
|
|
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"Error al intentar crear un nuevo TODO", err.Error()})
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|
|
|
|
func toggleHandler(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"El id entregado es invalido", err.Error()})
|
|
return
|
|
}
|
|
|
|
err = store.ToggleTodo(id)
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"Error al actualizar el TODO", err.Error()})
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|
|
|
|
func deleteHandler(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"El id entregado es invalido", err.Error()})
|
|
return
|
|
}
|
|
|
|
err = store.DeleteTodo(id)
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"Error al eliminar el TODO", err.Error()})
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|
|
|
|
func editHandler(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"El id entregado es invalido", err.Error()})
|
|
return
|
|
}
|
|
|
|
todos, err := store.GetTodos()
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"Error al intentar obtener todos los TODO", err.Error()})
|
|
return
|
|
}
|
|
editingTodo, err := store.GetTodo(id)
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"Error al intentar obtener un TODO", err.Error()})
|
|
return
|
|
}
|
|
|
|
templates.WritePage(w, &templates.EditPage{todos, editingTodo})
|
|
}
|
|
|
|
func updateHandler(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"El id entregado es invalido", err.Error()})
|
|
return
|
|
}
|
|
|
|
message := r.FormValue("todo")
|
|
|
|
todo := models.Todo{Id: id, Todo: message }
|
|
err = store.UpdateTodo(todo)
|
|
|
|
if err != nil {
|
|
templates.WritePage(w, &templates.Error500Page{"Error al intentar actualizar un TODO", err.Error()})
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|