Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into profile-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
in-mai-space committed Jun 12, 2024
2 parents 4c60c8b + 1fbfa77 commit 05282a1
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 178 deletions.
23 changes: 6 additions & 17 deletions backend/entities/events/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewEventController(eventService EventServiceInterface) *EventController {
return &EventController{eventService: eventService}
}

// GetAllEvents godoc
// GetEvents godoc
//
// @Summary Retrieve all events
// @Description Retrieves all events
Expand All @@ -31,7 +31,7 @@ func NewEventController(eventService EventServiceInterface) *EventController {
// @Failure 404 {object} error
// @Failure 500 {object} error
// @Router /events/ [get]
func (e *EventController) GetAllEvents(c *fiber.Ctx) error {
func (e *EventController) GetEvents(c *fiber.Ctx) error {
pageInfo, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPageInfo
Expand All @@ -42,22 +42,11 @@ func (e *EventController) GetAllEvents(c *fiber.Ctx) error {
pageInfo = nil
}

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)
events, err := e.eventService.GetEvents(*pageInfo, c.Query("start"), c.Query("end"))
if err != nil {
return err
}
return c.Status(http.StatusOK).JSON(events)
}

// GetEvent godoc
Expand Down
60 changes: 0 additions & 60 deletions backend/entities/events/base/models.go

This file was deleted.

4 changes: 3 additions & 1 deletion backend/entities/events/base/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package base

import (
"github.com/GenerateNU/sac/backend/entities/events/previews"
"github.com/GenerateNU/sac/backend/entities/events/series"
"github.com/GenerateNU/sac/backend/entities/events/tags"
"github.com/gofiber/fiber/v2"
Expand All @@ -12,6 +13,7 @@ import (
func EventRoutes(eventParams types.RouteParams) {
eventParams.Router = EventRouter(eventParams)

previews.EventPreviews(eventParams)
series.EventSeries(eventParams)
tags.EventTags(eventParams)
}
Expand All @@ -22,7 +24,7 @@ func EventRouter(eventParams types.RouteParams) fiber.Router {
// api/v1/events/*
events := eventParams.Router.Group("/events")

events.Get("/", eventParams.UtilityMiddleware.Paginator, eventController.GetAllEvents)
events.Get("/", eventParams.UtilityMiddleware.Paginator, eventController.GetEvents)
events.Post(
"/",
authMiddleware.AttachExtractor(eventParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")),
Expand Down
47 changes: 18 additions & 29 deletions backend/entities/events/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package base

import (
"errors"
"time"

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

"github.com/GenerateNU/sac/backend/types"
"github.com/GenerateNU/sac/backend/utilities"
Expand All @@ -16,7 +14,6 @@ import (
type EventServiceInterface interface {
// getters
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 @@ -33,11 +30,25 @@ func NewEventService(serviceParams types.ServiceParams) EventServiceInterface {
}

func (e *EventService) GetEvents(pageInfo fiberpaginate.PageInfo, start string, end string) ([]models.Event, error) {
return serviceGetEvents(e.DB, pageInfo, start, end, GetEvents, GetEventsByTime)
}
if start == "" || end == "" {
return events.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)
}

func (e *EventService) GetEventsPreview(pageInfo fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) {
return serviceGetEvents(e.DB, pageInfo, start, end, GetEventsPreview, GetEventsPreviewByTime)
if startTime.After(*endTime) {
return nil, utilities.BadRequest(errors.New("start time must be before end time"))
}

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

func (e *EventService) GetEvent(eventID string) (*models.Event, error) {
Expand Down Expand Up @@ -83,25 +94,3 @@ func (e *EventService) DeleteEvent(eventID string) error {

return DeleteEvent(e.DB, *idAsUUID)
}

func serviceGetEvents[E any](db *gorm.DB, pageInfo fiberpaginate.PageInfo, start string, end string, getEFunc func(*gorm.DB, fiberpaginate.PageInfo) ([]E, error), getEByTimeFunc func(*gorm.DB, fiberpaginate.PageInfo, time.Time, time.Time) ([]E, error)) ([]E, error) {
if start == "" || end == "" {
return getEFunc(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 getEByTimeFunc(db, pageInfo, *startTime, *endTime)
}
75 changes: 4 additions & 71 deletions backend/entities/events/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"time"

"github.com/GenerateNU/sac/backend/constants"
"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/entities/events"
"github.com/GenerateNU/sac/backend/entities/events/tags"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/garrettladley/fiberpaginate"

Expand All @@ -21,63 +19,17 @@ import (
)

func GetEvents(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]models.Event, error) {
return transactionsGetEvents[models.Event](db, &pageInfo, nil, nil)
}

func GetEventsPreview(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]EventPreview, error) {
events, err := transactionsGetEvents[EventStub](db, &pageInfo, nil, nil)
if err != nil {
return nil, err
}

eventPreviews := make([]EventPreview, len(events))
for index, event := range events {
tags, err := tags.GetEventTags(db, event.ID)
if err != nil {
return nil, err
}

eventPreviews[index].Tags = tags

host, err := clubs.GetClub(db, event.Host)
if err != nil {
return nil, err
}

eventPreviews[index] = *event.Into(tags, host.Name, host.Logo)
}

return eventPreviews, nil
return events.GetEvents(db, pageInfo)
}

func GetEventsByTime(db *gorm.DB, pageInfo fiberpaginate.PageInfo, startTime time.Time, endTime time.Time) ([]models.Event, error) {
return transactionsGetEvents[models.Event](db, &pageInfo, &startTime, &endTime)
}
var events []models.Event

func GetEventsPreviewByTime(db *gorm.DB, pageInfo fiberpaginate.PageInfo, startTime time.Time, endTime time.Time) ([]EventPreview, error) {
events, err := transactionsGetEvents[EventStub](db, &pageInfo, &startTime, &endTime)
if err != nil {
if err := db.Model(&events).Where("start_time >= ? AND end_time <= ?", startTime, endTime).Scopes(utilities.IntoScope(pageInfo, db)).Find(&events).Error; err != nil {
return nil, err
}

eventPreviews := make([]EventPreview, len(events))
for index, event := range events {
tags, err := tags.GetEventTags(db, event.ID)
if err != nil {
return nil, err
}

eventPreviews[index].Tags = tags

host, err := clubs.GetClub(db, event.Host)
if err != nil {
return nil, err
}

eventPreviews[index] = *event.Into(tags, host.Name, host.Logo)
}

return eventPreviews, nil
return events, nil
}

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

return nil
}

func transactionsGetEvents[E any](db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime *time.Time, endTime *time.Time) ([]E, error) {
var events []E
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
}
35 changes: 35 additions & 0 deletions backend/entities/events/previews/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package previews

import (
"net/http"

"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
"github.com/gofiber/fiber/v2"
)

type EventPreviewController struct {
service Service
}

func NewEventController(service Service) *EventPreviewController {
return &EventPreviewController{service: service}
}

func (e *EventPreviewController) GetEventPreviews(c *fiber.Ctx) error {
pageInfo, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPageInfo
}

usePagination := c.QueryBool("pagination", true)
if !usePagination {
pageInfo = nil
}

events, err := e.service.GetEventPreviews(pageInfo, c.Query("start"), c.Query("end"))
if err != nil {
return err
}
return c.Status(http.StatusOK).JSON(events)
}
43 changes: 43 additions & 0 deletions backend/entities/events/previews/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package previews

import (
"time"

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

type common struct {
ID uuid.UUID `json:"id"`
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"`
}

type EventStub struct {
common
Host uuid.UUID `json:"host"`
}

func (es *EventStub) Into(tags []models.Tag, hostName string, hostLogo string) *EventPreview {
return &EventPreview{
common: es.common,
Tags: tags,
HostName: hostName,
HostLogo: hostLogo,
}
}

func (es EventStub) TableName() string {
return "events"
}

type EventPreview struct {
common
Tags []models.Tag `json:"tags"`
HostName string `json:"host_name"`
HostLogo string `json:"host_logo"`
}
Loading

0 comments on commit 05282a1

Please sign in to comment.