Skip to content

Commit

Permalink
Merge branch 'main' into club-search
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-brennan2005 committed May 14, 2024
2 parents 2487089 + 852f5b1 commit d594117
Show file tree
Hide file tree
Showing 273 changed files with 12,291 additions and 5,421 deletions.
6 changes: 5 additions & 1 deletion backend/constants/email.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package constants

const DOMAIN string = "@generatesac.davidoduneye.com"
const (
DOMAIN string = "@generatesac.davidoduneye.com"
ONBOARDING_EMAIL string = "onboarding" + DOMAIN
DEFAULT_FROM_EMAIL string = "no-reply" + DOMAIN
)
5 changes: 5 additions & 0 deletions backend/constants/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

const (
PAGINATION_MIDDLEWARE_KEY string = "pagination"
)
7 changes: 0 additions & 7 deletions backend/constants/pagination.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package constants

import "strconv"

const (
DEFAULT_LIMIT int = 10
DEFAULT_PAGE int = 1
)

var (
DEFAULT_LIMIT_STRING string = strconv.Itoa(DEFAULT_LIMIT)
DEFAULT_PAGE_STRING string = strconv.Itoa(DEFAULT_PAGE)
)
15 changes: 7 additions & 8 deletions backend/entities/auth/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package base

import (
"github.com/GenerateNU/sac/backend/types"
"github.com/GenerateNU/sac/backend/utilities"
)

