Skip to content

Commit

Permalink
feat: add get participant wishes route
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui committed Feb 11, 2024
1 parent 75ca198 commit edb8528
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
20 changes: 20 additions & 0 deletions src/controllers/wishes_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 10 additions & 0 deletions src/database/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/database/queries/wish.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
70 changes: 70 additions & 0 deletions src/database/wish.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit edb8528

Please sign in to comment.