Timer create & delete

This commit is contained in:
2024-04-11 00:19:44 +02:00
parent 49f7be2a35
commit 5e12dabced
7 changed files with 331 additions and 30 deletions

View File

@ -5,6 +5,7 @@ import (
"net/http"
"database/sql"
"context"
"strings"
_ "github.com/mattn/go-sqlite3"
@ -12,6 +13,14 @@ import (
"stevenlr.com/timer/model"
)
func insertTimer(tx *sql.Tx, name string) error {
now := model.MakeTimeNow()
id := model.MakeUUID()
_, err := tx.Exec(`
INSERT INTO Timer VALUES ($1, $2, $3, $4)`, id, name, now, now);
return err
}
func initializeDatabase(db *sql.DB) error {
tx, err := db.Begin()
if err != nil { return err }
@ -28,16 +37,10 @@ func initializeDatabase(db *sql.DB) error {
`)
if err != nil { return err }
now := model.MakeTimeNow()
id := model.MakeUUID()
_, err = tx.Exec(`
INSERT INTO Timer VALUES ($1, $2, $3, $4)`, id, "My timer", now, now);
err = insertTimer(tx, "My timer")
if err != nil { return err }
id = model.MakeUUID()
_, err = tx.Exec(`
INSERT INTO Timer VALUES ($1, $2, $3, $4)`, id, "My timer 2", now, now);
err = insertTimer(tx, "My timer2")
if err != nil { return err }
return tx.Commit()
@ -72,16 +75,27 @@ func queryTimer(db *sql.DB, idStr string) *model.Timer {
return nil
}
type myServer struct {
func deleteTimer(db *sql.DB, idStr string) bool {
var id model.UUID
if err := id.Scan(idStr); err != nil { return false }
res, err := db.Exec("DELETE FROM Timer WHERE Id=$1", id)
if err != nil { return false }
affected, err := res.RowsAffected()
return err == nil && affected > 0
}
type MyServer struct {
db *sql.DB
}
func (server *myServer) handleNotFound(w http.ResponseWriter, _ *http.Request) {
func (server *MyServer) handleNotFound(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
view.Main(view.Error404()).Render(context.Background(), w)
}
func (server *myServer) handleMain(w http.ResponseWriter, r *http.Request) {
func (server *MyServer) handleMain(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
timers := queryAllTimers(server.db)
view.Main(view.TimersList(timers)).Render(context.Background(), w)
@ -90,7 +104,7 @@ func (server *myServer) handleMain(w http.ResponseWriter, r *http.Request) {
}
}
func (server *myServer) handleTimer(w http.ResponseWriter, r *http.Request) {
func (server *MyServer) handleTimer(w http.ResponseWriter, r *http.Request) {
timer := queryTimer(server.db, r.PathValue("timerId"))
if timer != nil {
view.Main(view.TimerView(*timer)).Render(context.Background(), w)
@ -99,6 +113,43 @@ func (server *myServer) handleTimer(w http.ResponseWriter, r *http.Request) {
}
}
func (server *MyServer) handleDeleteTimer(w http.ResponseWriter, r *http.Request) {
success := deleteTimer(server.db, r.PathValue("timerId"))
if !success {
w.WriteHeader(http.StatusNotFound)
}
}
func (server* MyServer) handlePutTimer(w http.ResponseWriter, r *http.Request) {
timerName := strings.TrimSpace(r.FormValue("timerName"))
tx, err := server.db.Begin()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
view.TimerCreateForm(timerName, "Internal server error").Render(context.Background(), w)
return
}
defer tx.Rollback()
if timerName == "" {
w.WriteHeader(http.StatusBadRequest)
view.TimerCreateForm("", "Timer name cannot be empty").Render(context.Background(), w)
return
}
err = insertTimer(tx, timerName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
view.TimerCreateForm(timerName, "Internal server error").Render(context.Background(), w)
return
}
tx.Commit()
timers := queryAllTimers(server.db)
view.TimersList(timers).Render(context.Background(), w)
}
func main() {
log.Println("Starting...")
@ -112,13 +163,15 @@ func main() {
log.Fatalln(err)
}
myServer := myServer{ db: db }
myServer := MyServer{ db: db }
fs := http.FileServer(http.Dir("static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.Handle("GET /static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/timer/{timerId}", myServer.handleTimer)
http.HandleFunc("/", myServer.handleMain)
http.HandleFunc("GET /timer/{timerId}", myServer.handleTimer)
http.HandleFunc("DELETE /timer/{timerId}", myServer.handleDeleteTimer)
http.HandleFunc("PUT /timer", myServer.handlePutTimer)
http.HandleFunc("GET /", myServer.handleMain)
log.Println("Started!")
http.ListenAndServe("0.0.0.0:80", nil)