68 lines
2.5 KiB
Go
68 lines
2.5 KiB
Go
package routing
|
|
|
|
import (
|
|
lishwist "lishwist/core"
|
|
"lishwist/http/response"
|
|
"lishwist/http/templates"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/Teajey/rsvp"
|
|
)
|
|
|
|
type foreignWishlistProps struct {
|
|
Navbar templates.NavCollapse
|
|
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{Navbar: templates.UserNavCollapse(user.Name, user.PasswordFromAdmin), CurrentUserId: user.Id, Username: otherUser.Name, Gifts: wishes}
|
|
return response.Data("foreign_wishlist.gotmpl", p)
|
|
}
|
|
|
|
type publicWishlistProps struct {
|
|
Navbar templates.NavCollapse
|
|
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{Navbar: templates.DefaultNavCollapse(), Username: otherUser.Name, GiftCount: giftCount}
|
|
return response.Data("public_foreign_wishlist.gotmpl", p)
|
|
}
|