From afbdf1cd178dbde773a4477f148eab73e4299f8b Mon Sep 17 00:00:00 2001 From: Mave Hur Date: Tue, 2 Apr 2024 01:02:23 -0400 Subject: [PATCH] add ordering a list of events by event time --- .github/workflows/go.yml | 2 +- go.mod | 2 +- main.go | 8 ++--- store/event.go | 69 ++++++++++++++++++++++------------------ 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c8676f5..a20d09c 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Go version uses: actions/setup-go@v4 with: - go-version: '1.21' + go-version: '1.21.3' - name: Build Go files in every directory recursively run: go build -v ./... diff --git a/go.mod b/go.mod index 4816b89..0b309f1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module blood-for-life-backend -go 1.21.3 +go 1.21 require github.com/labstack/echo v3.3.10+incompatible diff --git a/main.go b/main.go index 9fe726e..b15c6a1 100644 --- a/main.go +++ b/main.go @@ -90,16 +90,16 @@ func bind(e *echo.Echo, eventStore store.EventStore) { e.GET("/api/view-events", func(c echo.Context) error { var request apimodels.ViewEvents - bindErr := c.Bind(&request) - if bindErr != nil { - return c.JSON(http.StatusBadRequest, bindErr) + if err := c.Bind(&request); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request"}) } events, err := eventStore.ViewEvents(c.Request().Context(), request.UserID) if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to retrieve events"}) } + return c.JSON(http.StatusOK, events) }) diff --git a/store/event.go b/store/event.go index 47b0577..578da20 100644 --- a/store/event.go +++ b/store/event.go @@ -27,7 +27,7 @@ type EventStore interface { Create(ctx context.Context, name string, description string, start time.Time, end time.Time, volunteers int, location string) (*Event, error) Update(ctx context.Context, event Event) (*Event, error) Delete(ctx context.Context, id int) error - ViewEvents(ctx context.Context, UserID int) error + ViewEvents(ctx context.Context, UserID int) ([]Event, error) } type pgEventStore struct { @@ -117,39 +117,46 @@ func (s *pgEventStore) Delete(ctx context.Context, id int) error { // retrieves events for the given user and indicates if they are registered or on standby. func (s *pgEventStore) ViewEvents(ctx context.Context, userID int) ([]Event, error) { - // Query to get events for the given user along with their registration status. query := ` - SELECT - e.id, - e.name, - e.description, - e.start_time, - e.end_time, - e.volunteers, - e.location, - CASE - WHEN es.user_id = $1 THEN true - ELSE false - END AS registered, - CASE - WHEN eb.user_id = $1 THEN true - ELSE false - END AS on_standby - FROM - events e - LEFT JOIN - event_signups es ON e.id = es.event_id - LEFT JOIN - event_standbys eb ON e.id = eb.event_id - WHERE - es.user_id = $1 OR eb.user_id = $1 - ORDER BY - ABS(extract(epoch from e.start_time) - extract(epoch from CURRENT_TIMESTAMP)) + SELECT + e.id, + e.name, + e.description, + e.location, + e.start_date, + e.end_date, + e.volunteers_required, + CASE WHEN es.user_id = $1 THEN true ELSE false END AS registered, + CASE WHEN eb.user_id = $1 THEN true ELSE false END AS on_standby + FROM + events e + LEFT JOIN + event_signups es ON e.id = es.event_id AND es.user_id = $1 + LEFT JOIN + event_standbys eb ON e.id = eb.event_id AND eb.user_id = $1 ` - var events []Event - err := s.db.SelectContext(ctx, &events, query, userID) + rows, err := s.db.QueryContext(ctx, query, userID) + if err != nil { - return nil, fmt.Errorf("failed to get events for user: %w", err) + return nil, fmt.Errorf("failed to fetch events: %w", err) + } + + defer rows.Close() + + var events []Event + + //TODO: update "&event.Registered, &event.OnStandby" when register event api is ready. + for rows.Next() { + var event Event + if err := rows.Scan(&event.ID, &event.Name, &event.Description, &event.Location, &event.StartDate, &event.EndDate, &event.VolunteersRequired, &event.Registered, &event.OnStandby); err != nil { + return nil, fmt.Errorf("failed to scan event row: %w", err) + } + events = append(events, event) } + + if err := rows.Err(); err != nil { + return nil, fmt.Errorf("error iterating over event rows: %w", err) + } + return events, nil }