func Auth(params types.RouteParams) AuthServiceInterface {
Expand All @@ -12,17 +11,17 @@ func Auth(params types.RouteParams) AuthServiceInterface {
// api/v1/auth/*
auth := params.Router.Group("/auth")

auth.Post("/logout", utilities.Make(authController.Logout))
auth.Post("/login", utilities.Make(authController.Login))
auth.Post("/refresh", utilities.Make(authController.Refresh))
auth.Post("/logout", authController.Logout)
auth.Post("/login", authController.Login)
auth.Post("/refresh", authController.Refresh)

// TODO: rate limit
auth.Post("/send-code", utilities.Make(authController.SendCode))
auth.Post("/verify-email", utilities.Make(authController.VerifyEmail))
auth.Post("/send-code", authController.SendCode)
auth.Post("/verify-email", authController.VerifyEmail)

// TODO: rate limit
auth.Post("/forgot-password", utilities.Make(authController.ForgotPassword))
auth.Post("/verify-reset", utilities.Make(authController.VerifyPasswordResetToken))
auth.Post("/forgot-password", authController.ForgotPassword)
auth.Post("/verify-reset", authController.VerifyPasswordResetToken)

return authService
}
9 changes: 7 additions & 2 deletions backend/entities/categories/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package base
import (
"net/http"

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

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -64,7 +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 {
categories, err := cat.categoryService.GetCategories(c.Query("limit", constants.DEFAULT_LIMIT_STRING), c.Query("page", constants.DEFAULT_PAGE_STRING))
pagination, err := middleware.GetPagination(c)
if err != nil {
return err
}

categories, err := cat.categoryService.GetCategories(*pagination)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 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/GenerateNU/sac/backend/utilities"
"github.com/gofiber/fiber/v2"
)

Expand All @@ -23,15 +23,15 @@ func Category(categoryParams types.RouteParams) fiber.Router {
// api/v1/categories/*
categories := categoryParams.Router.Group("/categories")

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

// api/v1/categories/:categoryID/*
categoryID := categories.Group("/:categoryID")

categoryID.Get("/", categoryController.GetCategory)
categoryID.Delete("/", categoryParams.AuthMiddleware.Authorize(auth.DeleteAll), utilities.Make(categoryController.DeleteCategory))
categoryID.Patch("/", categoryParams.AuthMiddleware.Authorize(auth.WriteAll), utilities.Make(categoryController.UpdateCategory))
categoryID.Delete("/", categoryParams.AuthMiddleware.Authorize(auth.DeleteAll), categoryController.DeleteCategory)
categoryID.Patch("/", categoryParams.AuthMiddleware.Authorize(auth.WriteAll), categoryController.UpdateCategory)

return categoryID
}
17 changes: 4 additions & 13 deletions backend/entities/categories/base/service.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/models"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/types"
"github.com/GenerateNU/sac/backend/utilities"

Expand All @@ -11,7 +12,7 @@ import (

type CategoryServiceInterface interface {
CreateCategory(categoryBody models.CategoryRequestBody) (*models.Category, error)
GetCategories(limit string, page string) ([]models.Category, error)
GetCategories(pagination middleware.Pagination) ([]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 @@ -40,18 +41,8 @@ func (c *CategoryService) CreateCategory(categoryBody models.CategoryRequestBody
return CreateCategory(c.DB, *category)
}

func (c *CategoryService) GetCategories(limit string, page string) ([]models.Category, error) {
limitAsInt, err := utilities.ValidateNonNegative(limit)
if err != nil {
return nil, err
}

pageAsInt, err := utilities.ValidateNonNegative(page)
if err != nil {
return nil, err
}

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

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

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

Expand All @@ -22,12 +23,10 @@ func CreateCategory(db *gorm.DB, category models.Category) (*models.Category, er
return &category, nil
}

func GetCategories(db *gorm.DB, limit int, page int) ([]models.Category, error) {
func GetCategories(db *gorm.DB, pagination middleware.Pagination) ([]models.Category, error) {
var categories []models.Category

offset := (page - 1) * limit

if err := db.Limit(limit).Offset(offset).Find(&categories).Error; err != nil {
if err := db.Scopes(pagination.IntoScope(db)).Find(&categories).Error; err != nil {
return nil, err
}

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

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

"github.com/gofiber/fiber/v2"
)
Expand Down Expand Up @@ -32,7 +32,12 @@ func NewCategoryTagController(categoryTagService CategoryTagServiceInterface) *C
// @Failure 500 {object} error
// @Router /categories/{categoryID}/tags/ [get]
func (ct *CategoryTagController) GetTagsByCategory(c *fiber.Ctx) error {
tags, err := ct.categoryTagService.GetTagsByCategory(c.Params("categoryID"), c.Query("limit", constants.DEFAULT_LIMIT_STRING), c.Query("page", constants.DEFAULT_PAGE_STRING))
pagination, err := middleware.GetPagination(c)
if err != nil {
return err
}

tags, err := ct.categoryTagService.GetTagsByCategory(c.Params("categoryID"), *pagination)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 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/GenerateNU/sac/backend/utilities"
)

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("/", utilities.Make(categoryTagController.GetTagsByCategory))
categoryTags.Get("/:tagID", utilities.Make(categoryTagController.GetTagByCategory))
categoryTags.Get("/", middleware.Paginate, categoryTagController.GetTagsByCategory)
categoryTags.Get("/:tagID", categoryTagController.GetTagByCategory)
}
17 changes: 4 additions & 13 deletions backend/entities/categories/tag/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +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"
)

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

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

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

limitAsInt, err := utilities.ValidateNonNegative(limit)
if err != nil {
return nil, err
}

pageAsInt, err := utilities.ValidateNonNegative(page)
if err != nil {
return nil, err
}

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

func (t *CategoryTagService) GetTagByCategory(categoryID string, tagID string) (*models.Tag, error) {
Expand Down
7 changes: 3 additions & 4 deletions backend/entities/categories/tag/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"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, limit int, page int) ([]models.Tag, error) {
func GetTagsByCategory(db *gorm.DB, categoryID uuid.UUID, pagination middleware.Pagination) ([]models.Tag, error) {
var category models.Category

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

var tags []models.Tag

offset := (page - 1) * limit

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

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

"github.com/GenerateNU/sac/backend/constants"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/gofiber/fiber/v2"
)
Expand Down Expand Up @@ -32,15 +32,16 @@ func NewClubController(clubService ClubServiceInterface) *ClubController {
// @Router /clubs/ [get]
func (cl *ClubController) GetClubs(c *fiber.Ctx) error {
var queryParams models.ClubQueryParams

queryParams.Limit = constants.DEFAULT_LIMIT
queryParams.Page = constants.DEFAULT_PAGE

if err := c.QueryParser(&queryParams); err != nil {
return err
}

clubs, err := cl.clubService.GetClubs(&queryParams)
pagination, err := middleware.GetPagination(c)
if err != nil {
return err
}

clubs, err := cl.clubService.GetClubs(&queryParams, *pagination)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 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/utilities"
"github.com/GenerateNU/sac/backend/middleware"

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

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

// api/v1/clubs/:clubID/*
clubID := clubs.Group("/:clubID")

clubID.Get("/", utilities.Make(clubController.GetClub))
clubID.Patch("/", clubParams.AuthMiddleware.ClubAuthorizeById, utilities.Make(clubController.UpdateClub))
clubID.Delete("/", clubParams.AuthMiddleware.Authorize(p.DeleteAll), utilities.Make(clubController.DeleteClub))
clubID.Get("/", clubController.GetClub)
clubID.Patch("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubController.UpdateClub)
clubID.Delete("/", clubParams.AuthMiddleware.Authorize(p.DeleteAll), clubController.DeleteClub)

return clubID
}
15 changes: 4 additions & 11 deletions backend/entities/clubs/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +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"
)

type ClubServiceInterface interface {
GetClubs(queryParams *models.ClubQueryParams) ([]models.Club, error)
GetClubs(queryParams *models.ClubQueryParams, pagination middleware.Pagination) ([]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 @@ -25,16 +26,8 @@ func NewClubService(serviceParams types.ServiceParams) ClubServiceInterface {
return &ClubService{serviceParams}
}

func (c *ClubService) GetClubs(queryParams *models.ClubQueryParams) ([]models.Club, error) {
if queryParams.Limit < 0 {
return nil, fmt.Errorf("limit must be greater than 0")
}

if queryParams.Page < 0 {
return nil, fmt.Errorf("page must be greater than 0")
}

return GetClubs(c.DB, queryParams)
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) CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, error) {
Expand Down
4 changes: 3 additions & 1 deletion backend/entities/clubs/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package base
import (
"errors"

"github.com/GenerateNU/sac/backend/integrations/search"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/utilities"

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

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

if queryParams.Tags != nil && len(queryParams.Tags) > 0 {
Expand Down
Loading

0 comments on commit d594117

Please sign in to comment.