You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.3 KiB

package main
import (
"embed"
"log"
"net/http"
"strconv"
2 years ago
"jxs.me/todogo/database"
2 years ago
"jxs.me/todogo/views"
)
//go:embed public/*
var publicFS embed.FS
type server struct {
views *views.Views
router *http.ServeMux
2 years ago
db *database.DB
}
2 years ago
func newServer() *server {
2 years ago
srv := &server{
views: views.New(),
router: http.NewServeMux(),
2 years ago
db: database.New(),
}
srv.routes()
return srv
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}
func (s *server) HandleList() http.HandlerFunc {
type viewModel struct {
1 year ago
Todos []database.Todo
Only string
Incomplete int
}
list := s.views.Renderer("list.html")
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
only := q.Get("only")
1 year ago
var dbTodos []database.Todo
2 years ago
var err error
switch only {
case "active":
dbTodos, err = s.db.GetTodosByCompleted(false)
case "completed":
dbTodos, err = s.db.GetTodosByCompleted(true)
default:
dbTodos, err = s.db.GetAllTodos()
}
2 years ago
if err != nil {
http.Error(w, "Failed to fetch todos", http.StatusInternalServerError)
return
}
2 years ago
incomplete := s.db.GetIncompleteTodosCount()
2 years ago
vm := viewModel{
2 years ago
Todos: dbTodos,
2 years ago
Only: only,
Incomplete: incomplete,
}
2 years ago
list(w, r, vm)
}
}
2 years ago
func (s *server) HandleCreate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2 years ago
title := r.Form.Get("title")
2 years ago
if err := s.db.InsertTodo(title); err != nil {
http.Error(w, "failed to insert", http.StatusInternalServerError)
return
}
2 years ago
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
func (s *server) HandleToggle() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2 years ago
rawId := r.Form.Get("id")
id, err := strconv.Atoi(rawId)
if err != nil {
http.Error(w, "Bad id", http.StatusBadRequest)
2 years ago
return
2 years ago
}
err = s.db.ToggleTodo(id)
if err != nil {
http.Error(w, "failed to toggle", http.StatusInternalServerError)
2 years ago
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
2 years ago
func (s *server) HandleDestroy() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2 years ago
rawId := r.Form.Get("id")
id, err := strconv.Atoi(rawId)
if err != nil {
http.Error(w, "Bad id", http.StatusBadRequest)
return
}
err = s.db.DeleteTodo(id)
if err != nil {
http.Error(w, "failed to toggle", http.StatusInternalServerError)
return
}
2 years ago
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
2 years ago
func (s *server) HandleClear() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2 years ago
err := s.db.DeleteCompletedTodos()
if err != nil {
http.Error(w, "failed to clear", http.StatusInternalServerError)
return
}
2 years ago
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
func (s *server) routes() {
s.router.Handle("/public/", http.FileServer(http.FS(publicFS)))
s.router.HandleFunc("/", WithMethod("GET", s.HandleList()))
s.router.HandleFunc("/todo", WithForm(WithMethod("POST", s.HandleCreate())))
s.router.HandleFunc("/toggle", WithForm(WithMethod("POST", s.HandleToggle())))
s.router.HandleFunc("/destroy", WithForm(WithMethod("POST", s.HandleDestroy())))
s.router.HandleFunc("/clear", WithMethod("POST", s.HandleClear()))
}
func run() error {
srv := newServer()
log.Print("Running on 8080")
return http.ListenAndServe(":8080", srv)
}
func main() {
log.Fatal(run())
}