Skip to content

Commit

Permalink
feat: replace sqlc with jet on events and event routes
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui committed Feb 19, 2024
1 parent 3b0094d commit 14a7aae
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 51 deletions.
2 changes: 0 additions & 2 deletions src/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ func New(app_ctx types.AppContext, querier *database.Queries, service services.S
wishes.Delete("/:event_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.DeleteWish)
wishes.Get("/:event_id/:participant_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.UseEventParticipantAuthWithParam, c.GetWishes)
}
// TODO: Remove..
server.Get("/test", c.TestRoute)
server.Get("*", func(c *fiber.Ctx) error {
return utils.ResponseWithStatusCode(c, fiber.ErrNotFound.Code, types.Errors{
Errors: []string{"resource not found"},
Expand Down
82 changes: 71 additions & 11 deletions src/controllers/events_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"time"

"github.com/giftxtrade/api/src/database"
. "github.com/giftxtrade/api/src/database/jet/postgres/public/table"
"github.com/giftxtrade/api/src/mappers"
"github.com/giftxtrade/api/src/types"
"github.com/giftxtrade/api/src/utils"
. "github.com/go-jet/jet/v2/postgres"
"github.com/gofiber/fiber/v2"
)

Expand All @@ -33,26 +35,84 @@ func (ctr *Controller) CreateEvent(c *fiber.Ctx) error {

func (ctr *Controller) GetEvents(c *fiber.Ctx) error {
auth_user := GetAuthContext(c.UserContext())
events, err := ctr.Querier.FindAllEventsWithUser(c.Context(), sql.NullInt64{
Valid: true,
Int64: auth_user.User.ID,
})

participant_user_sub_query := SELECT(
Participant.AllColumns,
User.AllColumns,
).
FROM(
Participant.
LEFT_JOIN(User, Participant.UserID.EQ(User.ID)),
).
ORDER_BY(Participant.ID.ASC()).
AsTable(Participant.TableName())

query := SELECT(
Event.AllColumns,
Link.AllColumns,
participant_user_sub_query.AllColumns(),
).FROM(
Event.
LEFT_JOIN(Link, Event.ID.EQ(Link.EventID)).
LEFT_JOIN(Participant.AS("p1"), Event.ID.EQ(Participant.AS("p1").EventID)).
LEFT_JOIN(
participant_user_sub_query,
Event.ID.EQ(Participant.EventID.From(participant_user_sub_query)),
),
).
WHERE(
Participant.AS("p1").UserID.EQ(Int(auth_user.User.ID)),
).
ORDER_BY(
Event.DrawAt.ASC(),
Event.CloseAt.ASC(),
Participant.ID.From(participant_user_sub_query).ASC(),
)

var dest []types.Event
err := query.QueryContext(c.Context(), ctr.DB, &dest)
if err != nil {
return utils.FailResponse(c, "could not return events", err.Error())
fmt.Println(query.DebugSql(), err)
return utils.FailResponse(c, "could not return events")
}

mapped_events := mappers.DbFindAllEventsWithUserRowToEvent(events)
return utils.DataResponse(c, mapped_events)
return utils.DataResponse(c, dest)
}

func (ctr *Controller) GetEventById(c *fiber.Ctx) error {
auth := GetAuthContext(c.UserContext())
event_id := GetEventIdFromContext(c.UserContext())
event_rows, err := ctr.Querier.FindEventById(c.Context(), event_id)

query := SELECT(
Event.AllColumns,
Participant.AllColumns,
User.AllColumns,
Link.AllColumns,
Wish.AllColumns,
Product.AllColumns,
).FROM(
Event.
LEFT_JOIN(Link, Event.ID.EQ(Link.EventID)).
INNER_JOIN(Participant, Event.ID.EQ(Participant.EventID)).
LEFT_JOIN(User, Participant.UserID.EQ(User.ID)).
LEFT_JOIN(
Wish,
Event.ID.EQ(Wish.EventID).
AND(
Wish.UserID.EQ(Int(auth.User.ID)),
),
).
LEFT_JOIN(Product, Wish.ProductID.EQ(Product.ID)),
).WHERE(Event.ID.EQ(Int(event_id))).ORDER_BY(
Participant.Organizer.DESC(),
Participant.Accepted.DESC(),
Participant.CreatedAt.DESC(),
)

var event types.Event
err := query.QueryContext(c.Context(), ctr.DB, &event)
if err != nil {
return utils.FailResponse(c, "could not load event")
}

event := mappers.DbFindEventByIdToEvent(event_rows)
return utils.DataResponse(c, event)
}

Expand Down
36 changes: 0 additions & 36 deletions src/controllers/test_controller.go

This file was deleted.

5 changes: 3 additions & 2 deletions src/types/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ type DeleteWish struct {
}

type Wish struct {
ID int64 `json:"id"`
ID int64 `json:"id" sql:"primary_key"`
UserID int64 `json:"userId"`
ParticipantID int64 `json:"participantId"`
ProductID int64 `json:"productId,omitempty"`
Product *Product `json:"product,omitempty"`
EventID int64 `json:"eventId"`
Quantity int32 `json:"quantity"`
Quantity int32 `json:"quantity" alias:"wish.quantity"`
}

type Participant struct {
Expand Down Expand Up @@ -162,6 +162,7 @@ type Event struct {
UpdatedAt time.Time `json:"updatedAt" alias:"event.updated_at"`
Participants []Participant `json:"participants,omitempty"`
Links []Link `json:"links,omitempty"`
MyWishList []Wish `json:"my_wish_list,omitempty"`
}

type CreateEvent struct {
Expand Down

0 comments on commit 14a7aae

Please sign in to comment.