From ac90e210e74421710ff80abd27f342a0a5018325 Mon Sep 17 00:00:00 2001 From: garrettladley Date: Fri, 31 May 2024 16:33:29 -0400 Subject: [PATCH] controller and transactions complete | rename from 'pagination' -> 'pageInfo' --- .../entities/categories/base/controller.go | 6 +- .../entities/categories/tags/controller.go | 6 +- backend/entities/clubs/base/controller.go | 6 +- backend/entities/clubs/events/controller.go | 6 +- .../entities/clubs/followers/controller.go | 6 +- backend/entities/clubs/members/controller.go | 6 +- .../entities/clubs/recruitment/controller.go | 39 ++++- .../clubs/recruitment/transactions.go | 142 ++++++++---------- backend/entities/events/base/controller.go | 6 +- backend/entities/files/base/controller.go | 6 +- .../entities/leadership/base/controller.go | 6 +- backend/entities/socials/base/controller.go | 6 +- backend/entities/users/base/controller.go | 6 +- backend/utilities/api_error.go | 12 +- 14 files changed, 135 insertions(+), 124 deletions(-) diff --git a/backend/entities/categories/base/controller.go b/backend/entities/categories/base/controller.go index 2ec179336..5732bb2b4 100644 --- a/backend/entities/categories/base/controller.go +++ b/backend/entities/categories/base/controller.go @@ -64,12 +64,12 @@ func (cat *CategoryController) CreateCategory(c *fiber.Ctx) error { // @Failure 500 {string} error // @Router /categories/ [get] func (cat *CategoryController) GetCategories(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - categories, err := cat.categoryService.GetCategories(*pagination) + categories, err := cat.categoryService.GetCategories(*pageInfo) if err != nil { return err } diff --git a/backend/entities/categories/tags/controller.go b/backend/entities/categories/tags/controller.go index 8fb211b12..efaba43a8 100644 --- a/backend/entities/categories/tags/controller.go +++ b/backend/entities/categories/tags/controller.go @@ -32,12 +32,12 @@ func NewCategoryTagController(categoryTagService CategoryTagServiceInterface) *C // @Failure 500 {object} error // @Router /categories/{categoryID}/tags/ [get] func (ct *CategoryTagController) GetTagsByCategory(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - tags, err := ct.categoryTagService.GetTagsByCategory(c.Params("categoryID"), *pagination) + tags, err := ct.categoryTagService.GetTagsByCategory(c.Params("categoryID"), *pageInfo) if err != nil { return err } diff --git a/backend/entities/clubs/base/controller.go b/backend/entities/clubs/base/controller.go index 0d665fa11..f5416889a 100644 --- a/backend/entities/clubs/base/controller.go +++ b/backend/entities/clubs/base/controller.go @@ -30,12 +30,12 @@ func NewClubController(clubService ClubServiceInterface) *ClubController { // @Failure 500 {object} error // @Router /clubs/ [get] func (cl *ClubController) GetClubs(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - clubs, err := cl.clubService.GetClubs(*pagination) + clubs, err := cl.clubService.GetClubs(*pageInfo) if err != nil { return err } diff --git a/backend/entities/clubs/events/controller.go b/backend/entities/clubs/events/controller.go index 9f0235657..9726078b5 100644 --- a/backend/entities/clubs/events/controller.go +++ b/backend/entities/clubs/events/controller.go @@ -32,12 +32,12 @@ func NewClubEventController(clubEventService ClubEventServiceInterface) *ClubEve // @Failure 500 {object} error // @Router /clubs/{clubID}/events/ [get] func (cl *ClubEventController) GetClubEvents(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - if events, err := cl.clubEventService.GetClubEvents(c.Params("clubID"), *pagination); err != nil { + if events, err := cl.clubEventService.GetClubEvents(c.Params("clubID"), *pageInfo); err != nil { return err } else { return c.Status(http.StatusOK).JSON(events) diff --git a/backend/entities/clubs/followers/controller.go b/backend/entities/clubs/followers/controller.go index 45f8b9217..4e826506e 100644 --- a/backend/entities/clubs/followers/controller.go +++ b/backend/entities/clubs/followers/controller.go @@ -32,12 +32,12 @@ func NewClubFollowerController(clubFollowerService ClubFollowerServiceInterface) // @Failure 500 {object} error // @Router /clubs/{clubID}/followers/ [get] func (cf *ClubFollowerController) GetClubFollowers(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - followers, err := cf.clubFollowerService.GetClubFollowers(c.Params("clubID"), *pagination) + followers, err := cf.clubFollowerService.GetClubFollowers(c.Params("clubID"), *pageInfo) if err != nil { return err } diff --git a/backend/entities/clubs/members/controller.go b/backend/entities/clubs/members/controller.go index b5c899bee..d40733256 100644 --- a/backend/entities/clubs/members/controller.go +++ b/backend/entities/clubs/members/controller.go @@ -33,12 +33,12 @@ func NewClubMemberController(clubMemberService ClubMemberServiceInterface) *Club // @Failure 500 {object} error // @Router /clubs/{clubID}/members/ [get] func (cm *ClubMemberController) GetClubMembers(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - followers, err := cm.clubMemberService.GetClubMembers(c.Params("clubID"), *pagination) + followers, err := cm.clubMemberService.GetClubMembers(c.Params("clubID"), *pageInfo) if err != nil { return err } diff --git a/backend/entities/clubs/recruitment/controller.go b/backend/entities/clubs/recruitment/controller.go index 05c86dad3..3532ce05e 100644 --- a/backend/entities/clubs/recruitment/controller.go +++ b/backend/entities/clubs/recruitment/controller.go @@ -3,6 +3,8 @@ package recruitment import ( "net/http" + "github.com/GenerateNU/sac/backend/utilities" + "github.com/garrettladley/fiberpaginate" "github.com/gofiber/fiber/v2" ) @@ -65,17 +67,46 @@ func (cr *ClubRecruitmentController) CreateClubRecruitmentApplication(c *fiber.C } func (cr *ClubRecruitmentController) GetClubRecruitmentApplications(c *fiber.Ctx) error { - return nil + pageInfo, ok := fiberpaginate.FromContext(c) + if !ok { + return utilities.ErrExpectedPageInfo + } + + applications, err := cr.clubRecruitmentService.GetClubRecruitmentApplications(c.UserContext(), c.Params("clubID"), c.Params("recruitmentID"), *pageInfo) + if err != nil { + return err + } + + return c.Status(http.StatusOK).JSON(applications) } func (cr *ClubRecruitmentController) GetClubRecruitmentApplication(c *fiber.Ctx) error { - return nil + application, err := cr.clubRecruitmentService.GetClubRecruitmentApplication(c.UserContext(), c.Params("clubID"), c.Params("recruitmentID"), c.Params("applicationID")) + if err != nil { + return err + } + + return c.Status(http.StatusOK).JSON(application) } func (cr *ClubRecruitmentController) UpdateClubRecruitmentApplication(c *fiber.Ctx) error { - return nil + var body UpdateApplicationRequestBody + if err := c.BodyParser(&body); err != nil { + return err + } + + application, err := cr.clubRecruitmentService.UpdateClubRecruitmentApplication(c.UserContext(), c.Params("clubID"), c.Params("recruitmentID"), c.Params("applicationID"), body) + if err != nil { + return err + } + + return c.Status(http.StatusOK).JSON(application) } func (cr *ClubRecruitmentController) DeleteClubRecruitmentApplication(c *fiber.Ctx) error { - return nil + if err := cr.clubRecruitmentService.DeleteClubRecruitmentApplication(c.UserContext(), c.Params("clubID"), c.Params("recruitmentID"), c.Params("applicationID")); err != nil { + return err + } + + return c.SendStatus(http.StatusNoContent) } diff --git a/backend/entities/clubs/recruitment/transactions.go b/backend/entities/clubs/recruitment/transactions.go index de967e6e1..bc2238fb8 100644 --- a/backend/entities/clubs/recruitment/transactions.go +++ b/backend/entities/clubs/recruitment/transactions.go @@ -12,124 +12,104 @@ import ( ) func CreateClubRecruitment(ctx context.Context, db *gorm.DB, clubID *uuid.UUID, recruitment *models.Recruitment) (*models.Recruitment, error) { - return utilities.ExecuteWithTimeoutResult(ctx, func() (*models.Recruitment, error) { - if err := db.Model(&models.Club{}).Where("id = ?", clubID).Association("Recruitment").Append(recruitment); err != nil { - return nil, err - } + if err := db.WithContext(ctx).Model(&models.Club{}).Where("id = ?", clubID).Association("Recruitment").Append(recruitment); err != nil { + return nil, err + } - return recruitment, nil - }) + return recruitment, nil } func GetClubRecruitment(ctx context.Context, db *gorm.DB, clubID *uuid.UUID) (*models.Recruitment, error) { - return utilities.ExecuteWithTimeoutResult(ctx, func() (*models.Recruitment, error) { - var recruitment models.Recruitment - if err := db.Model(&models.Club{}).Where("id = ?", clubID).Association("Recruitment").Find(&recruitment); err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, utilities.ErrNotFound - } - - return nil, err + var recruitment models.Recruitment + if err := db.WithContext(ctx).Model(&models.Club{}).Where("id = ?", clubID).Association("Recruitment").Find(&recruitment); err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, utilities.ErrNotFound } - return &recruitment, nil - }) + return nil, err + } + + return &recruitment, nil } func UpdateClubRecruitment(ctx context.Context, db *gorm.DB, clubID *uuid.UUID, recruitment *models.Recruitment) (*models.Recruitment, error) { - return utilities.ExecuteWithTimeoutResult(ctx, func() (*models.Recruitment, error) { - existingRecruitment, err := GetClubRecruitment(ctx, db, clubID) - if err != nil { - return nil, err - } + existingRecruitment, err := GetClubRecruitment(ctx, db, clubID) + if err != nil { + return nil, err + } - if err := db.Model(&models.Club{}).Where("id = ?", clubID).Association("Recruitment").Replace(utilities.Merge(existingRecruitment, recruitment)); err != nil { - return nil, err - } + if err := db.WithContext(ctx).Model(&models.Club{}).Where("id = ?", clubID).Association("Recruitment").Replace(utilities.Merge(existingRecruitment, recruitment)); err != nil { + return nil, err + } - return recruitment, nil - }) + return recruitment, nil } func DeleteClubRecruitment(ctx context.Context, db *gorm.DB, clubID *uuid.UUID) error { - return utilities.ExecuteWithTimeout(context.Background(), func() error { - if err := db.Model(&models.Club{}).Where("id = ?", clubID).Association("Recruitment").Clear(); err != nil { - return err - } + if err := db.WithContext(ctx).Model(&models.Club{}).Where("id = ?", clubID).Association("Recruitment").Clear(); err != nil { + return err + } - return nil - }) + return nil } func CreateApplication(ctx context.Context, db *gorm.DB, clubID *uuid.UUID, recruitmentID *uuid.UUID, application *models.Application) (*models.Application, error) { - return utilities.ExecuteWithTimeoutResult(ctx, func() (*models.Application, error) { - recruitment, err := GetClubRecruitment(ctx, db, clubID) - if err != nil { - return nil, err - } + recruitment, err := GetClubRecruitment(ctx, db, clubID) + if err != nil { + return nil, err + } - if err := db.Model(&recruitment).Association("Application").Append(application); err != nil { - return nil, err - } + if err := db.WithContext(ctx).Model(&recruitment).Association("Application").Append(application); err != nil { + return nil, err + } - return application, nil - }) + return application, nil } func GetClubRecruitmentApplications(ctx context.Context, db *gorm.DB, clubID *uuid.UUID, recruitmentID *uuid.UUID, pageInfo fiberpaginate.PageInfo) ([]models.Application, error) { - result, err := utilities.ExecuteWithTimeoutResult(ctx, func() (*[]models.Application, error) { - recruitment, err := GetClubRecruitment(ctx, db, clubID) - if err != nil { - return nil, err - } - - var applications []models.Application - if err := db.Scopes(utilities.IntoScope(pageInfo, db)).Model(&recruitment).Association("Application").Find(&applications); err != nil { - return nil, err - } + recruitment, err := GetClubRecruitment(ctx, db, clubID) + if err != nil { + return nil, err + } - return &applications, nil - }) + var applications []models.Application + if err := db.WithContext(ctx).Scopes(utilities.IntoScope(pageInfo, db)).Model(&recruitment).Association("Application").Find(&applications); err != nil { + return nil, err + } - return *result, err + return applications, nil } func GetClubRecruitmentApplication(ctx context.Context, db *gorm.DB, clubID *uuid.UUID, recruitmentID *uuid.UUID, applicationID *uuid.UUID) (*models.Application, error) { - return utilities.ExecuteWithTimeoutResult(ctx, func() (*models.Application, error) { - var application models.Application - if err := db.Model(&models.Recruitment{}).Where("id = ?", recruitmentID).Association("Application").Find(&application, applicationID); err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, utilities.ErrNotFound - } - - return nil, err + var application models.Application + if err := db.WithContext(ctx).Model(&models.Recruitment{}).Where("id = ?", recruitmentID).Association("Application").Find(&application, applicationID); err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, utilities.ErrNotFound } - return &application, nil - }) + return nil, err + } + + return &application, nil } func UpdateClubRecruitmentApplication(ctx context.Context, db *gorm.DB, clubID *uuid.UUID, recruitmentID *uuid.UUID, applicationID *uuid.UUID, application *models.Application) (*models.Application, error) { - return utilities.ExecuteWithTimeoutResult(ctx, func() (*models.Application, error) { - existingApplication, err := GetClubRecruitmentApplication(ctx, db, clubID, recruitmentID, applicationID) - if err != nil { - return nil, err - } + existingApplication, err := GetClubRecruitmentApplication(ctx, db, clubID, recruitmentID, applicationID) + if err != nil { + return nil, err + } - if err := db.Model(&models.Recruitment{}).Where("id = ?", recruitmentID).Association("Application").Replace(utilities.Merge(existingApplication, application)); err != nil { - return nil, err - } + if err := db.WithContext(ctx).Model(&models.Recruitment{}).Where("id = ?", recruitmentID).Association("Application").Replace(utilities.Merge(existingApplication, application)); err != nil { + return nil, err + } - return application, nil - }) + return application, nil } func DeleteClubRecruitmentApplication(ctx context.Context, db *gorm.DB, clubID *uuid.UUID, recruitmentID *uuid.UUID, applicationID *uuid.UUID) error { - return utilities.ExecuteWithTimeout(ctx, func() error { - if err := db.Model(&models.Recruitment{}).Where("id = ?", recruitmentID).Association("Application").Delete(applicationID); err != nil { - return err - } + if err := db.WithContext(ctx).Model(&models.Recruitment{}).Where("id = ?", recruitmentID).Association("Application").Delete(applicationID); err != nil { + return err + } - return nil - }) + return nil } diff --git a/backend/entities/events/base/controller.go b/backend/entities/events/base/controller.go index e53934759..7d5a8003c 100644 --- a/backend/entities/events/base/controller.go +++ b/backend/entities/events/base/controller.go @@ -32,12 +32,12 @@ func NewEventController(eventService EventServiceInterface) *EventController { // @Failure 500 {object} error // @Router /events/ [get] func (e *EventController) GetAllEvents(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - events, err := e.eventService.GetEvents(*pagination) + events, err := e.eventService.GetEvents(*pageInfo) if err != nil { return err } diff --git a/backend/entities/files/base/controller.go b/backend/entities/files/base/controller.go index de0efaed3..48c8a58b2 100644 --- a/backend/entities/files/base/controller.go +++ b/backend/entities/files/base/controller.go @@ -32,12 +32,12 @@ func NewFileController(fileService FileServiceInterface) *FileController { // @Failure 500 {object} error // @Router /files/ [get] func (f *FileController) GetFiles(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - files, err := f.fileService.GetFiles(*pagination) + files, err := f.fileService.GetFiles(*pageInfo) if err != nil { return err } diff --git a/backend/entities/leadership/base/controller.go b/backend/entities/leadership/base/controller.go index 6df7616f6..ee8efc6b7 100644 --- a/backend/entities/leadership/base/controller.go +++ b/backend/entities/leadership/base/controller.go @@ -31,12 +31,12 @@ func NewLeaderController(leaderService LeaderServiceInterface) *LeaderController // @Failure 500 {string} error // @Router /leader/ [get] func (l *LeaderController) GetLeadership(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - Leaders, err := l.leaderService.GetLeaders(*pagination) + Leaders, err := l.leaderService.GetLeaders(*pageInfo) if err != nil { return err } diff --git a/backend/entities/socials/base/controller.go b/backend/entities/socials/base/controller.go index 0b2708004..7fa004dc2 100644 --- a/backend/entities/socials/base/controller.go +++ b/backend/entities/socials/base/controller.go @@ -54,12 +54,12 @@ func (co *SocialController) GetSocial(c *fiber.Ctx) error { // @Failure 500 {string} error // @Router /socials/ [get] func (co *SocialController) GetSocials(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - socials, err := co.socialService.GetSocials(*pagination) + socials, err := co.socialService.GetSocials(*pageInfo) if err != nil { return err } diff --git a/backend/entities/users/base/controller.go b/backend/entities/users/base/controller.go index 91c596acc..2cfcfea47 100644 --- a/backend/entities/users/base/controller.go +++ b/backend/entities/users/base/controller.go @@ -34,12 +34,12 @@ func NewUserController(userService UserServiceInterface) *UserController { // @Failure 500 {object} error // @Router /users/ [get] func (u *UserController) GetUsers(c *fiber.Ctx) error { - pagination, ok := fiberpaginate.FromContext(c) + pageInfo, ok := fiberpaginate.FromContext(c) if !ok { - return utilities.ErrExpectedPagination + return utilities.ErrExpectedPageInfo } - users, err := u.userService.GetUsers(*pagination) + users, err := u.userService.GetUsers(*pageInfo) if err != nil { return err } diff --git a/backend/utilities/api_error.go b/backend/utilities/api_error.go index f4ecc154e..7d6e297e6 100644 --- a/backend/utilities/api_error.go +++ b/backend/utilities/api_error.go @@ -10,9 +10,9 @@ import ( ) var ( - ErrNotFound = errors.New("not found") - ErrDuplicate = errors.New("duplicate") - ErrExpectedPagination = errors.New("expected pagination. make sure to use the fiberpaginate middleware") + ErrNotFound = errors.New("not found") + ErrDuplicate = errors.New("duplicate") + ErrExpectedPageInfo = errors.New("expected page info. make sure to use the fiberpaginate middleware") ) func IsNotFound(err error) bool { @@ -23,8 +23,8 @@ func IsDuplicate(err error) bool { return errors.Is(err, ErrDuplicate) } -func IsExpectedPagination(err error) bool { - return errors.Is(err, ErrExpectedPagination) +func IsExpectedPageInfo(err error) bool { + return errors.Is(err, ErrExpectedPageInfo) } type APIError struct { @@ -86,7 +86,7 @@ func ErrorHandler(c *fiber.Ctx, err error) error { apiErr = NewAPIError(http.StatusNotFound, err) case ErrDuplicate: apiErr = NewAPIError(http.StatusConflict, err) - case ErrExpectedPagination: + case ErrExpectedPageInfo: apiErr = NewAPIError(http.StatusInternalServerError, err) default: if castedErr, ok := err.(APIError); ok {