refac: slim up endpoints

This commit is contained in:
Teajey 2024-05-23 22:00:27 +12:00
parent b38e707ae2
commit 37c3f1f20d
Signed by: Teajey
GPG Key ID: 970E790FE834A713
7 changed files with 41 additions and 26 deletions

View File

@ -42,7 +42,7 @@ func (ctx *Context) WishlistDelete(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
} }
func (ctx *Context) UpdateForeignWishlist(w http.ResponseWriter, r *http.Request) { func (ctx *Context) ForeignWishlistPost(w http.ResponseWriter, r *http.Request) {
user := ctx.Auth.ExpectUser(r) user := ctx.Auth.ExpectUser(r)
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
@ -91,5 +91,5 @@ func (ctx *Context) UpdateForeignWishlist(w http.ResponseWriter, r *http.Request
default: default:
http.Error(w, "Invalid intent", http.StatusBadRequest) http.Error(w, "Invalid intent", http.StatusBadRequest)
} }
http.Redirect(w, r, "/"+userReference, http.StatusSeeOther) http.Redirect(w, r, "/list/"+userReference, http.StatusSeeOther)
} }

View File

@ -15,7 +15,7 @@ type ForeignWishlistProps struct {
Gifts []db.Gift Gifts []db.Gift
} }
func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) { func (ctx *Context) ForeignWishlist(w http.ResponseWriter, r *http.Request) {
userReference := r.PathValue("userReference") userReference := r.PathValue("userReference")
user := ctx.Auth.ExpectUser(r) user := ctx.Auth.ExpectUser(r)
if user.Reference == userReference { if user.Reference == userReference {

View File

@ -31,3 +31,21 @@ func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) {
p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.HostUrl.String()} p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.HostUrl.String()}
templates.Execute(w, "home.gotmpl", p) templates.Execute(w, "home.gotmpl", p)
} }
func (ctx *Context) HomePost(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "Couldn't parse form", http.StatusBadRequest)
return
}
switch r.Form.Get("intent") {
case "add_idea":
ctx.WishlistAdd(w, r)
return
case "delete_idea":
ctx.WishlistDelete(w, r)
return
default:
ctx.TodoUpdate(w, r)
return
}
}

View File

@ -11,15 +11,15 @@ func (ctx *Context) TodoUpdate(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
switch r.Form.Get("mode") { switch r.Form.Get("intent") {
case "unclaim": case "unclaim_todo":
unclaims := r.Form["gift"] unclaims := r.Form["gift"]
err := user.ClaimGifts([]string{}, unclaims) err := user.ClaimGifts([]string{}, unclaims)
if err != nil { if err != nil {
http.Error(w, "Failed to update claim...", http.StatusInternalServerError) http.Error(w, "Failed to update claim...", http.StatusInternalServerError)
return return
} }
case "complete": case "complete_todo":
claims := r.Form["gift"] claims := r.Form["gift"]
err := user.CompleteGifts(claims) err := user.CompleteGifts(claims)
if err != nil { if err != nil {
@ -28,7 +28,7 @@ func (ctx *Context) TodoUpdate(w http.ResponseWriter, r *http.Request) {
return return
} }
default: default:
http.Error(w, "Invalid mode", http.StatusBadRequest) http.Error(w, "Invalid intent", http.StatusBadRequest)
} }
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
} }

10
main.go
View File

@ -35,12 +35,10 @@ func main() {
publicMux.HandleFunc("GET /", authMiddleware.Login) publicMux.HandleFunc("GET /", authMiddleware.Login)
publicMux.HandleFunc("POST /", authMiddleware.LoginPost) publicMux.HandleFunc("POST /", authMiddleware.LoginPost)
protectedMux.HandleFunc("GET /", ctx.Home) protectedMux.HandleFunc("GET /{$}", ctx.Home)
protectedMux.HandleFunc("GET /{userReference}", ctx.ViewForeignWishlist) protectedMux.HandleFunc("POST /{$}", ctx.HomePost)
protectedMux.HandleFunc("POST /{userReference}/update", ctx.UpdateForeignWishlist) protectedMux.HandleFunc("GET /list/{userReference}", ctx.ForeignWishlist)
protectedMux.HandleFunc("POST /wishlist/add", ctx.WishlistAdd) protectedMux.HandleFunc("POST /list/{userReference}", ctx.ForeignWishlistPost)
protectedMux.HandleFunc("POST /wishlist/delete", ctx.WishlistDelete)
protectedMux.HandleFunc("POST /todo/update", ctx.TodoUpdate)
protectedMux.HandleFunc("POST /logout", authMiddleware.LogoutPost) protectedMux.HandleFunc("POST /logout", authMiddleware.LogoutPost)
http.Handle("/", authMiddleware) http.Handle("/", authMiddleware)

View File

@ -32,7 +32,7 @@
<div class="card-body"> <div class="card-body">
<h2>{{.Username}}'s list</h2> <h2>{{.Username}}'s list</h2>
{{with .Gifts}} {{with .Gifts}}
<form method="post" action="/{{$.UserReference}}/update" autocomplete="off" <form method="post" autocomplete="off"
onchange="acceptNames(this, 'claimSubmit', 'claimed', 'unclaimed'); acceptNames(this, 'completeSubmit', 'claimed'); acceptAttribute(this, 'deleteSubmit', 'data-deletable')"> onchange="acceptNames(this, 'claimSubmit', 'claimed', 'unclaimed'); acceptNames(this, 'completeSubmit', 'claimed'); acceptAttribute(this, 'deleteSubmit', 'data-deletable')">
<ul class="list-group mb-3"> <ul class="list-group mb-3">
{{range .}} {{range .}}
@ -82,7 +82,7 @@
<p>They don't have any gift ideas. Ask them to think of something, or add an idea yourself! 👇 (everyone <p>They don't have any gift ideas. Ask them to think of something, or add an idea yourself! 👇 (everyone
except them will be able to see it and claim it)</p> except them will be able to see it and claim it)</p>
{{end}} {{end}}
<form method="post" action="/{{$.UserReference}}/update"> <form method="post">
<div class="input-group mt-3"> <div class="input-group mt-3">
<input class="form-control" name="gift_name" <input class="form-control" name="gift_name"
placeholder="This will be invisible to {{.Username}}, but everyone else will be able to see it and possibly claim it." placeholder="This will be invisible to {{.Username}}, but everyone else will be able to see it and possibly claim it."

