lishwist/server/routing/foreign_wishlist.go

66 lines
1.9 KiB
Go

package routing
import (
"lishwist/db"
"lishwist/error"
"lishwist/templates"
"net/http"
)
type foreignWishlistProps struct {
CurrentUserId string
CurrentUserName string
Username string
Gifts []db.Gift
}
func (ctx *Context) ForeignWishlist(w http.ResponseWriter, r *http.Request) {
userReference := r.PathValue("userReference")
user := ctx.ExpectUser(r)
if user.Reference == userReference {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
otherUser, err := db.GetUserByReference(userReference)
if err != nil {
error.Page(w, "An error occurred while fetching this user :(", http.StatusInternalServerError, err)
return
}
if otherUser == nil {
error.Page(w, "User not found", http.StatusNotFound, err)
return
}
gifts, err := user.GetOtherUserGifts(userReference)
if err != nil {
error.Page(w, "An error occurred while fetching this user's wishlist :(", http.StatusInternalServerError, err)
return
}
p := foreignWishlistProps{CurrentUserId: user.Id, CurrentUserName: user.Name, Username: otherUser.Name, Gifts: gifts}
templates.Execute(w, "foreign_wishlist.gotmpl", p)
}
type publicWishlistProps struct {
Username string
GiftCount int
}
func (ctx *Context) PublicWishlist(w http.ResponseWriter, r *http.Request) {
userReference := r.PathValue("userReference")
otherUser, err := db.GetUserByReference(userReference)
if err != nil {
error.Page(w, "An error occurred while fetching this user :(", http.StatusInternalServerError, err)
return
}
if otherUser == nil {
error.Page(w, "User not found", http.StatusNotFound, err)
return
}
giftCount, err := otherUser.CountGifts()
if err != nil {
error.Page(w, "An error occurred while fetching data about this user :(", http.StatusInternalServerError, err)
return
}
p := publicWishlistProps{Username: otherUser.Name, GiftCount: giftCount}
templates.Execute(w, "public_foreign_wishlist.gotmpl", p)
}