Skip to content

Commit

Permalink
nested route refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 2, 2024
1 parent e2c3dbb commit 402c995
Show file tree
Hide file tree
Showing 18 changed files with 640 additions and 609 deletions.
24 changes: 24 additions & 0 deletions backend/src/controllers/category_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controllers

import (
"github.com/GenerateNU/sac/backend/src/services"

"github.com/gofiber/fiber/v2"
)

type CategoryTagController struct {
categoryTagService services.CategoryTagServiceInterface
}

func NewCategoryTagController(categoryTagService services.CategoryTagServiceInterface) *CategoryTagController {
return &CategoryTagController{categoryTagService: categoryTagService}
}

func (t *CategoryTagController) GetTagByCategory(c *fiber.Ctx) error {
tag, err := t.categoryTagService.GetTagByCategory(c.Params("categoryID"), c.Params("tagID"))
if err != nil {
return err.FiberError(c)
}

return c.Status(fiber.StatusOK).JSON(&tag)
}
8 changes: 4 additions & 4 deletions backend/src/controllers/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (t *TagController) CreateTag(c *fiber.Ctx) error {
return errors.FailedToParseRequestBody.FiberError(c)
}

dbTag, err := t.tagService.CreateTag(c.Params("categoryID"), tagBody)
dbTag, err := t.tagService.CreateTag(tagBody)
if err != nil {
return err.FiberError(c)
}
Expand All @@ -58,7 +58,7 @@ func (t *TagController) CreateTag(c *fiber.Ctx) error {
// @Failure 500 {string} string "failed to retrieve tag"
// @Router /api/v1/tags/{id} [get]
func (t *TagController) GetTag(c *fiber.Ctx) error {
tag, err := t.tagService.GetTag(c.Params("categoryID"), c.Params("tagID"))
tag, err := t.tagService.GetTag(c.Params("tagID"))
if err != nil {
return err.FiberError(c)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (t *TagController) UpdateTag(c *fiber.Ctx) error {
return errors.FailedToParseRequestBody.FiberError(c)
}

tag, err := t.tagService.UpdateTag(c.Params("categoryID"), c.Params("tagID"), tagBody)
tag, err := t.tagService.UpdateTag(c.Params("tagID"), tagBody)
if err != nil {
return err.FiberError(c)
}
Expand All @@ -110,7 +110,7 @@ func (t *TagController) UpdateTag(c *fiber.Ctx) error {
// @Failure 500 {string} string "failed to delete tag"
// @Router /api/v1/tags/{id} [delete]
func (t *TagController) DeleteTag(c *fiber.Ctx) error {
err := t.tagService.DeleteTag(c.Params("categoryID"), c.Params("tagID"))
err := t.tagService.DeleteTag(c.Params("tagID"))
if err != nil {
return err.FiberError(c)
}
Expand Down
22 changes: 0 additions & 22 deletions backend/src/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,3 @@ func (u *UserController) DeleteUser(c *fiber.Ctx) error {

return c.SendStatus(fiber.StatusNoContent)
}

func (u *UserController) GetUserTags(c *fiber.Ctx) error {
tags, err := u.userService.GetUserTags(c.Params("uid"))
if err != nil {
return err.FiberError(c)
}
return c.Status(fiber.StatusOK).JSON(&tags)
}

func (u *UserController) CreateUserTags(c *fiber.Ctx) error {
var requestBody models.CreateUserTagsBody
if err := c.BodyParser(&requestBody); err != nil {
return errors.FailedToParseRequestBody.FiberError(c)
}

tags, err := u.userService.CreateUserTags(c.Params("uid"), requestBody)
if err != nil {
return err.FiberError(c)
}

return c.Status(fiber.StatusCreated).JSON(&tags)
}
38 changes: 38 additions & 0 deletions backend/src/controllers/user_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package controllers

import (
"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
)

type UserTagController struct {
userTagService services.UserTagServiceInterface
}

func NewUserTagController(userTagService services.UserTagServiceInterface) *UserTagController {
return &UserTagController{userTagService: userTagService}
}

func (u *UserTagController) GetUserTags(c *fiber.Ctx) error {
tags, err := u.userTagService.GetUserTags(c.Params("userID"))
if err != nil {
return err.FiberError(c)
}
return c.Status(fiber.StatusOK).JSON(&tags)
}

func (u *UserTagController) CreateUserTags(c *fiber.Ctx) error {
var requestBody models.CreateUserTagsBody
if err := c.BodyParser(&requestBody); err != nil {
return errors.FailedToParseRequestBody.FiberError(c)
}

tags, err := u.userTagService.CreateUserTags(c.Params("userID"), requestBody)
if err != nil {
return err.FiberError(c)
}

return c.Status(fiber.StatusCreated).JSON(&tags)
}
3 changes: 2 additions & 1 deletion backend/src/models/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ type Tag struct {
}

type TagRequestBody struct {
Name string `json:"name" validate:"required,max=255"`
Name string `json:"name" validate:"required,max=255"`
CategoryID uuid.UUID `json:"category_id" validate:"required,uuid4"`
}
32 changes: 23 additions & 9 deletions backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ func Init(db *gorm.DB, settings config.Settings) *fiber.App {

utilityRoutes(app)
authRoutes(apiv1, services.NewAuthService(db, validate), settings.Auth)
userRoutes(apiv1, services.NewUserService(db, validate), middlewareService)
userRouter := userRoutes(apiv1, services.NewUserService(db, validate), middlewareService)
userTagRouter(userRouter, services.NewUserTagService(db, validate))
clubRoutes(apiv1, services.NewClubService(db, validate), middlewareService)
categoryRouter := categoryRoutes(apiv1, services.NewCategoryService(db, validate))
tagRoutes(categoryRouter, services.NewTagService(db, validate))
tagRoutes(apiv1, services.NewTagService(db, validate))
categoryTagRoutes(categoryRouter, services.NewCategoryTagService(db, validate))

return app
}
Expand Down Expand Up @@ -69,7 +71,7 @@ func utilityRoutes(router fiber.Router) {
})
}

func userRoutes(router fiber.Router, userService services.UserServiceInterface, middlewareService middleware.MiddlewareInterface) {
func userRoutes(router fiber.Router, userService services.UserServiceInterface, middlewareService middleware.MiddlewareInterface) fiber.Router {
userController := controllers.NewUserController(userService)

// api/v1/users/*
Expand All @@ -85,17 +87,21 @@ func userRoutes(router fiber.Router, userService services.UserServiceInterface,
usersID.Patch("/", middlewareService.Authorize(types.UserWrite), userController.UpdateUser)
usersID.Delete("/", middlewareService.Authorize(types.UserDelete), userController.DeleteUser)

usersID.Post("/tags", userController.CreateUserTags)
usersID.Get("/tags", userController.GetUserTags)
users.Get("/", userController.GetUsers)
users.Get("/:id", userController.GetUser)
users.Patch("/:id", userController.UpdateUser)
users.Delete("/:id", userController.DeleteUser)

userTags := users.Group("/:uid/tags")
return users
}

func userTagRouter(router fiber.Router, userTagService services.UserTagServiceInterface) {
userTagController := controllers.NewUserTagController(userTagService)

userTags.Post("/", userController.CreateUserTags)
userTags.Get("/", userController.GetUserTags)
userTags := router.Group("/:userID/tags")

userTags.Post("/", userTagController.CreateUserTags)
userTags.Get("/", userTagController.GetUserTags)
}

func clubRoutes(router fiber.Router, clubService services.ClubServiceInterface, middlewareService middleware.MiddlewareInterface) {
Expand Down Expand Up @@ -143,10 +149,18 @@ func categoryRoutes(router fiber.Router, categoryService services.CategoryServic
func tagRoutes(router fiber.Router, tagService services.TagServiceInterface) {
tagController := controllers.NewTagController(tagService)

tags := router.Group("/:categoryID/tags")
tags := router.Group("/tags")

tags.Get("/:tagID", tagController.GetTag)
tags.Post("/", tagController.CreateTag)
tags.Patch("/:tagID", tagController.UpdateTag)
tags.Delete("/:tagID", tagController.DeleteTag)
}

func categoryTagRoutes(router fiber.Router, categoryTagService services.CategoryTagServiceInterface) {
categoryTagController := controllers.NewCategoryTagController(categoryTagService)

categoryTags := router.Group("/:categoryID/tags")

categoryTags.Get("/:tagID", categoryTagController.GetTagByCategory)
}
39 changes: 39 additions & 0 deletions backend/src/services/category_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package services

import (
"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/transactions"
"github.com/GenerateNU/sac/backend/src/utilities"
"github.com/go-playground/validator/v10"
"gorm.io/gorm"
)

type CategoryTagServiceInterface interface {
GetTagByCategory(categoryID string, tagID string) (*models.Tag, *errors.Error)
}

type CategoryTagService struct {
DB *gorm.DB
Validate *validator.Validate
}

func NewCategoryTagService(db *gorm.DB, validate *validator.Validate) *CategoryTagService {
return &CategoryTagService{DB: db, Validate: validate}
}

func (t *CategoryTagService) GetTagByCategory(categoryID string, tagID string) (*models.Tag, *errors.Error) {
categoryIDAsUUID, idErr := utilities.ValidateID(categoryID)

if idErr != nil {
return nil, idErr
}

tagIDAsUUID, idErr := utilities.ValidateID(tagID)

if idErr != nil {
return nil, idErr
}

return transactions.GetTagByCategory(t.DB, *categoryIDAsUUID, *tagIDAsUUID)
}
48 changes: 10 additions & 38 deletions backend/src/services/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

type TagServiceInterface interface {
CreateTag(categoryID string, tagBody models.TagRequestBody) (*models.Tag, *errors.Error)
GetTag(categoryID string, tagID string) (*models.Tag, *errors.Error)
UpdateTag(categoryID string, tagID string, tagBody models.TagRequestBody) (*models.Tag, *errors.Error)
DeleteTag(categoryID string, tagID string) *errors.Error
CreateTag(tagBody models.TagRequestBody) (*models.Tag, *errors.Error)
GetTag(tagID string) (*models.Tag, *errors.Error)
UpdateTag(tagID string, tagBody models.TagRequestBody) (*models.Tag, *errors.Error)
DeleteTag(tagID string) *errors.Error
}

type TagService struct {
Expand All @@ -25,13 +25,7 @@ func NewTagService(db *gorm.DB, validate *validator.Validate) *TagService {
return &TagService{DB: db, Validate: validate}
}

func (t *TagService) CreateTag(categoryID string, tagBody models.TagRequestBody) (*models.Tag, *errors.Error) {
categoryIDAsUUID, idErr := utilities.ValidateID(categoryID)

if idErr != nil {
return nil, idErr
}

func (t *TagService) CreateTag(tagBody models.TagRequestBody) (*models.Tag, *errors.Error) {
if err := t.Validate.Struct(tagBody); err != nil {
return nil, &errors.FailedToValidateTag
}
Expand All @@ -41,34 +35,20 @@ func (t *TagService) CreateTag(categoryID string, tagBody models.TagRequestBody)
return nil, &errors.FailedToMapRequestToModel
}

tag.CategoryID = *categoryIDAsUUID

return transactions.CreateTag(t.DB, *tag)
}

func (t *TagService) GetTag(categoryID string, tagID string) (*models.Tag, *errors.Error) {
categoryIDAsUUID, idErr := utilities.ValidateID(categoryID)

if idErr != nil {
return nil, idErr
}

func (t *TagService) GetTag(tagID string) (*models.Tag, *errors.Error) {
tagIDAsUUID, idErr := utilities.ValidateID(tagID)

if idErr != nil {
return nil, idErr
}

return transactions.GetTag(t.DB, *categoryIDAsUUID, *tagIDAsUUID)
return transactions.GetTag(t.DB, *tagIDAsUUID)
}

func (t *TagService) UpdateTag(categoryID string, tagID string, tagBody models.TagRequestBody) (*models.Tag, *errors.Error) {
categoryIDAsUUID, idErr := utilities.ValidateID(categoryID)

if idErr != nil {
return nil, idErr
}

func (t *TagService) UpdateTag(tagID string, tagBody models.TagRequestBody) (*models.Tag, *errors.Error) {
tagIDAsUUID, idErr := utilities.ValidateID(tagID)

if idErr != nil {
Expand All @@ -84,23 +64,15 @@ func (t *TagService) UpdateTag(categoryID string, tagID string, tagBody models.T
return nil, &errors.FailedToMapRequestToModel
}

tag.CategoryID = *categoryIDAsUUID

return transactions.UpdateTag(t.DB, *tagIDAsUUID, *tag)
}

func (t *TagService) DeleteTag(categoryID string, tagID string) *errors.Error {
categoryIDAsUUID, idErr := utilities.ValidateID(categoryID)

if idErr != nil {
return idErr
}

func (t *TagService) DeleteTag(tagID string) *errors.Error {
tagIDAsUUID, idErr := utilities.ValidateID(tagID)

if idErr != nil {
return idErr
}

return transactions.DeleteTag(t.DB, *categoryIDAsUUID, *tagIDAsUUID)
return transactions.DeleteTag(t.DB, *tagIDAsUUID)
}
32 changes: 0 additions & 32 deletions backend/src/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type UserServiceInterface interface {
GetUser(id string) (*models.User, *errors.Error)
UpdateUser(id string, userBody models.UpdateUserRequestBody) (*models.User, *errors.Error)
DeleteUser(id string) *errors.Error
GetUserTags(id string) ([]models.Tag, *errors.Error)
CreateUserTags(id string, tagIDs models.CreateUserTagsBody) ([]models.Tag, *errors.Error)
}

type UserService struct {
Expand Down Expand Up @@ -111,33 +109,3 @@ func (u *UserService) DeleteUser(id string) *errors.Error {

return transactions.DeleteUser(u.DB, *idAsUUID)
}

func (u *UserService) GetUserTags(id string) ([]models.Tag, *errors.Error) {
idAsUUID, err := utilities.ValidateID(id)
if err != nil {
return nil, err
}

return transactions.GetUserTags(u.DB, *idAsUUID)
}

func (u *UserService) CreateUserTags(id string, tagIDs models.CreateUserTagsBody) ([]models.Tag, *errors.Error) {
// Validate the id:
idAsUUID, err := utilities.ValidateID(id)
if err != nil {
return nil, err
}

if err := u.Validate.Struct(tagIDs); err != nil {
return nil, &errors.FailedToValidateUserTags
}

// Retrieve a list of valid tags from the ids:
tags, err := transactions.GetTagsByIDs(u.DB, tagIDs.Tags)
if err != nil {
return nil, err
}

// Update the user to reflect the new tags:
return transactions.CreateUserTags(u.DB, *idAsUUID, tags)
}
Loading

0 comments on commit 402c995

Please sign in to comment.