all repos — well-binge @ 80150294670e221c25fd3ddab40d0301775d0e60

Create positive, recurring habits.

working save and delete
Marco Andronaco andronacomarco@gmail.com
Fri, 18 Oct 2024 00:29:07 +0200
commit

80150294670e221c25fd3ddab40d0301775d0e60

parent

4690b04bb57834544694aa1b0d740fe746ec7236

5 files changed, 83 insertions(+), 13 deletions(-)

jump to
M src/app/handlers.gosrc/app/handlers.go

@@ -57,6 +57,7 @@ name := r.FormValue("name")

if !checkHabitName(name) { http.Error(w, "Bad habit name.", http.StatusBadRequest) + return } var days uint

@@ -72,6 +73,7 @@

user, ok := getLoggedUser(r) if !ok { http.Error(w, "Could not get logged user", http.StatusInternalServerError) + return } db.Create(&Habit{

@@ -84,6 +86,68 @@

http.Redirect(w, r, "/habits", http.StatusFound) } +func postHabitsIDHandler(w http.ResponseWriter, r *http.Request) { + habit, err := getHabitHelper(w, r) + if err != nil { + return + } + + var changed bool + + name := r.FormValue("name") + if name != habit.Name { + if !checkHabitName(name) { + http.Error(w, "Bad habit name.", http.StatusBadRequest) + return + } + habit.Name = name + changed = true + } + + if !habit.Negative { + res, err := strconv.ParseUint(r.FormValue("days"), 10, 64) + if err != nil { + http.Error(w, "Bad days value.", http.StatusBadRequest) + return + } + days := uint(res) + if days != habit.Days { + habit.Days = days + changed = true + } + } + + disabled := r.FormValue("enabled") != "on" + if disabled != habit.Disabled { + habit.Disabled = disabled + changed = true + } + + if changed { + db.Save(&habit) + } + + http.Redirect(w, r, "/habits", http.StatusFound) +} + +func postDeleteIDHandler(w http.ResponseWriter, r *http.Request) { + id := getID(r) + if id == 0 { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + + user, ok := getLoggedUser(r) + if !ok { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + db.Delete(&Habit{}, "id = ? AND user_id = ?", id, user.ID) + + http.Redirect(w, r, "/habits", http.StatusFound) +} + func postAckIDHandler(w http.ResponseWriter, r *http.Request) { habit, err := getHabitHelper(w, r) if err != nil {

@@ -97,7 +161,7 @@ return

} } - db.Create(Ack{HabitID: habit.ID}) + db.Create(&Ack{HabitID: habit.ID}) now := time.Now() habit.LastAck = &now
M src/app/init.gosrc/app/init.go

@@ -119,9 +119,11 @@ // App

http.HandleFunc("GET /", getIndexHandler) http.HandleFunc("GET /habits", loginRequired(getHabitsHandler)) http.HandleFunc("GET /habits/{id}", loginRequired(getHabitsIDHandler)) - http.HandleFunc("GET /new/positive", loginRequired(getNewPositiveHandler)) - http.HandleFunc("GET /new/negative", loginRequired(getNewNegativeHandler)) + http.HandleFunc("GET /new-positive", loginRequired(getNewPositiveHandler)) + http.HandleFunc("GET /new-negative", loginRequired(getNewNegativeHandler)) http.HandleFunc("POST /new", loginRequired(postNewHandler)) + http.HandleFunc("POST /habits/{id}", loginRequired(postHabitsIDHandler)) + http.HandleFunc("POST /delete/{id}", loginRequired(postDeleteIDHandler)) http.HandleFunc("POST /ack/{id}", loginRequired(postAckIDHandler)) // Auth
M static/style.cssstatic/style.css

@@ -29,3 +29,7 @@

.actions > form { padding-inline: 5px; } + +thead { + font-weight: bold; +}
M templates/habits-id.tmpltemplates/habits-id.tmpl

@@ -5,7 +5,7 @@

{{define "content" -}} <h1>Edit habit</h1> - <form method="post" action="/edit"> + <form method="post" action="/habits/{{ .ID }}"> <label> <span>Name:</span> <input type="text" name="name" autocomplete="off" placeholder="Name" value="{{ .Name }}" required />

@@ -20,9 +20,9 @@ <span>Enabled:</span>

<input type="checkbox" name="enabled"{{ if not .Disabled }} checked{{ end }} /> </label> {{ end }} - <input type="submit" value="Edit" /> + <input type="submit" value="Save" /> </form> - <form method="post" action="/edit"> + <form method="post" action="/delete/{{ .ID }}"> <input type="submit" value="Delete" /> </form> {{end}}
M templates/habits.tmpltemplates/habits.tmpl

@@ -7,7 +7,7 @@ <h1>Welcome, <i>{{.User.Username}}</i>!</h1>

<a href="/logout">Logout</a><br /> <div class="habits-title"> <h3>Positive habits</h3> - <a href="/new/positive" class="button">+ Add</a> + <a href="/new-positive" class="button">+ Add</a> </div> <table> <thead>

@@ -34,14 +34,14 @@ </form>

</td> </tr> </a> - {{end}} + {{ end }} </tbody> <tfoot></tfoot> </table> <div class="habits-title"> <h3>Negative habits</h3> - <a href="/new/negative" class="button">+ Add</a> + <a href="/new-negative" class="button">+ Add</a> </div> <table> <thead>

@@ -52,10 +52,10 @@ <td>Actions</td>

</tr> </thead> <tbody> - {{range .Negative}} + {{ range .Negative }} <tr class="{{.Class}}"> - <td>{{.Name}}</td> - <td>{{.LastAck}}</td> + <td>{{ .Name }}</td> + <td><i>{{ .LastAck }}</i></td> <td class="actions"> <form action="/ack/{{ .ID }}" method="post"> <input type="submit" value="Ack" />

@@ -66,7 +66,7 @@ <input type="submit" value="Edit" />

</form> </td> </tr> - {{end}} + {{ end }} </tbody> <tfoot></tfoot> </table>