lishwist/context/foreign_wishlist.go

44 lines
1.3 KiB
Go

package context
import (
"lishwist/db"
"lishwist/templates"
"log"
"net/http"
)
type ForeignWishlistProps struct {
CurrentUserId string
CurrentUserName string
Username string
UserReference string
Gifts []db.Gift
}
func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) {
userReference := r.PathValue("userReference")
user := ctx.Auth.ExpectUser(r)
if user.Reference == userReference {
http.Error(w, "You can't view your own list, silly ;)", http.StatusForbidden)
return
}
otherUser, err := db.GetUserByReference(userReference)
if err != nil {
log.Printf("An error occurred while fetching a user: %s\n", err)
http.Error(w, "An error occurred while fetching this user :(", http.StatusInternalServerError)
return
}
if otherUser == nil {
http.Error(w, "User not found", http.StatusNotFound)
return
}
gifts, err := otherUser.GetGifts()
if err != nil {
log.Printf("An error occurred while fetching %s's wishlist: %s\n", otherUser.Name, err)
http.Error(w, "An error occurred while fetching this user's wishlist :(", http.StatusInternalServerError)
return
}
p := ForeignWishlistProps{CurrentUserId: user.Id, CurrentUserName: user.Name, Username: otherUser.Name, UserReference: userReference, Gifts: gifts}
templates.Execute(w, "foreign_wishlist.gotmpl", p)
}