diff --git a/context/context.go b/context/context.go index 4de0ad3..36dc5b6 100644 --- a/context/context.go +++ b/context/context.go @@ -49,7 +49,7 @@ func (ctx *Context) UpdateForeignWishlist(w http.ResponseWriter, r *http.Request return } userReference := r.PathValue("userReference") - switch r.Form.Get("mode") { + switch r.Form.Get("intent") { case "claim": claims := r.Form["unclaimed"] unclaims := r.Form["claimed"] @@ -66,8 +66,20 @@ func (ctx *Context) UpdateForeignWishlist(w http.ResponseWriter, r *http.Request http.Error(w, "Failed to complete gifts...", http.StatusInternalServerError) return } + case "add": + giftName := r.Form.Get("gift_name") + if giftName == "" { + http.Error(w, "Gift name not provided", http.StatusBadRequest) + return + } + err := user.AddGiftToUser(userReference, giftName) + if err != nil { + log.Printf("Failed to add gift idea to other user: %s\n", err) + http.Error(w, "Failed to add gift idea to other user...", http.StatusInternalServerError) + return + } default: - http.Error(w, "Invalid mode", http.StatusBadRequest) + http.Error(w, "Invalid intent", http.StatusBadRequest) } http.Redirect(w, r, "/"+userReference, http.StatusSeeOther) } diff --git a/context/foreign_wishlist.go b/context/foreign_wishlist.go index e6c487a..ef8b84c 100644 --- a/context/foreign_wishlist.go +++ b/context/foreign_wishlist.go @@ -32,7 +32,7 @@ func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) http.Error(w, "User not found", http.StatusNotFound) return } - gifts, err := otherUser.GetGifts() + gifts, err := user.GetOtherUserGifts(userReference) 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) diff --git a/db/user.go b/db/user.go index 8d3581b..26f9883 100644 --- a/db/user.go +++ b/db/user.go @@ -19,8 +19,11 @@ type Gift struct { ClaimantId string ClaimantName string Sent bool + RecipientId string RecipientName string RecipientRef string + CreatorId string + CreatorName string } func queryForUser(query string, args ...any) (*User, error) { @@ -83,7 +86,7 @@ func (u *User) GetPassHash() ([]byte, error) { } func (u *User) GetGifts() ([]Gift, error) { - stmt := "SELECT gift.id, gift.name, claimant.id, claimant.name, gift.sent FROM gift JOIN user ON gift.recipient_id = user.id LEFT JOIN user AS claimant ON gift.claimant_id = claimant.id WHERE user.id = ?" + stmt := "SELECT gift.id, gift.name, claimant.id, claimant.name, gift.sent FROM gift JOIN user ON gift.recipient_id = user.id LEFT JOIN user AS claimant ON gift.claimant_id = claimant.id WHERE gift.creator_id = user.id AND user.id = ?" rows, err := database.Query(stmt, u.Id) if err != nil { return nil, err @@ -93,15 +96,18 @@ func (u *User) GetGifts() ([]Gift, error) { for rows.Next() { var id string var name string - var claimantId string - var claimantName string + var claimantId sql.NullString + var claimantName sql.NullString var sent bool - rows.Scan(&id, &name, &claimantId, &claimantName, &sent) + err = rows.Scan(&id, &name, &claimantId, &claimantName, &sent) + if err != nil { + return nil, err + } gift := Gift{ Id: id, Name: name, - ClaimantId: claimantId, - ClaimantName: claimantName, + ClaimantId: claimantId.String, + ClaimantName: claimantName.String, Sent: sent, } gifts = append(gifts, gift) @@ -113,6 +119,53 @@ func (u *User) GetGifts() ([]Gift, error) { return gifts, nil } +func (u *User) GetOtherUserGifts(otherUserReference string) ([]Gift, error) { + otherUser, err := GetUserByReference(otherUserReference) + if err != nil { + return nil, err + } + if otherUser.Id == u.Id { + return nil, fmt.Errorf("Not allowed to view own foreign wishlist") + } + stmt := "SELECT gift.id, gift.name, claimant.id, claimant.name, gift.sent, gift.creator_id, creator.name, gift.recipient_id FROM gift JOIN user ON gift.recipient_id = user.id LEFT JOIN user AS claimant ON gift.claimant_id = claimant.id LEFT JOIN user AS creator ON gift.creator_id = creator.id WHERE user.id = ?" + rows, err := database.Query(stmt, otherUser.Id) + if err != nil { + return nil, err + } + defer rows.Close() + gifts := []Gift{} + for rows.Next() { + var id string + var name string + var claimantId sql.NullString + var claimantName sql.NullString + var sent bool + var creatorId string + var creatorName string + var recipientId string + err = rows.Scan(&id, &name, &claimantId, &claimantName, &sent, &creatorId, &creatorName, &recipientId) + if err != nil { + return nil, err + } + gift := Gift{ + Id: id, + Name: name, + ClaimantId: claimantId.String, + ClaimantName: claimantName.String, + Sent: sent, + CreatorId: creatorId, + CreatorName: creatorName, + RecipientId: recipientId, + } + gifts = append(gifts, gift) + } + err = rows.Err() + if err != nil { + return nil, err + } + return gifts, nil +} + func (u *User) GetTodo() ([]Gift, error) { stmt := "SELECT gift.id, gift.name, gift.sent, recipient.name, recipient.reference FROM gift JOIN user ON gift.claimant_id = user.id JOIN user AS recipient ON gift.recipient_id = recipient.id WHERE user.id = ? ORDER BY gift.sent ASC, gift.name" rows, err := database.Query(stmt, u.Id) @@ -289,3 +342,16 @@ func (u *User) CompleteGifts(claims []string) error { err = tx.Commit() return err } + +func (u *User) AddGiftToUser(otherUserReference string, giftName string) error { + otherUser, err := GetUserByReference(otherUserReference) + if err != nil { + return err + } + stmt := "INSERT INTO gift (name, recipient_id, creator_id) VALUES (?, ?, ?)" + _, err = database.Exec(stmt, giftName, otherUser.Id, u.Id) + if err != nil { + return err + } + return nil +} diff --git a/templates/foreign_wishlist.gotmpl b/templates/foreign_wishlist.gotmpl index 6c351da..7cc9ffa 100644 --- a/templates/foreign_wishlist.gotmpl +++ b/templates/foreign_wishlist.gotmpl @@ -34,9 +34,9 @@ {{with .Gifts}}
- -
{{else}} -

They haven't wished for anything. Ask them to think of some stuff!

+

They don't have any gift ideas. Ask them to think of something, or add an idea yourself! 👇 (everyone + except them will be able to see it and claim it)

{{end}} +
+
+ + +
+