feat: add update method for both trash
This commit is contained in:
parent
22b61ce44e
commit
9a99f4f03b
|
@ -55,3 +55,22 @@ func NewTrashDetailResponse(id, description string, price int, createdAt, update
|
|||
UpdatedAt: updatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
type UpdateTrashCategoryDTO struct {
|
||||
Name string `json:"name" validate:"required,min=3,max=100"`
|
||||
}
|
||||
|
||||
func (t *UpdateTrashCategoryDTO) Validate() error {
|
||||
validate := validator.New()
|
||||
return validate.Struct(t)
|
||||
}
|
||||
|
||||
type UpdateTrashDetailDTO struct {
|
||||
Description string `json:"description" validate:"required,min=3,max=255"`
|
||||
Price int `json:"price" validate:"required,min=0"`
|
||||
}
|
||||
|
||||
func (t *UpdateTrashDetailDTO) Validate() error {
|
||||
validate := validator.New()
|
||||
return validate.Struct(t)
|
||||
}
|
|
@ -44,4 +44,6 @@ func AppRouter(app *fiber.App) {
|
|||
app.Get("/trash-categorydetail/:id", controllers.GetTrashCategoryDetail)
|
||||
app.Post("/addtrash-category", controllers.CreateTrashCategory)
|
||||
app.Post("/addtrash-categorydetail", controllers.CreateTrashDetail)
|
||||
app.Put("/updatetrash-category/:id", controllers.UpdateTrashCategory)
|
||||
app.Put("/updatetrash-detail/:id", controllers.UpdateTrashDetail)
|
||||
}
|
||||
|
|
|
@ -164,3 +164,90 @@ func CreateTrashDetail(c *fiber.Ctx) error {
|
|||
detailResponse,
|
||||
))
|
||||
}
|
||||
|
||||
func UpdateTrashCategory(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
|
||||
var categoryInput dto.UpdateTrashCategoryDTO
|
||||
if err := c.BodyParser(&categoryInput); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
"Invalid input data",
|
||||
nil,
|
||||
))
|
||||
}
|
||||
|
||||
if err := categoryInput.Validate(); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
"Validation failed: "+err.Error(),
|
||||
nil,
|
||||
))
|
||||
}
|
||||
|
||||
updatedCategory, err := services.UpdateTrashCategory(id, categoryInput.Name)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||
fiber.StatusInternalServerError,
|
||||
"Failed to update trash category",
|
||||
nil,
|
||||
))
|
||||
}
|
||||
|
||||
response := dto.NewTrashCategoryResponse(
|
||||
updatedCategory.ID,
|
||||
updatedCategory.Name,
|
||||
utils.FormatDateToIndonesianFormat(updatedCategory.CreatedAt),
|
||||
utils.FormatDateToIndonesianFormat(updatedCategory.UpdatedAt),
|
||||
)
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||
fiber.StatusOK,
|
||||
"Trash category updated successfully",
|
||||
response,
|
||||
))
|
||||
}
|
||||
|
||||
func UpdateTrashDetail(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
|
||||
var detailInput dto.UpdateTrashDetailDTO
|
||||
if err := c.BodyParser(&detailInput); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
"Invalid input data",
|
||||
nil,
|
||||
))
|
||||
}
|
||||
|
||||
if err := detailInput.Validate(); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
"Validation failed: "+err.Error(),
|
||||
nil,
|
||||
))
|
||||
}
|
||||
|
||||
updatedDetail, err := services.UpdateTrashDetail(id, detailInput.Description, detailInput.Price)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||
fiber.StatusInternalServerError,
|
||||
"Failed to update trash detail",
|
||||
nil,
|
||||
))
|
||||
}
|
||||
|
||||
response := dto.NewTrashDetailResponse(
|
||||
updatedDetail.ID,
|
||||
updatedDetail.Description,
|
||||
updatedDetail.Price,
|
||||
utils.FormatDateToIndonesianFormat(updatedDetail.CreatedAt),
|
||||
utils.FormatDateToIndonesianFormat(updatedDetail.UpdatedAt),
|
||||
)
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||
fiber.StatusOK,
|
||||
"Trash detail updated successfully",
|
||||
response,
|
||||
))
|
||||
}
|
|
@ -7,7 +7,6 @@ import (
|
|||
|
||||
func GetTrashCategories() ([]domain.TrashCategory, error) {
|
||||
var categories []domain.TrashCategory
|
||||
|
||||
if err := config.DB.Find(&categories).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -22,6 +21,15 @@ func GetTrashCategoryDetail(id string) (domain.TrashCategory, error) {
|
|||
return category, nil
|
||||
}
|
||||
|
||||
func GetTrashDetailByID(id string) (domain.TrashDetail, error) {
|
||||
var detail domain.TrashDetail
|
||||
|
||||
if err := config.DB.Where("id = ?", id).First(&detail).Error; err != nil {
|
||||
return detail, err
|
||||
}
|
||||
return detail, nil
|
||||
}
|
||||
|
||||
func CreateTrashCategory(category *domain.TrashCategory) error {
|
||||
if err := config.DB.Create(category).Error; err != nil {
|
||||
return err
|
||||
|
@ -35,3 +43,17 @@ func CreateTrashDetail(detail *domain.TrashDetail) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateTrashCategory(category *domain.TrashCategory) error {
|
||||
if err := config.DB.Save(category).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateTrashDetail(detail *domain.TrashDetail) error {
|
||||
if err := config.DB.Save(detail).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
)
|
||||
|
||||
func GetTrashCategories() ([]domain.TrashCategory, error) {
|
||||
|
||||
return repositories.GetTrashCategories()
|
||||
}
|
||||
|
||||
|
@ -16,12 +15,10 @@ func GetTrashCategoryDetail(id string) (domain.TrashCategory, error) {
|
|||
|
||||
func CreateTrashCategory(name string) (domain.TrashCategory, error) {
|
||||
category := domain.TrashCategory{Name: name}
|
||||
|
||||
err := repositories.CreateTrashCategory(&category)
|
||||
if err != nil {
|
||||
return category, err
|
||||
}
|
||||
|
||||
return category, nil
|
||||
}
|
||||
|
||||
|
@ -31,11 +28,40 @@ func CreateTrashDetail(categoryID, description string, price int) (domain.TrashD
|
|||
Description: description,
|
||||
Price: price,
|
||||
}
|
||||
|
||||
err := repositories.CreateTrashDetail(&detail)
|
||||
if err != nil {
|
||||
return detail, err
|
||||
}
|
||||
return detail, nil
|
||||
}
|
||||
|
||||
func UpdateTrashCategory(id, name string) (domain.TrashCategory, error) {
|
||||
category, err := repositories.GetTrashCategoryDetail(id)
|
||||
if err != nil {
|
||||
return domain.TrashCategory{}, err
|
||||
}
|
||||
category.Name = name
|
||||
if err := repositories.UpdateTrashCategory(&category); err != nil {
|
||||
return domain.TrashCategory{}, err
|
||||
}
|
||||
return category, nil
|
||||
}
|
||||
|
||||
func UpdateTrashDetail(id, description string, price int) (domain.TrashDetail, error) {
|
||||
|
||||
detail, err := repositories.GetTrashDetailByID(id)
|
||||
if err != nil {
|
||||
|
||||
return domain.TrashDetail{}, err
|
||||
}
|
||||
|
||||
detail.Description = description
|
||||
detail.Price = price
|
||||
|
||||
if err := repositories.UpdateTrashDetail(&detail); err != nil {
|
||||
|
||||
return domain.TrashDetail{}, err
|
||||
}
|
||||
|
||||
return detail, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue