-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into event-api-be-integration
- Loading branch information
Showing
8 changed files
with
249 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters