96 lines
2.7 KiB
Go
96 lines
2.7 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
|
|
}
|
|
targets := r.Form["gift"]
|
|
err := user.RemoveGifts(targets...)
|
|
if err != nil {
|
|
log.Printf("Failed to remove gifts: %s\n", err)
|
|
http.Error(w, "Failed to remove gifts.", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|
|
|
|
func (ctx *Context) UpdateForeignWishlist(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
|
|
}
|
|
userReference := r.PathValue("userReference")
|
|
switch r.Form.Get("intent") {
|
|
case "claim":
|
|
claims := r.Form["unclaimed"]
|
|
unclaims := r.Form["claimed"]
|
|
err := user.ClaimGifts(claims, unclaims)
|
|
if err != nil {
|
|
http.Error(w, "Failed to update claim...", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
case "complete":
|
|
claims := r.Form["claimed"]
|
|
err := user.CompleteGifts(claims)
|
|
if err != nil {
|
|
log.Printf("Failed to complete gifts: %s\n", err)
|
|
http.Error(w, "Failed to complete gifts...", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
case "add":
|
|
giftName := r.Form.Get("gift_name")
|
|
if giftName == "" {
|
|
http.Error(w, "Gift name not provided", http.StatusBadRequest)
|
|
return
|
|
}
|
|
err := user.AddGiftToUser(userReference, giftName)
|
|
if err != nil {
|
|
log.Printf("Failed to add gift idea to other user: %s\n", err)
|
|
http.Error(w, "Failed to add gift idea to other user...", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
case "delete":
|
|
claims := r.Form["unclaimed"]
|
|
unclaims := r.Form["claimed"]
|
|
gifts := append(claims, unclaims...)
|
|
err := user.RemoveGifts(gifts...)
|
|
if err != nil {
|
|
log.Printf("Failed to remove gift idea for other user: %s\n", err)
|
|
http.Error(w, "Failed to remove gift idea for other user...", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
default:
|
|
http.Error(w, "Invalid intent", http.StatusBadRequest)
|
|
}
|
|
http.Redirect(w, r, "/"+userReference, http.StatusSeeOther)
|
|
}
|