Skip to content

Commit

Permalink
* feat: implemented post request
Browse files Browse the repository at this point in the history
* fix: added UnregisterEvent to EventStore interface

* feat: added unrestier_event to apimodels

---------
  • Loading branch information
janayee-c committed Mar 28, 2024
1 parent 8d28d27 commit 1fe724b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion apimodels/create_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ type CreateEvent struct {
Description string `json:"description"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
VolunteersRequired int `json:"volunteersRequired"`
VolunteersRequired int `json:"volunteersRequired"` //change to match in event.go?
Location string `json:"location"`
}
19 changes: 19 additions & 0 deletions apimodels/unregister_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package apimodels

type UnregisterEvent struct {
UserID int `json:"userID"`
EventID int `json:"eventID"`
}

/*
Logic for deregistering user
1. Get context of the user and their ID
2. Get context of the event and its ID
4. Check if user is on signup list and remove
5. If not, check if user is on standby list and remove
*/

16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,20 @@ func bind(e *echo.Echo, eventStore store.EventStore) {

return c.JSON(http.StatusOK, event)
})

e.POST("/api/unregister", func(c echo.Context) error {
var request apimodels.UnregisterEvent

// parse request body
bindErr := c.Bind(&request)
if bindErr != nil {
return c.JSON(http.StatusBadRequest, bindErr)
}

err := eventStore.UnregisterEvent(c.Request().Context(), request.UserID, request.EventID)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusOK, request.UserID) //return userID that was removed if successful
})
}
29 changes: 29 additions & 0 deletions store/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"context"
"database/sql"
"fmt"
"time"

Expand All @@ -27,6 +28,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
UnregisterEvent(ctx context.Context, UserID int, EventID int) error
}

type pgEventStore struct {
Expand Down Expand Up @@ -113,3 +115,30 @@ func (s *pgEventStore) Delete(ctx context.Context, id int) error {

return nil
}


// unregisters a user from an event
func (s *pgEventStore) UnregisterEvent(ctx context.Context, userID int, eventID int) error {

//returning id of the signUp from event_signups
signUpQuery := `DELETE FROM event_signups WHERE user_id = $1 AND event_id = $2 RETURNING id`
var signUpID int
err := s.db.GetContext(ctx, &signUpID, signUpQuery, userID, eventID)

if err != nil && err != sql.ErrNoRows {
// Handle errors NOT corresponding to a 'no rows' return error
return fmt.Errorf("failed to remove user from event signup list: %w", err)
}

//returning id of the signUp from event_standby
if err == sql.ErrNoRows {
var standByID int
standByQuery := `DELETE FROM event_standbys WHERE user_id = $1 AND event_id = $2 RETURNING id`
err := s.db.GetContext(ctx, &standByID, standByQuery, userID, eventID)

if err != nil {
return fmt.Errorf("failed to remove user from event standby list: %w", err)
}
}
return nil
}

0 comments on commit 1fe724b

Please sign in to comment.