From edb8528ddb7e625457a2ee55b7e0b0f839643e1e Mon Sep 17 00:00:00 2001 From: ayaanqui Date: Sat, 10 Feb 2024 20:20:22 -0600 Subject: [PATCH] feat: add get participant wishes route --- src/controllers/controller.go | 5 ++ src/controllers/wishes_controller.go | 20 ++++++++ src/database/db.go | 10 ++++ src/database/queries/wish.sql | 12 +++++ src/database/wish.sql.go | 70 ++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) diff --git a/src/controllers/controller.go b/src/controllers/controller.go index a0530004..babcd5cf 100644 --- a/src/controllers/controller.go +++ b/src/controllers/controller.go @@ -72,6 +72,7 @@ func New(app_ctx types.AppContext, querier *database.Queries, service services.S { wishes.Post("/:event_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.CreateWish) wishes.Delete("/:event_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.DeleteWish) + wishes.Get("/:event_id/:participant_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.UseEventParticipantAuthWithParam, c.GetWishes) } server.Get("*", func(c *fiber.Ctx) error { return utils.ResponseWithStatusCode(c, fiber.ErrNotFound.Code, types.Errors{ @@ -105,3 +106,7 @@ func GetEventIdFromContext(user_context context.Context) int64 { id := user_context.Value(EVENT_ID_PARAM_KEY).(int64) return id } + +func GetParticipantFromContext(user_context context.Context) database.Participant { + return user_context.Value(PARTICIPANT_OB_KEY).(database.Participant) +} diff --git a/src/controllers/wishes_controller.go b/src/controllers/wishes_controller.go index f93102c8..ae7d133c 100644 --- a/src/controllers/wishes_controller.go +++ b/src/controllers/wishes_controller.go @@ -87,3 +87,23 @@ func (ctr *Controller) DeleteWish(c *fiber.Ctx) error { } return utils.DataResponse(c, mappers.DbWishToWish(wish, nil)) } + +func (ctr *Controller) GetWishes(c *fiber.Ctx) error { + auth := GetAuthContext(c.UserContext()) + event_id := GetEventIdFromContext(c.UserContext()) + participant := GetParticipantFromContext(c.UserContext()) + wishes, err := ctr.Querier.GetAllWishesForUser(c.Context(), database.GetAllWishesForUserParams{ + UserID: auth.User.ID, + EventID: event_id, + ParticipantID: participant.ID, + }) + if err != nil { + return utils.FailResponse(c, "could not fetch wishes") + } + + mapped_wishes := make([]types.Wish, len(wishes)) + for i, w := range wishes { + mapped_wishes[i] = mappers.DbWishToWish(w.Wish, &w.Product) + } + return utils.DataResponse(c, mapped_wishes) +} diff --git a/src/database/db.go b/src/database/db.go index 1af22b66..c3a376d3 100644 --- a/src/database/db.go +++ b/src/database/db.go @@ -111,6 +111,9 @@ func Prepare(ctx context.Context, db DBTX) (*Queries, error) { if q.findUserByIdOrEmailStmt, err = db.PrepareContext(ctx, findUserByIdOrEmail); err != nil { return nil, fmt.Errorf("error preparing query FindUserByIdOrEmail: %w", err) } + if q.getAllWishesForUserStmt, err = db.PrepareContext(ctx, getAllWishesForUser); err != nil { + return nil, fmt.Errorf("error preparing query GetAllWishesForUser: %w", err) + } if q.getWishByAllIDsStmt, err = db.PrepareContext(ctx, getWishByAllIDs); err != nil { return nil, fmt.Errorf("error preparing query GetWishByAllIDs: %w", err) } @@ -291,6 +294,11 @@ func (q *Queries) Close() error { err = fmt.Errorf("error closing findUserByIdOrEmailStmt: %w", cerr) } } + if q.getAllWishesForUserStmt != nil { + if cerr := q.getAllWishesForUserStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getAllWishesForUserStmt: %w", cerr) + } + } if q.getWishByAllIDsStmt != nil { if cerr := q.getWishByAllIDsStmt.Close(); cerr != nil { err = fmt.Errorf("error closing getWishByAllIDsStmt: %w", cerr) @@ -409,6 +417,7 @@ type Queries struct { findUserByIdStmt *sql.Stmt findUserByIdAndEmailStmt *sql.Stmt findUserByIdOrEmailStmt *sql.Stmt + getAllWishesForUserStmt *sql.Stmt getWishByAllIDsStmt *sql.Stmt patchParticipantStmt *sql.Stmt setUserAsAdminStmt *sql.Stmt @@ -454,6 +463,7 @@ func (q *Queries) WithTx(tx *sql.Tx) *Queries { findUserByIdStmt: q.findUserByIdStmt, findUserByIdAndEmailStmt: q.findUserByIdAndEmailStmt, findUserByIdOrEmailStmt: q.findUserByIdOrEmailStmt, + getAllWishesForUserStmt: q.getAllWishesForUserStmt, getWishByAllIDsStmt: q.getWishByAllIDsStmt, patchParticipantStmt: q.patchParticipantStmt, setUserAsAdminStmt: q.setUserAsAdminStmt, diff --git a/src/database/queries/wish.sql b/src/database/queries/wish.sql index 488b0bbc..810bfbed 100644 --- a/src/database/queries/wish.sql +++ b/src/database/queries/wish.sql @@ -23,3 +23,15 @@ WHERE user_id = $2 AND participant_id = $3 AND event_id = $4; + +-- name: GetAllWishesForUser :many +SELECT + sqlc.embed(wish), + sqlc.embed(product) +FROM wish +INNER JOIN product ON product.id = wish.product_id +WHERE + wish.user_id = $1 AND + wish.participant_id = $2 AND + wish.event_id = $3 +ORDER BY wish.created_at DESC; diff --git a/src/database/wish.sql.go b/src/database/wish.sql.go index 21240921..20280fff 100644 --- a/src/database/wish.sql.go +++ b/src/database/wish.sql.go @@ -62,6 +62,76 @@ func (q *Queries) DeleteWish(ctx context.Context, id int64) (int64, error) { return id, err } +const getAllWishesForUser = `-- name: GetAllWishesForUser :many +SELECT + wish.id, wish.user_id, wish.participant_id, wish.product_id, wish.event_id, wish.created_at, wish.updated_at, + product.id, product.title, product.description, product.product_key, product.image_url, product.total_reviews, product.rating, product.price, product.currency, product.url, product.category_id, product.created_at, product.updated_at, product.product_ts, product.origin +FROM wish +INNER JOIN product ON product.id = wish.product_id +WHERE + wish.user_id = $1 AND + wish.participant_id = $2 AND + wish.event_id = $3 +ORDER BY wish.created_at DESC +` + +type GetAllWishesForUserParams struct { + UserID int64 `db:"user_id" json:"userId"` + ParticipantID int64 `db:"participant_id" json:"participantId"` + EventID int64 `db:"event_id" json:"eventId"` +} + +type GetAllWishesForUserRow struct { + Wish Wish `db:"wish" json:"wish"` + Product Product `db:"product" json:"product"` +} + +func (q *Queries) GetAllWishesForUser(ctx context.Context, arg GetAllWishesForUserParams) ([]GetAllWishesForUserRow, error) { + rows, err := q.query(ctx, q.getAllWishesForUserStmt, getAllWishesForUser, arg.UserID, arg.ParticipantID, arg.EventID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetAllWishesForUserRow + for rows.Next() { + var i GetAllWishesForUserRow + if err := rows.Scan( + &i.Wish.ID, + &i.Wish.UserID, + &i.Wish.ParticipantID, + &i.Wish.ProductID, + &i.Wish.EventID, + &i.Wish.CreatedAt, + &i.Wish.UpdatedAt, + &i.Product.ID, + &i.Product.Title, + &i.Product.Description, + &i.Product.ProductKey, + &i.Product.ImageUrl, + &i.Product.TotalReviews, + &i.Product.Rating, + &i.Product.Price, + &i.Product.Currency, + &i.Product.Url, + &i.Product.CategoryID, + &i.Product.CreatedAt, + &i.Product.UpdatedAt, + &i.Product.ProductTs, + &i.Product.Origin, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getWishByAllIDs = `-- name: GetWishByAllIDs :one SELECT id, user_id, participant_id, product_id, event_id, created_at, updated_at FROM wish