Skip to content

Commit

Permalink
Merge branch 'main' into event-api-be-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
alderwhiteford authored Jun 15, 2024
2 parents d283bd7 + 07b2ab5 commit aebbdb8
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 15 deletions.
20 changes: 10 additions & 10 deletions backend/entities/clubs/members/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

type ClubMemberServiceInterface interface {
GetClubMembers(clubID string, pageInfo fiberpaginate.PageInfo) ([]models.User, error)
CreateClubMember(clubID string, userID string) error
GetClubMembers(clubID string, pageInfo fiberpaginate.PageInfo) ([]models.User, error)
DeleteClubMember(clubID string, userID string) error
}

Expand All @@ -21,15 +21,6 @@ func NewClubMemberService(params types.ServiceParams) ClubMemberServiceInterface
return &ClubMemberService{params}
}

func (cms *ClubMemberService) GetClubMembers(clubID string, pageInfo fiberpaginate.PageInfo) ([]models.User, error) {
clubIDAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
return nil, err
}

return GetClubMembers(cms.DB, *clubIDAsUUID, pageInfo)
}

func (cms *ClubMemberService) CreateClubMember(clubID string, userID string) error {
clubIDAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
Expand All @@ -44,6 +35,15 @@ func (cms *ClubMemberService) CreateClubMember(clubID string, userID string) err
return CreateClubMember(cms.DB, *clubIDAsUUID, *userIDAsUUID)
}

func (cms *ClubMemberService) GetClubMembers(clubID string, pageInfo fiberpaginate.PageInfo) ([]models.User, error) {
clubIDAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
return nil, err
}

return GetClubMembers(cms.DB, *clubIDAsUUID, pageInfo)
}

