lishwist/http/routing/wishlist.go

69 lines
2.1 KiB
Go

package routing
import (
lishwist "lishwist/core"
"lishwist/http/rsvp"
"net/http"
)
func WishlistAdd(app *lishwist.Session, h http.Header, r *rsvp.Request) rsvp.Response {
form := r.ParseForm()
newGiftName := form.Get("gift_name")
err := app.MakeWish(newGiftName)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to add gift.").LogError(err)
}
return rsvp.SeeOther("/")
}
func WishlistDelete(app *lishwist.Session, h http.Header, r *rsvp.Request) rsvp.Response {
form := r.ParseForm()
targets := form["gift"]
err := app.RevokeWishes(targets...)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to remove gifts.").LogError(err)
}
return rsvp.SeeOther("/")
}
func ForeignWishlistPost(app *lishwist.Session, 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 := app.ClaimWishes(claims, unclaims)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to update claim...").LogError(err)
}
case "complete":
claims := form["claimed"]
err := app.CompleteWishes(claims)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to complete gifts...").LogError(err)
}
case "add":
wishName := form.Get("gift_name")
if wishName == "" {
return rsvp.Error(http.StatusBadRequest, "Gift name not provided")
}
err := app.SuggestWishForUser(userReference, wishName)
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 := app.RecindWishesForUser(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)
}