Skip to content

Commit

Permalink
Merge branch 'main' into 669-fix-events
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored May 17, 2024
2 parents c90ebe0 + 3805572 commit 92a90a2
Show file tree
Hide file tree
Showing 95 changed files with 10,157 additions and 1,578 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
)
13 changes: 0 additions & 13 deletions backend/constants/pagination.go

This file was deleted.

34 changes: 34 additions & 0 deletions backend/entities/auth/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@ func (a *AuthController) Login(c *fiber.Ctx) error {
return c.Status(http.StatusOK).JSON(user)
}

// Register godoc
//
// @Summary Registers a user
// @Description Registers a user
// @ID register-user
// @Tags auth
// @Accept json
// @Produce json
// @Param userBody body models.CreateUserRequestBody true "User Body"
// @Success 201 {object} models.User
// @Failure 400 {object} error
// @Failure 404 {object} error
// @Failure 500 {object} error
// @Router /auth/register [post]
func (a *AuthController) Register(c *fiber.Ctx) error {
var userBody models.CreateUserRequestBody

if err := c.BodyParser(&userBody); err != nil {
return utilities.InvalidJSON()
}

user, tokens, err := a.authService.Register(userBody)
if err != nil {
return err
}

err = auth.SetResponseTokens(c, tokens)
if err != nil {
return err
}

return c.Status(http.StatusCreated).JSON(user)
}

// Refresh godoc
//
// @Summary Refreshes a user's access token
Expand Down
8 changes: 3 additions & 5 deletions backend/entities/auth/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"github.com/GenerateNU/sac/backend/types"
)

func Auth(params types.RouteParams) AuthServiceInterface {
authService := NewAuthService(params.ServiceParams)
authController := NewAuthController(authService)
func Auth(params types.RouteParams) {
authController := NewAuthController(NewAuthService(params.ServiceParams))

// api/v1/auth/*
auth := params.Router.Group("/auth")

auth.Post("/register", authController.Register)
auth.Post("/logout", authController.Logout)
auth.Post("/login", authController.Login)
auth.Post("/refresh", authController.Refresh)
Expand All @@ -22,6 +22,4 @@ func Auth(params types.RouteParams) AuthServiceInterface {
// TODO: rate limit
auth.Post("/forgot-password", authController.ForgotPassword)
auth.Post("/verify-reset", authController.VerifyPasswordResetToken)

return authService
}
44 changes: 44 additions & 0 deletions backend/entities/auth/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type AuthServiceInterface interface {
GetRole(id string) (*models.UserRole, error)
Register(userBody models.CreateUserRequestBody) (*models.User, *auth.Token, error)
Login(userBody models.LoginUserResponseBody) (*models.User, *auth.Token, error)
Refresh(refreshToken string) (*auth.Token, error)
Logout(c *fiber.Ctx) error
Expand Down Expand Up @@ -79,6 +80,49 @@ func (a *AuthService) Login(loginBody models.LoginUserResponseBody) (*models.Use
return user, tokens, nil
}

func (a *AuthService) Register(userBody models.CreateUserRequestBody) (*models.User, *auth.Token, error) {
if err := utilities.Validate(a.Validate, userBody); err != nil {
return nil, nil, err
}

if err := auth.ValidatePassword(userBody.Password); err != nil {
return nil, nil, err
}

user, err := utilities.MapRequestToModel(userBody, &models.User{})
if err != nil {
return nil, nil, err
}

passwordHash, err := auth.ComputeHash(userBody.Password)
if err != nil {
return nil, nil, err
}

user.Email = utilities.NormalizeEmail(userBody.Email)
user.PasswordHash = *passwordHash

if err := a.Integrations.Email.SendWelcomeEmail(fmt.Sprintf("%s %s", user.FirstName, user.LastName), user.Email); err != nil {
return nil, nil, fmt.Errorf("failed to send welcome email: %w", err)
}

user, err = CreateUser(a.DB, user)
if err != nil {
return nil, nil, err
}

if err := a.SendCode(user.Email); err != nil {
return nil, nil, err
}

_, tokens, err := a.Login(models.LoginUserResponseBody{Email: user.Email, Password: userBody.Password})
if err != nil {
return nil, nil, err
}

return user, tokens, nil
}

func (a *AuthService) Refresh(refreshToken string) (*auth.Token, error) {
claims, err := a.JWT.ExtractClaims(refreshToken, auth.RefreshToken)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions backend/entities/auth/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"gorm.io/gorm"
)

func CreateUser(db *gorm.DB, user *models.User) (*models.User, error) {
if err := db.Create(user).Error; err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
return nil, utilities.ErrDuplicate
}
return nil, err
}

return user, nil
}

func GetToken(db *gorm.DB, token string, tokenType models.VerificationType) (*models.Verification, error) {
tokenModel := models.Verification{}
if err := db.Where("token = ? AND type = ?", token, tokenType).First(&tokenModel).Error; err != nil {
Expand Down
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,9 +3,9 @@ package base
import (
"net/http"

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

"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, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPagination
}

categories, err := cat.categoryService.GetCategories(*pagination)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion backend/entities/categories/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/entities/categories/tag"
"github.com/GenerateNU/sac/backend/types"
"github.com/garrettladley/fiberpaginate"
"github.com/gofiber/fiber/v2"
)

Expand All @@ -23,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("/", categoryController.GetCategories)
categories.Get("/", fiberpaginate.New(), categoryController.GetCategories)

// api/v1/categories/:categoryID/*
categoryID := categories.Group("/:categoryID")
Expand Down
17 changes: 4 additions & 13 deletions backend/entities/categories/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"github.com/GenerateNU/sac/backend/entities/models"
"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(limit string, page string) ([]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 @@ -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(pageInfo fiberpaginate.PageInfo) ([]models.Category, error) {
return GetCategories(c.DB, pageInfo)
}

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 @@ -4,6 +4,7 @@ import (
"errors"

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

"github.com/GenerateNU/sac/backend/entities/models"
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, pageInfo fiberpaginate.PageInfo) ([]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(utilities.IntoScope(pageInfo, db)).Find(&categories).Error; err != nil {
return nil, err
}

Expand Down
11 changes: 8 additions & 3 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/constants"

"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
"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, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPagination
}

tags, err := ct.categoryTagService.GetTagsByCategory(c.Params("categoryID"), *pagination)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion backend/entities/categories/tag/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tag

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

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

categoryTags.Get("/", categoryTagController.GetTagsByCategory)
categoryTags.Get("/", fiberpaginate.New(), 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 @@ -4,10 +4,11 @@ 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 CategoryTagServiceInterface interface {
GetTagsByCategory(categoryID string, limit string, page string) ([]models.Tag, error)
GetTagsByCategory(categoryID string, pageInfo fiberpaginate.PageInfo) ([]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, pageInfo fiberpaginate.PageInfo) ([]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, pageInfo)
}

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 @@ -3,6 +3,7 @@ package tag
import (
"errors"

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

"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -11,7 +12,7 @@ import (
"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, pageInfo fiberpaginate.PageInfo) ([]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(utilities.IntoScope(pageInfo, db)).Find(&tags).Error; err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit 92a90a2

Please sign in to comment.