Skip to content

Commit

Permalink
add ordering a list of events by event time
Browse files Browse the repository at this point in the history
  • Loading branch information
mavehur committed Apr 2, 2024
1 parent 69bb361 commit afbdf1c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
69 changes: 38 additions & 31 deletions store/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {

Check failure on line 151 in store/event.go

View workflow job for this annotation

GitHub Actions / build

event.Registered undefined (type Event has no field or method Registered)

Check failure on line 151 in store/event.go

View workflow job for this annotation

GitHub Actions / build

event.OnStandby undefined (type Event has no field or method OnStandby)
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
}

0 comments on commit afbdf1c

Please sign in to comment.