Skip to content

Commit

Permalink
addressed review
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Jan 21, 2024
1 parent 1c282bd commit bff3a71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
5 changes: 5 additions & 0 deletions backend/src/controllers/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func (t *TagController) GetTag(c *fiber.Ctx) error {
// @Param id path int true "Tag ID"
// @Success 204
// @Failure 400 {string} string "failed to process the request"
// @Failure 400 {string} string "failed to validate id"
// @Failure 400 {string} string "failed to validate the data"
// @Failure 404 {string} string "failed to find tag"
// @Failure 500 {string} string "failed to update tag"
// @Router /api/v1/tags/{id} [patch]
func (t *TagController) UpdateTag(c *fiber.Ctx) error {
var tagBody models.UpdateTagRequestBody

Expand Down
16 changes: 8 additions & 8 deletions backend/src/services/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
)

type TagServiceInterface interface {
CreateTag(partialTag models.CreateTagRequestBody) (*models.Tag, error)
CreateTag(tagBody models.CreateTagRequestBody) (*models.Tag, error)
GetTag(id string) (*models.Tag, error)
UpdateTag(id string, partialTag models.UpdateTagRequestBody) error
UpdateTag(id string, tagBody models.UpdateTagRequestBody) error
DeleteTag(id string) error
}

type TagService struct {
DB *gorm.DB
}

func (t *TagService) CreateTag(partialTag models.CreateTagRequestBody) (*models.Tag, error) {
func (t *TagService) CreateTag(tagBody models.CreateTagRequestBody) (*models.Tag, error) {
tag := models.Tag{
Name: partialTag.Name,
CategoryID: partialTag.CategoryID,
Name: tagBody.Name,
CategoryID: tagBody.CategoryID,
}

if err := utilities.ValidateData(tag); err != nil {
Expand All @@ -42,16 +42,16 @@ func (t *TagService) GetTag(id string) (*models.Tag, error) {
return transactions.GetTag(t.DB, *idAsUint)
}

func (t *TagService) UpdateTag(id string, partialTag models.UpdateTagRequestBody) error {
func (t *TagService) UpdateTag(id string, tagBody models.UpdateTagRequestBody) error {
idAsUint, err := utilities.ValidateID(id)

if err != nil {
return err
}

tag := models.Tag{
Name: partialTag.Name,
CategoryID: partialTag.CategoryID,
Name: tagBody.Name,
CategoryID: tagBody.CategoryID,
}

if err := utilities.ValidateData(tag); err != nil {
Expand Down
1 change: 1 addition & 0 deletions backend/src/utilities/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func ValidateData(model interface{}) error {
return nil
}

// Validates that an id follows postgres uint format, returns a uint otherwise returns an error
func ValidateID(id string) (*uint, error) {
idAsInt, err := strconv.Atoi(id)

Expand Down

0 comments on commit bff3a71

Please sign in to comment.