func (cms *ClubMemberService) DeleteClubMember(clubID string, userID string) error {
clubIDAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions backend/entities/clubs/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ func GetAdminIDs(db *gorm.DB, clubID uuid.UUID) ([]uuid.UUID, error) {

return adminUUIDs, nil
}

func IsMember(db *gorm.DB, clubID uuid.UUID, userID uuid.UUID) (bool, error) {
db = cache.SetUseCache(db, true)

var count int64
if err := db.Table("user_club_members").Where("club_id = ? AND user_id = ?", clubID, userID).Count(&count).Error; err != nil {
return false, err
}

return count > 0, nil
}
2 changes: 2 additions & 0 deletions backend/entities/events/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package base

import (
"github.com/GenerateNU/sac/backend/entities/events/previews"
"github.com/GenerateNU/sac/backend/entities/events/rsvps"
"github.com/GenerateNU/sac/backend/entities/events/series"
"github.com/GenerateNU/sac/backend/entities/events/tags"

Expand All @@ -17,6 +18,7 @@ func EventRoutes(eventParams types.RouteParams) {
// MARK: must be called first to avoid conflict between api/v1/events/preview and api/v1/events/:eventID
previews.EventPreviews(eventParams)
EventRouter(eventParams)
rsvps.EventsRSVPs(eventParams)
series.EventSeries(eventParams)
tags.EventTags(eventParams)
}
Expand Down
47 changes: 47 additions & 0 deletions backend/entities/events/rsvps/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package rsvps

import (
"net/http"

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

type Controller struct {
service Service
}

func NewController(service Service) *Controller {
return &Controller{service: service}
}

func (co *Controller) GetEventRSVPs(c *fiber.Ctx) error {
pageInfo, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPageInfo
}

followers, err := co.service.GetEventRSVPs(c.Params("eventID"), *pageInfo)
if err != nil {
return err
}

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

func (co *Controller) CreateEventRSVP(c *fiber.Ctx) error {
if err := co.service.CreateEventRSVP(c.Params("eventID"), c.Params("userID")); err != nil {
return err
}

return c.SendStatus(http.StatusCreated)
}

func (co *Controller) DeleteEventRSVP(c *fiber.Ctx) error {
if err := co.service.DeleteEventRSVP(c.Params("eventID"), c.Params("userID")); err != nil {
return err
}

return c.SendStatus(http.StatusNoContent)
}
18 changes: 18 additions & 0 deletions backend/entities/events/rsvps/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package rsvps

import (
"github.com/GenerateNU/sac/backend/permission"
"github.com/GenerateNU/sac/backend/types"
"github.com/gofiber/fiber/v2"
)

func EventsRSVPs(params types.RouteParams) {
controller := NewController(NewHandler(params.ServiceParams))

// api/v1/events/:eventID/rsvps/*
params.Router.Route("/rsvps", func(r fiber.Router) {
r.Get("/", params.UtilityMiddleware.Paginator, controller.GetEventRSVPs)
r.Post("/:userID", controller.CreateEventRSVP)
r.Delete("/:userID", params.AuthMiddleware.UserAuthorizeById, params.AuthMiddleware.Authorize(permission.DeleteAll), controller.DeleteEventRSVP)
})
}
59 changes: 59 additions & 0 deletions backend/entities/events/rsvps/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package rsvps

import (
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/types"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
)

type Service interface {
CreateEventRSVP(eventID string, userID string) error
GetEventRSVPs(eventID string, pageInfo fiberpaginate.PageInfo) ([]models.User, error)
DeleteEventRSVP(eventID string, userID string) error
}

type Handler struct {
types.ServiceParams
}

func NewHandler(params types.ServiceParams) Service {
return &Handler{params}
}

func (h *Handler) CreateEventRSVP(eventID string, userID string) error {
eventIDAsUUID, err := utilities.ValidateID(eventID)
if err != nil {
return err
}

userIDAsUUID, err := utilities.ValidateID(userID)
if err != nil {
return err
}

return CreateEventRSVP(h.DB, *eventIDAsUUID, *userIDAsUUID)
}

func (h *Handler) GetEventRSVPs(eventID string, pageInfo fiberpaginate.PageInfo) ([]models.User, error) {
eventIDAsUUID, err := utilities.ValidateID(eventID)
if err != nil {
return nil, err
}

return GetEventRSVPs(h.DB, *eventIDAsUUID, pageInfo)
}

func (h *Handler) DeleteEventRSVP(eventID string, userID string) error {
eventIDAsUUID, err := utilities.ValidateID(eventID)
if err != nil {
return err
}

userIDAsUUID, err := utilities.ValidateID(userID)
if err != nil {
return err
}

return DeleteEventRSVP(h.DB, *eventIDAsUUID, *userIDAsUUID)
}
93 changes: 93 additions & 0 deletions backend/entities/events/rsvps/transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package rsvps

import (
"errors"

"github.com/GenerateNU/sac/backend/database/cache"
"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/entities/events"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/entities/users"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
"github.com/google/uuid"
"gorm.io/gorm"
)

func CreateEventRSVP(db *gorm.DB, eventID uuid.UUID, userID uuid.UUID) error {
db = cache.SetUseCache(db, true)

event, err := events.GetEvent(db, eventID)
if err != nil {
return err
}

if event.IsArchived {
return utilities.ForbiddenReason(errors.New("cannot RSVP to an archived event"))
}

if event.IsDraft {
return utilities.ForbiddenReason(errors.New("cannot RSVP to a draft event"))
}

user, err := users.GetUser(db, userID)
if err != nil {
return err
}

if !event.IsPublic {
isMember, err := clubs.IsMember(db, *event.Host, userID)
if err != nil {
return err
}

if !isMember {
return utilities.ForbiddenReason(errors.New("cannot RSVP to a private event when not a member"))
}
}

if err := db.Model(&user).Association("RSVP").Append(event); err != nil {
return err
}

return nil
}

func GetEventRSVPs(db *gorm.DB, eventID uuid.UUID, pageInfo fiberpaginate.PageInfo) ([]models.User, error) {
db = cache.SetUseCache(db, true)

event, err := events.GetEvent(db, eventID)
if err != nil {
return nil, err
}

var users []models.User
if err := db.Scopes(utilities.IntoScope(pageInfo, db)).Model(&event).Association("RSVP").Find(&users); err != nil {
return nil, err
}

return users, nil
}

func DeleteEventRSVP(db *gorm.DB, eventID uuid.UUID, userID uuid.UUID) error {
db = cache.SetUseCache(db, true)

user, err := users.GetUser(db, userID)
if err != nil {
return err
}

event, err := events.GetEvent(db, eventID)
if err != nil {
return err
}

if err := db.Model(&user).Association("RSVP").Delete(event); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return utilities.BadRequest(errors.New("user was never RSVPed to this event"))
}
return err
}

return nil
}
14 changes: 9 additions & 5 deletions backend/utilities/api_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,27 @@ func BadRequest(err error) APIError {
}

func InvalidJSON() APIError {
return NewAPIError(http.StatusBadRequest, fmt.Errorf("invalid JSON request data"))
return NewAPIError(http.StatusBadRequest, errors.New("invalid JSON request data"))
}

func Unauthorized() APIError {
return NewAPIError(http.StatusUnauthorized, fmt.Errorf("unauthorized"))
return NewAPIError(http.StatusUnauthorized, errors.New("unauthorized"))
}

func Forbidden() APIError {
return NewAPIError(http.StatusForbidden, fmt.Errorf("forbidden"))
return NewAPIError(http.StatusForbidden, errors.New("forbidden"))
}

func ForbiddenReason(err error) APIError {
return NewAPIError(http.StatusForbidden, err)
}

func Conflict() APIError {
return NewAPIError(http.StatusConflict, fmt.Errorf("conflict"))
return NewAPIError(http.StatusConflict, errors.New("conflict"))
}

func InternalServerError() APIError {
return NewAPIError(http.StatusInternalServerError, fmt.Errorf("internal server error"))
return NewAPIError(http.StatusInternalServerError, errors.New("internal server error"))
}

func ErrorHandler(c *fiber.Ctx, err error) error {
Expand Down

0 comments on commit aebbdb8

Please sign in to comment.