-
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.
- Loading branch information
1 parent
91dea77
commit cde14a8
Showing
9 changed files
with
296 additions
and
56 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,45 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/GenerateNU/sac/backend/src/models" | ||
"github.com/GenerateNU/sac/backend/src/services" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
type TagController struct { | ||
tagService services.TagServiceInterface | ||
} | ||
|
||
func NewTagController(tagService services.TagServiceInterface) *TagController { | ||
return &TagController{tagService: tagService} | ||
} | ||
|
||
// CreateTag godoc | ||
// | ||
// @Summary Creates a tag | ||
// @Description Creates a tag | ||
// @ID create-tag | ||
// @Tags tag | ||
// @Accept json | ||
// @Produce json | ||
// @Success 201 {object} models.Tag | ||
// @Failure 400 {string} string "failed to process the request" | ||
// @Failure 400 {string} string "failed to validate the data" | ||
// @Failure 500 {string} string "failed to create tag" | ||
// @Router /api/v1/tags/ [post] | ||
func (t *TagController) CreateTag(c *fiber.Ctx) error { | ||
var tagBody models.CreateTagRequestBody | ||
|
||
if err := c.BodyParser(&tagBody); err != nil { | ||
return fiber.NewError(fiber.StatusBadRequest, "failed to process the request") | ||
} | ||
|
||
dbTag, err := t.tagService.CreateTag(tagBody) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.Status(fiber.StatusCreated).JSON(&dbTag) | ||
} |
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
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,30 @@ | ||
package services | ||
|
||
import ( | ||
"github.com/GenerateNU/sac/backend/src/models" | ||
"github.com/GenerateNU/sac/backend/src/transactions" | ||
"github.com/GenerateNU/sac/backend/src/utilities" | ||
"github.com/gofiber/fiber/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type TagServiceInterface interface { | ||
CreateTag(partialTag models.CreateTagRequestBody) (*models.Tag, error) | ||
} | ||
|
||
type TagService struct { | ||
DB *gorm.DB | ||
} | ||
|
||
func (t *TagService) CreateTag(partialTag models.CreateTagRequestBody) (*models.Tag, error) { | ||
tag := models.Tag{ | ||
Name: partialTag.Name, | ||
CategoryID: partialTag.CategoryID, | ||
} | ||
|
||
if err := utilities.ValidateData(tag); err != nil { | ||
return nil, fiber.NewError(fiber.StatusBadRequest, "failed to validate the data") | ||
} | ||
|
||
return transactions.CreateTag(t.DB, tag) | ||
} |
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,31 @@ | ||
package transactions | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/GenerateNU/sac/backend/src/models" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func CreateTag(db *gorm.DB, tag models.Tag) (*models.Tag, error) { | ||
if err := db.Create(&tag).Error; err != nil { | ||
return nil, fiber.NewError(fiber.StatusInternalServerError, "failed to create tag") | ||
} | ||
return &tag, nil | ||
} | ||
|
||
func GetTag(db *gorm.DB, id uint) (*models.Tag, error) { | ||
var tag models.Tag | ||
|
||
if err := db.First(&tag, id).Error; err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, fiber.NewError(fiber.StatusBadRequest, "invalid tag id") | ||
} else { | ||
return nil, fiber.NewError(fiber.StatusInternalServerError, "unable to retrieve tag") | ||
} | ||
} | ||
|
||
return &tag, 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
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.