|
|
|
@ -5,7 +5,9 @@ import (
|
|
|
|
|
"html/template"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed views/*.html
|
|
|
|
@ -40,6 +42,19 @@ type Todo struct {
|
|
|
|
|
|
|
|
|
|
type ViewModel struct {
|
|
|
|
|
Todos []Todo
|
|
|
|
|
Only string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func filterSlice[T any](xs []T, pred func(T) bool) []T {
|
|
|
|
|
result := make([]T, 0, len(xs))
|
|
|
|
|
for _, element := range xs {
|
|
|
|
|
if pred(element) {
|
|
|
|
|
result = append(result, element)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
@ -48,8 +63,21 @@ func main() {
|
|
|
|
|
t := New()
|
|
|
|
|
http.Handle("/public/", http.FileServer(http.FS(publicFS)))
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
|
renderTodos := todos
|
|
|
|
|
only := q.Get("only")
|
|
|
|
|
|
|
|
|
|
if only == "active" {
|
|
|
|
|
renderTodos = filterSlice(todos, func(todo Todo) bool { return !todo.Completed })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if only == "completed" {
|
|
|
|
|
renderTodos = filterSlice(todos, func(todo Todo) bool { return todo.Completed })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vm := ViewModel{
|
|
|
|
|
Todos: todos,
|
|
|
|
|
Todos: renderTodos,
|
|
|
|
|
Only: only,
|
|
|
|
|
}
|
|
|
|
|
err := t.Render(w, "list.html", vm)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -69,12 +97,36 @@ func main() {
|
|
|
|
|
|
|
|
|
|
title := r.Form.Get("title")
|
|
|
|
|
todos = append(todos, Todo{
|
|
|
|
|
Id: strconv.Itoa(rand.Int()),
|
|
|
|
|
Title: title,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Location", "/")
|
|
|
|
|
w.WriteHeader(http.StatusSeeOther)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/toggle", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method != "POST" {
|
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id := r.Form.Get("id")
|
|
|
|
|
for i, todo := range todos {
|
|
|
|
|
if todo.Id == id {
|
|
|
|
|
todo.Completed = !todo.Completed
|
|
|
|
|
todos[i] = todo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Location", "/")
|
|
|
|
|
w.WriteHeader(http.StatusSeeOther)
|
|
|
|
|
})
|
|
|
|
|
log.Print("Running on 8080")
|
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
|
}
|
|
|
|
|