From 1fe724bcf418a09da3c344ebfbf033a763161d12 Mon Sep 17 00:00:00 2001 From: Janaye Cheong Date: Thu, 28 Mar 2024 05:25:23 -0700 Subject: [PATCH 1/2] * feat: implemented post request * fix: added UnregisterEvent to EventStore interface * feat: added unrestier_event to apimodels --------- --- apimodels/create_event.go | 2 +- apimodels/unregister_event.go | 19 +++++++++++++++++++ main.go | 16 ++++++++++++++++ store/event.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 apimodels/unregister_event.go diff --git a/apimodels/create_event.go b/apimodels/create_event.go index cad0253..49b944a 100644 --- a/apimodels/create_event.go +++ b/apimodels/create_event.go @@ -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"` } diff --git a/apimodels/unregister_event.go b/apimodels/unregister_event.go new file mode 100644 index 0000000..13205ed --- /dev/null +++ b/apimodels/unregister_event.go @@ -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 + +*/ + diff --git a/main.go b/main.go index ad7e539..56a1ef1 100644 --- a/main.go +++ b/main.go @@ -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 + }) } diff --git a/store/event.go b/store/event.go index a65cf65..c784824 100644 --- a/store/event.go +++ b/store/event.go @@ -2,6 +2,7 @@ package store import ( "context" + "database/sql" "fmt" "time" @@ -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 { @@ -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 +} From 93be2aaae31249c80b6f738209480ce6ddce1149 Mon Sep 17 00:00:00 2001 From: Janaye Cheong Date: Thu, 28 Mar 2024 05:52:50 -0700 Subject: [PATCH 2/2] removed unnecessary comment --- apimodels/create_event.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apimodels/create_event.go b/apimodels/create_event.go index 49b944a..e9864eb 100644 --- a/apimodels/create_event.go +++ b/apimodels/create_event.go @@ -5,6 +5,6 @@ type CreateEvent struct { Description string `json:"description"` StartDate string `json:"startDate"` EndDate string `json:"endDate"` - VolunteersRequired int `json:"volunteersRequired"` //change to match in event.go? + VolunteersRequired int `json:"volunteersRequired"` Location string `json:"location"` }