lishwist/http/routing/foreign_wishlist.go

65 lines
2.3 KiB
Go

package routing
import (
lishwist "lishwist/core"
"lishwist/http/response"
"log"
"net/http"
"github.com/Teajey/rsvp"
)
type foreignWishlistProps struct {
CurrentUserId string
CurrentUserName string
Username string
Gifts []lishwist.Wish
}
func ForeignWishlist(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
userReference := r.PathValue("userReference")
user := app.User()
if user.Reference == userReference {
return rsvp.Found("/", "You're not allowed to view your own wishlist!")
}
otherUser, err := lishwist.GetUserByReference(userReference)
if err != nil {
log.Printf("Couldn't get user by reference %q: %s\n", userReference, err)
return response.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(")
}
if otherUser == nil {
return response.Error(http.StatusInternalServerError, "User not found")
}
wishes, err := app.GetOthersWishes(userReference)
if err != nil {
log.Printf("%q couldn't get wishes of other user %q: %s\n", user.Name, otherUser.Name, err)
return response.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(")
}
p := foreignWishlistProps{CurrentUserId: user.Id, CurrentUserName: user.Name, Username: otherUser.Name, Gifts: wishes}
return response.Data("foreign_wishlist.gotmpl", p)
}
type publicWishlistProps struct {
Username string
GiftCount int
}
func PublicWishlist(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
userReference := r.PathValue("userReference")
otherUser, err := lishwist.GetUserByReference(userReference)
if err != nil {
log.Printf("Couldn't get user by reference %q on public wishlist: %s\n", userReference, err)
return response.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(")
}
if otherUser == nil {
return response.Error(http.StatusInternalServerError, "User not found")
}
giftCount, err := otherUser.WishCount()
if err != nil {
log.Printf("Couldn't get wishes of user %q on public wishlist: %s\n", otherUser.Name, err)
return response.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(")
}
p := publicWishlistProps{Username: otherUser.Name, GiftCount: giftCount}
return response.Data("public_foreign_wishlist.gotmpl", p)
}