96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
package routing
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/Teajey/rsvp"
|
|
|
|
lishwist "lishwist/core"
|
|
"lishwist/http/response"
|
|
)
|
|
|
|
func WishlistAdd(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return response.Error(http.StatusBadRequest, "Failed to parse form")
|
|
}
|
|
|
|
newGiftName := r.Form.Get("gift_name")
|
|
_, err = app.MakeWish(newGiftName)
|
|
if err != nil {
|
|
log.Printf("%s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "Failed to add gift.")
|
|
}
|
|
return rsvp.SeeOther("/", "Wish added!")
|
|
}
|
|
|
|
func WishlistDelete(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return response.Error(http.StatusBadRequest, "Failed to parse form")
|
|
}
|
|
|
|
targets := r.Form["gift"]
|
|
err = app.RevokeWishes(targets...)
|
|
if err != nil {
|
|
log.Printf("%s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "Failed to remove gifts.")
|
|
}
|
|
return rsvp.SeeOther("/", "Wish deleted")
|
|
}
|
|
|
|
func ForeignWishlistPost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return response.Error(http.StatusBadRequest, "Failed to parse form")
|
|
}
|
|
|
|
userReference := r.PathValue("userReference")
|
|
resp := rsvp.SeeOther("/lists/"+userReference, "Update successful")
|
|
intent := r.Form.Get("intent")
|
|
switch intent {
|
|
case "claim":
|
|
claims := r.Form["unclaimed"]
|
|
unclaims := r.Form["claimed"]
|
|
err := app.ClaimWishes(claims, unclaims)
|
|
if err != nil {
|
|
log.Printf("%s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "Failed to update claim...")
|
|
}
|
|
resp.Body = "Successfully claimed wishes"
|
|
case "complete":
|
|
claims := r.Form["claimed"]
|
|
err := app.CompleteWishes(claims)
|
|
if err != nil {
|
|
log.Printf("%s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "Failed to complete gifts...")
|
|
}
|
|
resp.Body = "Successfully completed wishes"
|
|
case "add":
|
|
wishName := r.Form.Get("gift_name")
|
|
if wishName == "" {
|
|
return response.Error(http.StatusBadRequest, "Gift name not provided")
|
|
}
|
|
err := app.SuggestWishForUser(userReference, wishName)
|
|
if err != nil {
|
|
log.Printf("%s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "Failed to add gift idea to other user...")
|
|
}
|
|
resp.Body = "Successfully added wishes"
|
|
case "delete":
|
|
claims := r.Form["unclaimed"]
|
|
unclaims := r.Form["claimed"]
|
|
gifts := append(claims, unclaims...)
|
|
err := app.RecindWishesForUser(gifts...)
|
|
if err != nil {
|
|
log.Printf("%s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "Failed to remove gift idea for other user...")
|
|
}
|
|
resp.Body = "Successfully removed wishes"
|
|
default:
|
|
return response.Error(http.StatusBadRequest, "Invalid intent %q", intent)
|
|
}
|
|
return resp
|
|
}
|