todo handler and tests

This commit is contained in:
2025-02-09 00:21:58 -05:00
parent 6175abee25
commit c10cc07324
14 changed files with 714 additions and 34 deletions

View File

@@ -2,10 +2,11 @@ package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"gitea.michaelthomson.dev/mthomson/habits/internal/migrate"
todohandler "gitea.michaelthomson.dev/mthomson/habits/internal/todo/handler"
todosqliterepository "gitea.michaelthomson.dev/mthomson/habits/internal/todo/repository/sqlite"
todoservice "gitea.michaelthomson.dev/mthomson/habits/internal/todo/service"
_ "github.com/mattn/go-sqlite3"
@@ -21,19 +22,28 @@ func main() {
defer db.Close()
// run migrations
migrate.Migrate(db);
migrate.Migrate(db)
// create repos
todoRepository := todosqliterepository.NewSqliteTodoRepository(db)
// create services
todoService := todoservice.NewTodoService(&todoRepository)
todoService := todoservice.NewTodoService(todoRepository)
// create todo
todo := todoservice.NewTodo("clean dishes", false)
newTodo, err := todoService.CreateTodo(todo)
if err != nil {
log.Fatal(err)
// create mux
mux := http.NewServeMux()
// register handlers
mux.Handle("GET /todo/{id}", todohandler.HandleTodoGet(todoService))
mux.Handle("POST /todo", todohandler.HandleTodoCreate(todoService))
mux.Handle("DELETE /todo/{id}", todohandler.HandleTodoDelete(todoService))
mux.Handle("PUT /todo/{id}", todohandler.HandleTodoUpdate(todoService))
// create server
server := &http.Server{
Addr: ":8080",
Handler: mux,
}
fmt.Printf("ID of created todo: %d\n", newTodo.Id)
server.ListenAndServe()
}