package routing import ( lishwist "lishwist/core" "lishwist/http/file" "lishwist/http/response" "lishwist/http/session" "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 *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { userReference, _ := file.NameAtPathValue(r, "userReference") user := app.User() if user.Reference == userReference { return rsvp.Data("You're not allowed to view your own wishlist!").StatusFound("/") } otherUser, err := lishwist.GetUserByReference(userReference) if err != nil { log.Printf("Couldn't get user by reference %q: %s\n", userReference, err) return response.Error("An error occurred while fetching this user :(").StatusInternalServerError() } if otherUser == nil { return response.Error("User not found").StatusInternalServerError() } 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("An error occurred while fetching this user :(").StatusInternalServerError() } 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 Referrer string } func PublicWishlist(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { userReference, _ := file.NameAtPathValue(r, "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("An error occurred while fetching this user :(").StatusInternalServerError() } if otherUser == nil { return response.Error("User not found").StatusInternalServerError() } 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("An error occurred while fetching this user :(").StatusInternalServerError() } p := publicWishlistProps{Navbar: templates.DefaultNavCollapse(), Username: otherUser.Name, GiftCount: giftCount, Referrer: r.URL.Path} return response.Data("public_foreign_wishlist.gotmpl", p) }