86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package routing
|
|
|
|
import (
|
|
"lishwist/error"
|
|
"net/http"
|
|
)
|
|
|
|
func (ctx *Context) WishlistAdd(w http.ResponseWriter, r *http.Request) {
|
|
user := ctx.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 {
|
|
error.Page(w, "Failed to add gift.", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|
|
|
|
func (ctx *Context) WishlistDelete(w http.ResponseWriter, r *http.Request) {
|
|
user := ctx.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 {
|
|
error.Page(w, "Failed to remove gifts.", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|
|
|
|
func (ctx *Context) ForeignWishlistPost(w http.ResponseWriter, r *http.Request) {
|
|
user := ctx.ExpectUser(r)
|
|
if err := r.ParseForm(); err != nil {
|
|
error.Page(w, "Failed to parse form...", http.StatusBadRequest, err)
|
|
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 {
|
|
error.Page(w, "Failed to update claim...", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
case "complete":
|
|
claims := r.Form["claimed"]
|
|
err := user.CompleteGifts(claims)
|
|
if err != nil {
|
|
error.Page(w, "Failed to complete gifts...", http.StatusInternalServerError, nil)
|
|
return
|
|
}
|
|
case "add":
|
|
giftName := r.Form.Get("gift_name")
|
|
if giftName == "" {
|
|
error.Page(w, "Gift name not provided", http.StatusBadRequest, nil)
|
|
return
|
|
}
|
|
err := user.AddGiftToUser(userReference, giftName)
|
|
if err != nil {
|
|
error.Page(w, "Failed to add gift idea to other user...", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
case "delete":
|
|
claims := r.Form["unclaimed"]
|
|
unclaims := r.Form["claimed"]
|
|
gifts := append(claims, unclaims...)
|
|
err := user.RemoveGifts(gifts...)
|
|
if err != nil {
|
|
error.Page(w, "Failed to remove gift idea for other user...", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
default:
|
|
http.Error(w, "Invalid intent", http.StatusBadRequest)
|
|
}
|
|
http.Redirect(w, r, "/list/"+userReference, http.StatusSeeOther)
|
|
}
|