From bff3a71bdb83cd0fcb9be11aa7c5b815e9b2ee4f Mon Sep 17 00:00:00 2001 From: garrettladley Date: Sat, 20 Jan 2024 23:20:24 -0500 Subject: [PATCH] addressed review --- backend/src/controllers/tag.go | 5 +++++ backend/src/services/tag.go | 16 ++++++++-------- backend/src/utilities/validator.go | 1 + 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/backend/src/controllers/tag.go b/backend/src/controllers/tag.go index 05c2540b2..53b7a799d 100644 --- a/backend/src/controllers/tag.go +++ b/backend/src/controllers/tag.go @@ -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 diff --git a/backend/src/services/tag.go b/backend/src/services/tag.go index 915ded92a..fb693f499 100644 --- a/backend/src/services/tag.go +++ b/backend/src/services/tag.go @@ -9,9 +9,9 @@ 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 } @@ -19,10 +19,10 @@ 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 { @@ -42,7 +42,7 @@ 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 { @@ -50,8 +50,8 @@ func (t *TagService) UpdateTag(id string, partialTag models.UpdateTagRequestBody } tag := models.Tag{ - Name: partialTag.Name, - CategoryID: partialTag.CategoryID, + Name: tagBody.Name, + CategoryID: tagBody.CategoryID, } if err := utilities.ValidateData(tag); err != nil { diff --git a/backend/src/utilities/validator.go b/backend/src/utilities/validator.go index a161587c6..07ef87d87 100644 --- a/backend/src/utilities/validator.go +++ b/backend/src/utilities/validator.go @@ -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)