-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Garrett Ladley <[email protected]> Co-authored-by: garrettladley <[email protected]>
- Loading branch information
1 parent
ca7841b
commit b2874b4
Showing
11 changed files
with
456 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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 ClubTagController struct { | ||
clubTagService services.ClubTagServiceInterface | ||
} | ||
|
||
func NewClubTagController(clubTagService services.ClubTagServiceInterface) *ClubTagController { | ||
return &ClubTagController{clubTagService: clubTagService} | ||
} | ||
|
||
// CreateClubTags godoc | ||
// | ||
// @Summary Create Club Tags | ||
// @Description Adds Tags to a Club | ||
// @ID create-club-tags | ||
// @Tags club | ||
// @Accept json | ||
// @Produce json | ||
// @Success 201 {object} []models.Tag | ||
// @Failure 404 {string} string "club not found" | ||
// @Failure 400 {string} string "invalid request body" | ||
// @Failure 400 {string} string "failed to validate id" | ||
// @Failure 500 {string} string "database error" | ||
// @Router /api/v1/clubs/:id/tags [post] | ||
func (l *ClubTagController) CreateClubTags(c *fiber.Ctx) error { | ||
var clubTagsBody models.CreateClubTagsRequestBody | ||
if err := c.BodyParser(&clubTagsBody); err != nil { | ||
return errors.FailedToParseRequestBody.FiberError(c) | ||
} | ||
|
||
clubTags, err := l.clubTagService.CreateClubTags(c.Params("clubID"), clubTagsBody) | ||
if err != nil { | ||
return err.FiberError(c) | ||
} | ||
|
||
return c.Status(fiber.StatusCreated).JSON(clubTags) | ||
} | ||
|
||
// GetClubTags godoc | ||
// | ||
// @Summary Get Club Tags | ||
// @Description Retrieves the tags for a club | ||
// @ID get-club-tags | ||
// @Tags club | ||
// @Produce json | ||
// @Success 200 {object} []models.Tag | ||
// @Failure 404 {string} string "club not found" | ||
// @Failure 400 {string} string "invalid request body" | ||
// @Failure 400 {string} string "failed to validate id" | ||
// @Failure 500 {string} string "database error" | ||
// @Router /api/v1/clubs/:id/tags [get] | ||
func (l *ClubTagController) GetClubTags(c *fiber.Ctx) error { | ||
clubTags, err := l.clubTagService.GetClubTags(c.Params("clubID")) | ||
if err != nil { | ||
return err.FiberError(c) | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON(clubTags) | ||
} | ||
|
||
// DeleteClubTags godoc | ||
// | ||
// @Summary Delete Club Tags | ||
// @Description Deletes the tags for a club | ||
// @ID delete-club-tags | ||
// @Tags club | ||
// @Success 204 | ||
// @Failure 404 {string} string "club not found" | ||
// @Failure 400 {string} string "invalid request body" | ||
// @Failure 400 {string} string "failed to validate id" | ||
// @Failure 500 {string} string "database error" | ||
// @Router /api/v1/clubs/:id/tags/:tagId [delete] | ||
func (l *ClubTagController) DeleteClubTag(c *fiber.Ctx) error { | ||
err := l.clubTagService.DeleteClubTag(c.Params("clubID"), c.Params("tagID")) | ||
if err != nil { | ||
return err.FiberError(c) | ||
} | ||
|
||
return c.SendStatus(fiber.StatusNoContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/GenerateNU/sac/backend/src/controllers" | ||
"github.com/GenerateNU/sac/backend/src/services" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func ClubTag(router fiber.Router, clubTagService services.ClubTagServiceInterface) { | ||
clubTagController := controllers.NewClubTagController(clubTagService) | ||
|
||
clubTags := router.Group("/:clubID/tags") | ||
|
||
clubTags.Post("/", clubTagController.CreateClubTags) | ||
clubTags.Get("/", clubTagController.GetClubTags) | ||
clubTags.Delete("/:tagID", clubTagController.DeleteClubTag) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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 ClubTagServiceInterface interface { | ||
CreateClubTags(id string, clubTagsBody models.CreateClubTagsRequestBody) ([]models.Tag, *errors.Error) | ||
GetClubTags(id string) ([]models.Tag, *errors.Error) | ||
DeleteClubTag(id string, tagId string) *errors.Error | ||
} | ||
|
||
type ClubTagService struct { | ||
DB *gorm.DB | ||
Validate *validator.Validate | ||
} | ||
|
||
func NewClubTagService(db *gorm.DB, validate *validator.Validate) ClubTagServiceInterface { | ||
return &ClubTagService{DB: db, Validate: validate} | ||
} | ||
|
||
func (c *ClubTagService) CreateClubTags(id string, clubTagsBody models.CreateClubTagsRequestBody) ([]models.Tag, *errors.Error) { | ||
idAsUUID, err := utilities.ValidateID(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := c.Validate.Struct(clubTagsBody); err != nil { | ||
return nil, &errors.FailedToValidateClubTags | ||
} | ||
|
||
tags, err := transactions.GetTagsByIDs(c.DB, clubTagsBody.Tags) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return transactions.CreateClubTags(c.DB, *idAsUUID, tags) | ||
} | ||
|
||
func (c *ClubTagService) GetClubTags(id string) ([]models.Tag, *errors.Error) { | ||
idAsUUID, err := utilities.ValidateID(id) | ||
if err != nil { | ||
return nil, &errors.FailedToValidateID | ||
} | ||
|
||
return transactions.GetClubTags(c.DB, *idAsUUID) | ||
} | ||
|
||
func (c *ClubTagService) DeleteClubTag(id string, tagId string) *errors.Error { | ||
idAsUUID, err := utilities.ValidateID(id) | ||
if err != nil { | ||
return &errors.FailedToValidateID | ||
} | ||
|
||
tagIdAsUUID, err := utilities.ValidateID(tagId) | ||
if err != nil { | ||
return &errors.FailedToValidateID | ||
} | ||
|
||
return transactions.DeleteClubTag(c.DB, *idAsUUID, *tagIdAsUUID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package transactions | ||
|
||
import ( | ||
"github.com/GenerateNU/sac/backend/src/errors" | ||
"github.com/GenerateNU/sac/backend/src/models" | ||
"github.com/google/uuid" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
// Create tags for a club | ||
func CreateClubTags(db *gorm.DB, id uuid.UUID, tags []models.Tag) ([]models.Tag, *errors.Error) { | ||
user, err := GetClub(db, id) | ||
if err != nil { | ||
return nil, &errors.UserNotFound | ||
} | ||
|
||
if err := db.Model(&user).Association("Tag").Replace(tags); err != nil { | ||
return nil, &errors.FailedToUpdateUser | ||
} | ||
|
||
return tags, nil | ||
} | ||
|
||
// Get tags for a club | ||
func GetClubTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, *errors.Error) { | ||
var tags []models.Tag | ||
|
||
club, err := GetClub(db, id) | ||
if err != nil { | ||
return nil, &errors.ClubNotFound | ||
} | ||
|
||
if err := db.Model(&club).Association("Tag").Find(&tags); err != nil { | ||
return nil, &errors.FailedToGetTag | ||
} | ||
return tags, nil | ||
} | ||
|
||
// Delete tag for a club | ||
func DeleteClubTag(db *gorm.DB, id uuid.UUID, tagId uuid.UUID) *errors.Error { | ||
club, err := GetClub(db, id) | ||
if err != nil { | ||
return &errors.ClubNotFound | ||
} | ||
|
||
tag, err := GetTag(db, tagId) | ||
if err != nil { | ||
return &errors.TagNotFound | ||
} | ||
|
||
if err := db.Model(&club).Association("Tag").Delete(&tag); err != nil { | ||
return &errors.FailedToUpdateClub | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.