Skip to content

Commit

Permalink
controller and transactions complete | rename from 'pagination' -> 'p…
Browse files Browse the repository at this point in the history
…ageInfo'
  • Loading branch information
garrettladley committed May 31, 2024
1 parent 8152032 commit ac90e21
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 124 deletions.
6 changes: 3 additions & 3 deletions backend/entities/categories/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/categories/tags/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/clubs/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/clubs/events/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/clubs/followers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/clubs/members/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
39 changes: 35 additions & 4 deletions backend/entities/clubs/recruitment/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package recruitment
import (
"net/http"

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

Expand Down Expand Up @@ -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)
}
142 changes: 61 additions & 81 deletions backend/entities/clubs/recruitment/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions backend/entities/events/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/files/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/leadership/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit ac90e21

Please sign in to comment.