View File

@ -2,7 +2,7 @@
<div class="flex-grow-1"></div> <div class="flex-grow-1"></div>
<ul class="navbar-nav"> <ul class="navbar-nav">
<li class="nav-item"><button class="btn btn-success" <li class="nav-item"><button class="btn btn-success"
onclick="navigator.clipboard.writeText('{{.HostUrl}}/{{.Reference}}'); alert('The share link to your wishlist has been copied to your clipboard. Anyone with the link will be able to claim gifts for you. Share it with someone!');">Copy onclick="navigator.clipboard.writeText('{{.HostUrl}}/list/{{.Reference}}'); alert('The share link to your wishlist has been copied to your clipboard. Anyone with the link will be able to claim gifts for you. Share it with someone!');">Copy
share link</button></li> share link</button></li>
<li class="nav-item"> <li class="nav-item">
<div class="dropdown"> <div class="dropdown">
@ -28,8 +28,7 @@
<div class="card-body"> <div class="card-body">
<h2>Your wishlist</h2> <h2>Your wishlist</h2>
{{with .Gifts}} {{with .Gifts}}
<form method="post" action="/wishlist/delete" onchange="acceptNames(this, 'deleteSubmit', 'gift')" <form method="post" onchange="acceptNames(this, 'deleteSubmit', 'gift')" autocomplete="off">
autocomplete="off">
<ul class="list-group mb-3"> <ul class="list-group mb-3">
{{range .}} {{range .}}
<li class="list-group-item"> <li class="list-group-item">
@ -40,16 +39,16 @@
</li> </li>
{{end}} {{end}}
</ul> </ul>
<button id="deleteSubmit" class="btn btn-danger mb-3" type="submit" name="mode" value="delete" <button id="deleteSubmit" class="btn btn-danger mb-3" type="submit" name="intent" value="delete_idea"
disabled>Delete</button> disabled>Delete</button>
</form> </form>
{{else}} {{else}}
<p>Your list is empty. Think of some things to add!</p> <p>Your list is empty. Think of some things to add!</p>
{{end}} {{end}}
<form method="post" action="/wishlist/add"> <form method="post">
<div class="input-group"> <div class="input-group">
<input class="form-control" name="gift_name" required> <input class="form-control" name="gift_name" required>
<button class="btn btn-primary" type="submit">Add gift idea</button> <button class="btn btn-primary" type="submit" name="intent" value="add_idea">Add gift idea</button>
</div> </div>
</form> </form>
</div> </div>
@ -59,7 +58,7 @@
<section class="card"> <section class="card">
<div class="card-body"> <div class="card-body">
<h2>Your todo list</h2> <h2>Your todo list</h2>
<form method="post" action="/todo/update" <form method="post"
onchange="acceptNames(this, 'unclaimSubmit', 'gift'); acceptNames(this, 'completeSubmit', 'gift')" onchange="acceptNames(this, 'unclaimSubmit', 'gift'); acceptNames(this, 'completeSubmit', 'gift')"
autocomplete="off"> autocomplete="off">
<ul class="list-group mb-3"> <ul class="list-group mb-3">
@ -77,14 +76,14 @@
</em> </em>
</label> </label>
<span id="todo_detail_{{.Id}}"> <span id="todo_detail_{{.Id}}">
for <a href="/{{.RecipientRef}}">{{.RecipientName}}</a> for <a href="/list/{{.RecipientRef}}">{{.RecipientName}}</a>
</span> </span>
</li> </li>
{{end}} {{end}}
</ul> </ul>
<button id="unclaimSubmit" class="btn btn-warning" type="submit" name="mode" value="unclaim" <button id="unclaimSubmit" class="btn btn-warning" type="submit" name="intent" value="unclaim_todo"
disabled>Unclaim</button> disabled>Unclaim</button>
<button id="completeSubmit" class="btn btn-success" type="submit" name="mode" value="complete" <button id="completeSubmit" class="btn btn-success" type="submit" name="mode" value="complete_todo"
disabled>Complete</button> disabled>Complete</button>
</form> </form>
</div> </div>