Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unregister-event api #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
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
}
Loading