lishwist/context/context.go

48 lines
1.1 KiB
Go

package context
import (
"lishwist/auth"
"log"
"net/http"
)
type Context struct {
Auth *auth.AuthMiddleware
}
func (ctx *Context) WishlistAdd(w http.ResponseWriter, r *http.Request) {
user := ctx.Auth.ExpectUser(r)
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
newGiftName := r.Form.Get("gift_name")
err := user.AddGift(newGiftName)
if err != nil {
log.Printf("Failed to add gift: %s\n", err)
http.Error(w, "Failed to add gift.", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (ctx *Context) WishlistDelete(w http.ResponseWriter, r *http.Request) {
user := ctx.Auth.ExpectUser(r)
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
target := r.Form.Get("gift_id")
if target == "" {
http.Error(w, "Gift ID not provided"+target, http.StatusBadRequest)
return
}
err := user.RemoveGift(target)
if err != nil {
log.Printf("Failed to remove gift: %s\n", err)
http.Error(w, "Failed to remove gift.", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}