lishwist/server/routing/foreign_wishlist.go

57 lines
2.1 KiB
Go

package routing
import (
"lishwist/api/db"
"lishwist/rsvp"
"net/http"
)
type foreignWishlistProps struct {
CurrentUserId string
CurrentUserName string
Username string
Gifts []db.Gift
}
func ForeignWishlist(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
userReference := r.PathValue("userReference")
if currentUser.Reference == userReference {
return rsvp.SeeOther("/")
}
otherUser, err := db.GetUserByReference(userReference)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(").Log("Couldn't get user by reference %q: %s", userReference, err)
}
if otherUser == nil {
return rsvp.Error(http.StatusInternalServerError, "User not found")
}
gifts, err := currentUser.GetOtherUserGifts(userReference)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(").Log("%q couldn't get wishes of other user %q: %s", currentUser.Name, otherUser.Name, err)
}
p := foreignWishlistProps{CurrentUserId: currentUser.Id, CurrentUserName: currentUser.Name, Username: otherUser.Name, Gifts: gifts}
return rsvp.Data("foreign_wishlist.gotmpl", p)
}
type publicWishlistProps struct {
Username string
GiftCount int
}
func PublicWishlist(h http.Header, r *rsvp.Request) rsvp.Response {
userReference := r.PathValue("userReference")
otherUser, err := db.GetUserByReference(userReference)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(").Log("Couldn't get user by reference %q on public wishlist: %s", userReference, err)
}
if otherUser == nil {
return rsvp.Error(http.StatusInternalServerError, "User not found")
}
giftCount, err := otherUser.CountGifts()
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(").Log("Couldn't get wishes of user %q on public wishlist: %s", otherUser.Name, err)
}
p := publicWishlistProps{Username: otherUser.Name, GiftCount: giftCount}
return rsvp.Data("public_foreign_wishlist.gotmpl", p)
}