Skip to content

Commit

Permalink
Merge branch 'dashboard-state-mngmnt' of https://github.com/GenerateN…
Browse files Browse the repository at this point in the history
…U/sac into dashboard-state-mngmnt
  • Loading branch information
in-mai-space committed May 16, 2024
2 parents 5d5bc94 + 60de11c commit 55e2d29
Show file tree
Hide file tree
Showing 52 changed files with 179 additions and 231 deletions.
5 changes: 0 additions & 5 deletions backend/constants/middleware.go

This file was deleted.

6 changes: 0 additions & 6 deletions backend/constants/pagination.go

This file was deleted.

8 changes: 4 additions & 4 deletions backend/entities/categories/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"

"github.com/gofiber/fiber/v2"
)
Expand Down Expand Up @@ -64,9 +64,9 @@ func (cat *CategoryController) CreateCategory(c *fiber.Ctx) error {
// @Failure 500 {string} error
// @Router /categories/ [get]
func (cat *CategoryController) GetCategories(c *fiber.Ctx) error {
pagination, err := middleware.GetPagination(c)
if err != nil {
return err
pagination, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPagination
}

categories, err := cat.categoryService.GetCategories(*pagination)
Expand Down
4 changes: 2 additions & 2 deletions backend/entities/categories/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package base
import (
"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/entities/categories/tag"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/types"
"github.com/garrettladley/fiberpaginate"
"github.com/gofiber/fiber/v2"
)

Expand All @@ -24,7 +24,7 @@ func Category(categoryParams types.RouteParams) fiber.Router {
categories := categoryParams.Router.Group("/categories")

categories.Post("/", categoryParams.AuthMiddleware.Authorize(auth.CreateAll), categoryController.CreateCategory)
categories.Get("/", middleware.Paginate, categoryController.GetCategories)
categories.Get("/", fiberpaginate.New(), categoryController.GetCategories)

// api/v1/categories/:categoryID/*
categoryID := categories.Group("/:categoryID")
Expand Down
8 changes: 4 additions & 4 deletions backend/entities/categories/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package base

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

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

type CategoryServiceInterface interface {
CreateCategory(categoryBody models.CategoryRequestBody) (*models.Category, error)
GetCategories(pagination middleware.Pagination) ([]models.Category, error)
GetCategories(pageInfo fiberpaginate.PageInfo) ([]models.Category, error)
GetCategory(id string) (*models.Category, error)
UpdateCategory(id string, params models.CategoryRequestBody) (*models.Category, error)
DeleteCategory(id string) error
Expand Down Expand Up @@ -41,8 +41,8 @@ func (c *CategoryService) CreateCategory(categoryBody models.CategoryRequestBody
return CreateCategory(c.DB, *category)
}

func (c *CategoryService) GetCategories(pagination middleware.Pagination) ([]models.Category, error) {
return GetCategories(c.DB, pagination)
func (c *CategoryService) GetCategories(pageInfo fiberpaginate.PageInfo) ([]models.Category, error) {
return GetCategories(c.DB, pageInfo)
}

func (c *CategoryService) GetCategory(id string) (*models.Category, error) {
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/categories/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package base
import (
"errors"

"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
"github.com/google/uuid"

"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -23,10 +23,10 @@ func CreateCategory(db *gorm.DB, category models.Category) (*models.Category, er
return &category, nil
}

func GetCategories(db *gorm.DB, pagination middleware.Pagination) ([]models.Category, error) {
func GetCategories(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]models.Category, error) {
var categories []models.Category

if err := db.Scopes(pagination.IntoScope(db)).Find(&categories).Error; err != nil {
if err := db.Scopes(utilities.IntoScope(pageInfo, db)).Find(&categories).Error; err != nil {
return nil, err
}

Expand Down
10 changes: 5 additions & 5 deletions backend/entities/categories/tag/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package tag
import (
"net/http"

"github.com/GenerateNU/sac/backend/middleware"

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

Expand Down Expand Up @@ -32,9 +32,9 @@ func NewCategoryTagController(categoryTagService CategoryTagServiceInterface) *C
// @Failure 500 {object} error
// @Router /categories/{categoryID}/tags/ [get]
func (ct *CategoryTagController) GetTagsByCategory(c *fiber.Ctx) error {
pagination, err := middleware.GetPagination(c)
if err != nil {
return err
pagination, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPagination
}

tags, err := ct.categoryTagService.GetTagsByCategory(c.Params("categoryID"), *pagination)
Expand Down
4 changes: 2 additions & 2 deletions backend/entities/categories/tag/routes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package tag

import (
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/types"
"github.com/garrettladley/fiberpaginate"
)

func CategoryTag(categoryParams types.RouteParams) {
Expand All @@ -11,6 +11,6 @@ func CategoryTag(categoryParams types.RouteParams) {
// api/v1/categories/:categoryID/tags/*
categoryTags := categoryParams.Router.Group("/tags")

categoryTags.Get("/", middleware.Paginate, categoryTagController.GetTagsByCategory)
categoryTags.Get("/", fiberpaginate.New(), categoryTagController.GetTagsByCategory)
categoryTags.Get("/:tagID", categoryTagController.GetTagByCategory)
}
8 changes: 4 additions & 4 deletions backend/entities/categories/tag/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package tag

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

type CategoryTagServiceInterface interface {
GetTagsByCategory(categoryID string, pagination middleware.Pagination) ([]models.Tag, error)
GetTagsByCategory(categoryID string, pageInfo fiberpaginate.PageInfo) ([]models.Tag, error)
GetTagByCategory(categoryID string, tagID string) (*models.Tag, error)
}

Expand All @@ -20,13 +20,13 @@ func NewCategoryTagService(params types.ServiceParams) CategoryTagServiceInterfa
return &CategoryTagService{params}
}

func (t *CategoryTagService) GetTagsByCategory(categoryID string, pagination middleware.Pagination) ([]models.Tag, error) {
func (t *CategoryTagService) GetTagsByCategory(categoryID string, pageInfo fiberpaginate.PageInfo) ([]models.Tag, error) {
categoryIDAsUUID, err := utilities.ValidateID(categoryID)
if err != nil {
return nil, err
}

return GetTagsByCategory(t.DB, *categoryIDAsUUID, pagination)
return GetTagsByCategory(t.DB, *categoryIDAsUUID, pageInfo)
}

func (t *CategoryTagService) GetTagByCategory(categoryID string, tagID string) (*models.Tag, error) {
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/categories/tag/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package tag
import (
"errors"

"github.com/garrettladley/fiberpaginate"
"github.com/google/uuid"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/utilities"

"gorm.io/gorm"
)

func GetTagsByCategory(db *gorm.DB, categoryID uuid.UUID, pagination middleware.Pagination) ([]models.Tag, error) {
func GetTagsByCategory(db *gorm.DB, categoryID uuid.UUID, pageInfo fiberpaginate.PageInfo) ([]models.Tag, error) {
var category models.Category

if err := db.Where("id = ?", categoryID).First(&category).Error; err != nil {
Expand All @@ -24,7 +24,7 @@ func GetTagsByCategory(db *gorm.DB, categoryID uuid.UUID, pagination middleware.

var tags []models.Tag

if err := db.Where("category_id = ?", categoryID).Scopes(pagination.IntoScope(db)).Find(&tags).Error; err != nil {
if err := db.Where("category_id = ?", categoryID).Scopes(utilities.IntoScope(pageInfo, db)).Find(&tags).Error; err != nil {
return nil, err
}

Expand Down
8 changes: 4 additions & 4 deletions backend/entities/clubs/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"

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

Expand Down Expand Up @@ -36,9 +36,9 @@ func (cl *ClubController) GetClubs(c *fiber.Ctx) error {
return err
}

pagination, err := middleware.GetPagination(c)
if err != nil {
return err
pagination, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPagination
}

clubs, err := cl.clubService.GetClubs(&queryParams, *pagination)
Expand Down
4 changes: 2 additions & 2 deletions backend/entities/clubs/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/GenerateNU/sac/backend/entities/clubs/member"
"github.com/GenerateNU/sac/backend/entities/clubs/poc"
"github.com/GenerateNU/sac/backend/entities/clubs/tag"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/garrettladley/fiberpaginate"

"github.com/GenerateNU/sac/backend/types"
"github.com/gofiber/fiber/v2"
Expand All @@ -34,7 +34,7 @@ func ClubRouter(clubParams types.RouteParams) fiber.Router {
// api/v1/clubs/*
clubs := clubParams.Router.Group("/clubs")

clubs.Get("/", middleware.Paginate, clubController.GetClubs)
clubs.Get("/", fiberpaginate.New(), clubController.GetClubs)
clubs.Post("/", clubParams.AuthMiddleware.Authorize(p.CreateAll), clubController.CreateClub)

// api/v1/clubs/:clubID/*
Expand Down
8 changes: 4 additions & 4 deletions backend/entities/clubs/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (

"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/types"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
)

type ClubServiceInterface interface {
GetClubs(queryParams *models.ClubQueryParams, pagination middleware.Pagination) ([]models.Club, error)
GetClubs(queryParams *models.ClubQueryParams, pageInfo fiberpaginate.PageInfo) ([]models.Club, error)
GetClub(id string) (*models.Club, error)
CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, error)
UpdateClub(id string, clubBody models.UpdateClubRequestBody) (*models.Club, error)
Expand All @@ -26,8 +26,8 @@ func NewClubService(serviceParams types.ServiceParams) ClubServiceInterface {
return &ClubService{serviceParams}
}

func (c *ClubService) GetClubs(queryParams *models.ClubQueryParams, pagination middleware.Pagination) ([]models.Club, error) {
return GetClubs(c.DB, c.Integrations.Search, queryParams, pagination)
func (c *ClubService) GetClubs(queryParams *models.ClubQueryParams, pageInfo fiberpaginate.PageInfo) ([]models.Club, error) {
return GetClubs(c.DB, c.Integrations.Search, queryParams, pageInfo)
}

func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, error) {
Expand Down
6 changes: 3 additions & 3 deletions backend/entities/clubs/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"

"github.com/GenerateNU/sac/backend/integrations/search"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
"github.com/sahilm/fuzzy"

"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -15,7 +15,7 @@ import (
"gorm.io/gorm"
)

func GetClubs(db *gorm.DB, pinecone search.SearchClientInterface, queryParams *models.ClubQueryParams, pagination middleware.Pagination) ([]models.Club, error) {
func GetClubs(db *gorm.DB, pinecone search.SearchClientInterface, queryParams *models.ClubQueryParams, pageInfo fiberpaginate.PageInfo) ([]models.Club, error) {
query := db.Model(&models.Club{})

if queryParams.Tags != nil && len(queryParams.Tags) > 0 {
Expand Down Expand Up @@ -72,7 +72,7 @@ func GetClubs(db *gorm.DB, pinecone search.SearchClientInterface, queryParams *m
return clubsSorted, nil
}

if err := query.Scopes(pagination.IntoScope(query)).Find(&clubs).Error; err != nil {
if err := query.Scopes(utilities.IntoScope(pageInfo, query)).Find(&clubs).Error; err != nil {
return nil, err
}

Expand Down
9 changes: 5 additions & 4 deletions backend/entities/clubs/event/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package event
import (
"net/http"

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

Expand Down Expand Up @@ -31,9 +32,9 @@ func NewClubEventController(clubEventService ClubEventServiceInterface) *ClubEve
// @Failure 500 {object} error
// @Router /clubs/{clubID}/events/ [get]
func (cl *ClubEventController) GetClubEvents(c *fiber.Ctx) error {
pagination, err := middleware.GetPagination(c)
if err != nil {
return err
pagination, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPagination
}

if events, err := cl.clubEventService.GetClubEvents(c.Params("clubID"), *pagination); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions backend/entities/clubs/event/routes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package event

import (
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/types"
"github.com/garrettladley/fiberpaginate"
)

func ClubEvent(clubParams types.RouteParams) {
Expand All @@ -11,5 +11,5 @@ func ClubEvent(clubParams types.RouteParams) {
// api/v1/clubs/:clubID/events/*
events := clubParams.Router.Group("/events")

events.Get("/", middleware.Paginate, clubEventController.GetClubEvents)
events.Get("/", fiberpaginate.New(), clubEventController.GetClubEvents)
}
8 changes: 4 additions & 4 deletions backend/entities/clubs/event/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package event

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

type ClubEventServiceInterface interface {
GetClubEvents(clubID string, pagination middleware.Pagination) ([]models.Event, error)
GetClubEvents(clubID string, pageInfo fiberpaginate.PageInfo) ([]models.Event, error)
}

type ClubEventService struct {
Expand All @@ -19,11 +19,11 @@ func NewClubEventService(params types.ServiceParams) ClubEventServiceInterface {
return &ClubEventService{params}
}

func (c *ClubEventService) GetClubEvents(clubID string, pagination middleware.Pagination) ([]models.Event, error) {
func (c *ClubEventService) GetClubEvents(clubID string, pageInfo fiberpaginate.PageInfo) ([]models.Event, error) {
idAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
return nil, err
}

return GetClubEvents(c.DB, *idAsUUID, pagination)
return GetClubEvents(c.DB, *idAsUUID, pageInfo)
}
8 changes: 5 additions & 3 deletions backend/entities/clubs/event/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package event

import (
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/utilities"

"github.com/garrettladley/fiberpaginate"
"github.com/google/uuid"
"gorm.io/gorm"
)

func GetClubEvents(db *gorm.DB, clubID uuid.UUID, pagination middleware.Pagination) ([]models.Event, error) {
func GetClubEvents(db *gorm.DB, clubID uuid.UUID, pageInfo fiberpaginate.PageInfo) ([]models.Event, error) {
var events []models.Event

if err := db.Where("club_id = ?", clubID).Scopes(pagination.IntoScope(db)).Find(&events).Error; err != nil {
if err := db.Where("club_id = ?", clubID).Scopes(utilities.IntoScope(pageInfo, db)).Find(&events).Error; err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit 55e2d29

Please sign in to comment.