Skip to content

Commit

Permalink
get events start & end and preview
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Jun 5, 2024
1 parent 936f90d commit e0d0db4
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 13 deletions.
20 changes: 15 additions & 5 deletions backend/entities/events/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,22 @@ func (e *EventController) GetAllEvents(c *fiber.Ctx) error {
return utilities.ErrExpectedPageInfo
}

events, err := e.eventService.GetEvents(*pageInfo)
if err != nil {
return err
start := c.Query("start")
end := c.Query("end")

if c.QueryBool("preview", false) {
events, err := e.eventService.GetEventsPreview(*pageInfo, start, end)
if err != nil {
return err
}
return c.Status(http.StatusOK).JSON(events)
} else {
events, err := e.eventService.GetEvents(*pageInfo, start, end)
if err != nil {
return err
}
return c.Status(http.StatusOK).JSON(events)
}

return c.Status(http.StatusOK).JSON(events)
}

// GetEvent godoc
Expand Down
18 changes: 18 additions & 0 deletions backend/entities/events/base/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package base

import (
"time"

"github.com/GenerateNU/sac/backend/entities/models"
)

type EventPreview struct {
Title string `json:"title"`

EventType models.EventType `json:"event_type"`
Location string `json:"location"`
Link string `json:"link"`

StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
49 changes: 46 additions & 3 deletions backend/entities/events/base/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package base

import (
"errors"

"github.com/GenerateNU/sac/backend/entities/events"
"github.com/GenerateNU/sac/backend/entities/models"

Expand All @@ -11,7 +13,8 @@ import (

type EventServiceInterface interface {
// getters
GetEvents(pageInfo fiberpaginate.PageInfo) ([]models.Event, error)
GetEvents(pageInfo fiberpaginate.PageInfo, start string, end string) ([]models.Event, error)
GetEventsPreview(pageInfo fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error)
GetEvent(eventID string) (*models.Event, error)
// event cud
CreateEvent(body events.CreateEventRequestBody) (*models.Event, error)
Expand All @@ -27,8 +30,48 @@ func NewEventService(serviceParams types.ServiceParams) EventServiceInterface {
return &EventService{serviceParams}
}

func (e *EventService) GetEvents(pageInfo fiberpaginate.PageInfo) ([]models.Event, error) {
return GetEvents(e.DB, pageInfo)
func (e *EventService) GetEvents(pageInfo fiberpaginate.PageInfo, start string, end string) ([]models.Event, error) {
if start != "" && end != "" {
return GetEvents(e.DB, pageInfo)
}

startTime, err := utilities.ParseTime(start, utilities.YYYY_dash_MM_dash_DD)
if err != nil {
return nil, utilities.BadRequest(err)
}

endTime, err := utilities.ParseTime(end, utilities.YYYY_dash_MM_dash_DD)
if err != nil {
return nil, utilities.BadRequest(err)
}

if startTime.After(endTime) {
return nil, utilities.BadRequest(errors.New("start time must be before end time"))
}

return GetEventsByTime(e.DB, startTime, endTime)
}

func (e *EventService) GetEventsPreview(pageInfo fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) {
if start != "" && end != "" {
return GetEventsPreview(e.DB, pageInfo)
}

startTime, err := utilities.ParseTime(start, utilities.YYYY_dash_MM_dash_DD)
if err != nil {
return nil, utilities.BadRequest(err)
}

endTime, err := utilities.ParseTime(end, utilities.YYYY_dash_MM_dash_DD)
if err != nil {
return nil, utilities.BadRequest(err)
}

if startTime.After(endTime) {
return nil, utilities.BadRequest(errors.New("start time must be before end time"))
}

return GetEventsPreviewByTime(e.DB, startTime, endTime)
}

func (e *EventService) GetEvent(eventID string) (*models.Event, error) {
Expand Down
37 changes: 32 additions & 5 deletions backend/entities/events/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package base
import (
"errors"
"log/slog"
"time"

"github.com/GenerateNU/sac/backend/constants"
"github.com/GenerateNU/sac/backend/entities/events"
Expand All @@ -18,12 +19,19 @@ import (
)

func GetEvents(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]models.Event, error) {
var events []models.Event
if err := db.Scopes(utilities.IntoScope(pageInfo, db)).Find(&events).Error; err != nil {
return nil, err
}
return getEvents[models.Event](db, &pageInfo, nil, nil)
}

return events, nil
func GetEventsPreview(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]EventPreview, error) {
return getEvents[EventPreview](db, &pageInfo, nil, nil)
}

func GetEventsByTime(db *gorm.DB, startTime time.Time, endTime time.Time) ([]models.Event, error) {
return getEvents[models.Event](db, nil, &startTime, &endTime)
}

func GetEventsPreviewByTime(db *gorm.DB, startTime time.Time, endTime time.Time) ([]EventPreview, error) {
return getEvents[EventPreview](db, nil, &startTime, &endTime)
}

func CreateEvent(db *gorm.DB, event models.Event) (*models.Event, error) {
Expand Down Expand Up @@ -101,3 +109,22 @@ func DeleteEvent(db *gorm.DB, id uuid.UUID) error {

return nil
}

func getEvents[T any](db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime, endTime *time.Time) ([]T, error) {
var events []T
query := db.Model(&events)

if startTime != nil && endTime != nil {
query = query.Where("start_time >= ? AND end_time <= ?", *startTime, *endTime)
}

if pageInfo != nil {
query = query.Scopes(utilities.IntoScope(*pageInfo, db))
}

if err := query.Find(&events).Error; err != nil {
return nil, err
}

return events, nil
}
13 changes: 13 additions & 0 deletions backend/utilities/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utilities

import "time"

type TimeFormat string

const (
YYYY_dash_MM_dash_DD TimeFormat = "2006-01-02"
)

func ParseTime(s string, format TimeFormat) (time.Time, error) {
return time.Parse(string(format), s)
}

0 comments on commit e0d0db4

Please sign in to comment.