Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Jan 20, 2024
1 parent fa44d3b commit ed18a94
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
22 changes: 22 additions & 0 deletions backend/src/controllers/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,25 @@ func (t *TagController) GetTag(c *fiber.Ctx) error {

return c.Status(fiber.StatusOK).JSON(&tag)
}

// DeleteTag godoc
//
// @Summary Deletes a tag
// @Description Deletes a tag
// @ID delete-tag
// @Tags tag
// @Param id path int true "Tag ID"
// @Success 204 {string} string "No Content"
// @Failure 400 {string} string "failed to validate id"
// @Failure 404 {string} string "failed to find tag"
// @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("id"))

if err != nil {
return err
}

return c.SendStatus(fiber.StatusNoContent)
}
1 change: 1 addition & 0 deletions backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ func tagRoutes(router fiber.Router, tagService services.TagServiceInterface) {

tags.Post("/", tagController.CreateTag)
tags.Get("/:id", tagController.GetTag)
tags.Delete("/:id", tagController.DeleteTag)
}
11 changes: 11 additions & 0 deletions backend/src/services/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type TagServiceInterface interface {
CreateTag(partialTag models.CreateTagRequestBody) (*models.Tag, error)
GetTag(id string) (*models.Tag, error)
DeleteTag(id string) error
}

type TagService struct {
Expand Down Expand Up @@ -39,3 +40,13 @@ func (t *TagService) GetTag(id string) (*models.Tag, error) {

return transactions.GetTag(t.DB, *idAsUint)
}

func (t *TagService) DeleteTag(id string) error {
idAsUint, err := utilities.ValidateID(id)

if err != nil {
return err
}

return transactions.DeleteTag(t.DB, *idAsUint)
}
12 changes: 12 additions & 0 deletions backend/src/transactions/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ func GetTag(db *gorm.DB, id uint) (*models.Tag, error) {

return &tag, nil
}

func DeleteTag(db *gorm.DB, id uint) error {
if result := db.Delete(&models.Tag{}, id); result.RowsAffected == 0 {
if result.Error != nil {
return fiber.NewError(fiber.StatusInternalServerError, "failed to delete tag")
} else {
return fiber.NewError(fiber.StatusNotFound, "failed to find tag")
}
}

return nil
}
2 changes: 1 addition & 1 deletion backend/tests/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func generateRandomDBName() string {
result[i] = letterBytes[generateRandomInt(int64(len(letterBytes)))]
}

return string(result)
return fmt.Sprintf("%s%s", prefix, string(result))
}

func configureDatabase(config config.Settings) (*gorm.DB, error) {
Expand Down
58 changes: 55 additions & 3 deletions backend/tests/api/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCreateTagWorks(t *testing.T) {
appAssert.App.DropDB()
}

var AssertNoTagCreation = func(app TestApp, assert *assert.A, resp *http.Response) {
var AssertNoTags = func(app TestApp, assert *assert.A, resp *http.Response) {
var tags []models.Tag

err := app.Conn.Find(&tags).Error
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestCreateTagFailsBadRequest(t *testing.T) {
Status: 400,
Message: "failed to process the request",
},
DBTester: AssertNoTagCreation,
DBTester: AssertNoTags,
},
)
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestCreateTagFailsValidation(t *testing.T) {
Status: 400,
Message: "failed to validate the data",
},
DBTester: AssertNoTagCreation,
DBTester: AssertNoTags,
},
)
}
Expand Down Expand Up @@ -137,6 +137,8 @@ func TestGetTagWorks(t *testing.T) {
},
},
)

existingAppAssert.App.DropDB()
}

func TestGetTagFailsBadRequest(t *testing.T) {
Expand Down Expand Up @@ -172,3 +174,53 @@ func TestGetTagFailsNotFound(t *testing.T) {
},
)
}

func TestDeleteTagWorks(t *testing.T) {
existingAppAssert := CreateSampleTag(t, "Generate", "Science")

TestRequest{
Method: "DELETE",
Path: "/api/v1/tags/1",
}.TestOnStatusAndDB(t, &existingAppAssert,
DBTesterWithStatus{
Status: 204,
DBTester: AssertNoTags,
},
)

existingAppAssert.App.DropDB()
}

func TestDeleteTagFailsBadRequest(t *testing.T) {
badRequests := []string{
"0",
"-1",
"1.1",
"foo",
"null",
}

for _, badRequest := range badRequests {
TestRequest{
Method: "DELETE",
Path: fmt.Sprintf("/api/v1/tags/%s", badRequest),
}.TestOnStatusAndMessage(t, nil,
MessageWithStatus{
Status: 400,
Message: "failed to validate id",
},
)
}
}

func TestDeleteTagFailsNotFound(t *testing.T) {
TestRequest{
Method: "DELETE",
Path: "/api/v1/tags/1",
}.TestOnStatusAndMessage(t, nil,
MessageWithStatus{
Status: 404,
Message: "failed to find tag",
},
)
}

0 comments on commit ed18a94

Please sign in to comment.