lishwist/http/routing/wishlist.go

69 lines
2.1 KiB
Go

package routing
import (
"lishwist/http/api/db"
"lishwist/http/rsvp"
"net/http"
)
func WishlistAdd(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
form := r.ParseForm()
newGiftName := form.Get("gift_name")
err := currentUser.AddGift(newGiftName)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to add gift.").LogError(err)
}
return rsvp.SeeOther("/")
}
func WishlistDelete(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
form := r.ParseForm()
targets := form["gift"]
err := currentUser.RemoveGifts(targets...)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to remove gifts.").LogError(err)
}
return rsvp.SeeOther("/")
}
func ForeignWishlistPost(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
form := r.ParseForm()
userReference := r.PathValue("userReference")
intent := form.Get("intent")
switch intent {
case "claim":
claims := form["unclaimed"]
unclaims := form["claimed"]
err := currentUser.ClaimGifts(claims, unclaims)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to update claim...").LogError(err)
}
case "complete":
claims := form["claimed"]
err := currentUser.CompleteGifts(claims)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to complete gifts...").LogError(err)
}
case "add":
giftName := form.Get("gift_name")
if giftName == "" {
return rsvp.Error(http.StatusBadRequest, "Gift name not provided")
}
err := currentUser.AddGiftToUser(userReference, giftName)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to add gift idea to other user...").LogError(err)
}
case "delete":
claims := form["unclaimed"]
unclaims := form["claimed"]
gifts := append(claims, unclaims...)
err := currentUser.RemoveGifts(gifts...)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to remove gift idea for other user...").LogError(err)
}
default:
return rsvp.Error(http.StatusBadRequest, "Invalid intent %q", intent)
}
return rsvp.SeeOther("/list/" + userReference)
}