Skip to content

Commit

Permalink
Tests for singular events working
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyh2024 committed Feb 19, 2024
1 parent 882f0a9 commit dc42f35
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 29 deletions.
22 changes: 11 additions & 11 deletions backend/src/models/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Event struct {
StartTime time.Time `gorm:"type:timestamptz" json:"start_time" validate:"required,ltecsfield=EndTime"`
EndTime time.Time `gorm:"type:timestamptz" json:"end_time" validate:"required,gtecsfield=StartTime"`
Location string `gorm:"type:varchar(255)" json:"location" validate:"required,max=255"`
EventType EventType `gorm:"type:varchar(255);default:open" json:"event_type" validate:"required,max=255"`
EventType EventType `gorm:"type:varchar(255);default:open" json:"event_type" validate:"required,max=255,oneof=open membersOnly"`
IsRecurring bool `gorm:"not null;type:bool;default:false" json:"is_recurring" validate:"-"`

ParentEvent *uuid.UUID `gorm:"foreignKey:ParentEvent" json:"-" validate:"uuid4"`
Expand Down Expand Up @@ -71,7 +71,7 @@ type EventInstanceException struct {
}

type CreateSeriesRequestBody struct {
RecurringType RecurringType `json:"recurring_type" validate:"max=255"`
RecurringType RecurringType `json:"recurring_type" validate:"max=255,oneof=daily weekly monthly"`
SeparationCount int `json:"separation_count" validate:"min=0"`
MaxOccurrences int `json:"max_occurrences" validate:"min=2"`
DayOfWeek int `json:"day_of_week" validate:"min=1,max=7"`
Expand All @@ -87,7 +87,7 @@ type CreateEventRequestBody struct {
StartTime time.Time `json:"start_time" validate:"required,ltecsfield=EndTime"`
EndTime time.Time `json:"end_time" validate:"required,gtecsfield=StartTime"`
Location string `json:"location" validate:"required,max=255"`
EventType EventType `json:"event_type" validate:"required,max=255"`
EventType EventType `json:"event_type" validate:"required,max=255,oneof=open membersOnly"`
IsRecurring *bool `json:"is_recurring" validate:"required"`

// TODO club/tag/notification logic
Expand All @@ -100,13 +100,13 @@ type CreateEventRequestBody struct {
}

type UpdateEventRequestBody struct {
Name string `json:"name" validate:"required,max=255"`
Preview string `json:"preview" validate:"required,max=255"`
Content string `json:"content" validate:"required,max=255"`
StartTime time.Time `json:"start_time" validate:"required,datetime,ltecsfield=EndTime"`
EndTime time.Time `json:"end_time" validate:"required,datetime,gtecsfield=StartTime"`
Location string `json:"location" validate:"required,max=255"`
EventType EventType `gorm:"type:varchar(255);default:open" json:"event_type" validate:"required,max=255"`
Name string `json:"name" validate:"omitempty,max=255"`
Preview string `json:"preview" validate:"omitempty,max=255"`
Content string `json:"content" validate:"omitempty,max=255"`
StartTime time.Time `json:"start_time" validate:"omitempty,ltecsfield=EndTime"`
EndTime time.Time `json:"end_time" validate:"omitempty,gtecsfield=StartTime"`
Location string `json:"location" validate:"omitempty,max=255"`
EventType EventType `gorm:"type:varchar(255);default:open" json:"event_type" validate:"omitempty,max=255,oneof=open membersOnly"`

RSVP []User `json:"-" validate:"-"`
Waitlist []User `json:"-" validate:"-"`
Expand All @@ -116,7 +116,7 @@ type UpdateEventRequestBody struct {
}

type UpdateSeriesRequestBody struct {
RecurringType RecurringType `json:"recurring_type" validate:"max=255"`
RecurringType RecurringType `json:"recurring_type" validate:"max=255,oneof=daily weekly monthly"`
SeparationCount int `json:"separation_count" validate:"min=0"`
MaxOccurrences int `json:"max_occurrences" validate:"min=2"`
DayOfWeek int `json:"day_of_week" validate:"min=1,max=7"`
Expand Down
2 changes: 1 addition & 1 deletion backend/src/server/routes/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Event(router fiber.Router, eventService services.EventServiceInterface) {

eventID.Get("/", eventController.GetEvent)
eventID.Get("/series", eventController.GetSeriesByEventID)
events.Patch("/", eventController.UpdateEvent)
eventID.Patch("/", eventController.UpdateEvent)
eventID.Delete("/", eventController.DeleteEvent)
eventID.Delete("/series", eventController.DeleteSeriesByEventID)

Expand Down
20 changes: 15 additions & 5 deletions backend/src/services/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (e *EventService) GetEvents(limit string, page string) ([]models.Event, *er
return transactions.GetEvents(e.DB, *limitAsInt, offset)
}

// TODO: add logic for creating the []event here
// TODO Q: should we always return a slice of events? or should we return a slice of events if it's a series and a single event if it's not?
// right now we are always returning a slice
func (e *EventService) CreateEvent(eventBody models.CreateEventRequestBody) ([]models.Event, *errors.Error) {
if err := e.Validate.Struct(eventBody); err != nil {
return nil, &errors.FailedToValidateEvent
Expand Down Expand Up @@ -127,12 +127,22 @@ func (e *EventService) UpdateEvent(id string, eventBody models.UpdateEventReques
return nil, &errors.FailedToValidateEvent
}

event, err := utilities.MapRequestToModel(eventBody, &models.UpdateEventRequestBody{})
if err != nil {
return nil, &errors.FailedToMapRequestToModel
updatedEvent := &models.Event{
Name: eventBody.Name,
Preview: eventBody.Preview,
Content: eventBody.Content,
StartTime: eventBody.StartTime,
EndTime: eventBody.EndTime,
Location: eventBody.Location,
EventType: eventBody.EventType,
}

return transactions.UpdateEvent(e.DB, *idAsUUID, *event)
// updatedEvent, err := utilities.MapRequestToModel(eventBody, &models.UpdateEventRequestBody{})
// if err != nil {
// return nil, &errors.FailedToMapRequestToModel
// }

return transactions.UpdateEvent(e.DB, *idAsUUID, *updatedEvent)
}

func (e *EventService) UpdateSeries(eventID string, seriesID string, seriesBody models.UpdateSeriesRequestBody) ([]models.Event, *errors.Error) {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/transactions/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func CreateEventSeries(db *gorm.DB, series models.Series) ([]models.Event, *erro
return series.Events, nil
}

func UpdateEvent(db *gorm.DB, id uuid.UUID, event models.UpdateEventRequestBody) (*models.Event, *errors.Error) {
func UpdateEvent(db *gorm.DB, id uuid.UUID, event models.Event) (*models.Event, *errors.Error) {
if err := db.Model(&models.Event{}).Where("id = ?", id).Updates(event).Error; err != nil {
if stdliberrors.Is(err, gorm.ErrRecordNotFound) {
return nil, &errors.UserNotFound
Expand Down
Loading

0 comments on commit dc42f35

Please sign in to comment.