func (h *ProductHandler) UpdateProduct(c echo.Context) error {
productID := c.Param("id")
var req productEntity.ProductRequest
err := c.Bind(&req)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Failed to bind data",
"error": err,
})
}
var product productEntity.Product
product, err = h.productUC.GetProductByID(productID, &product)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{
"message": "Failed to get product",
"error": err,
})
}
err = h.productUC.UpdateProduct(productID, &req)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Failed to update data",
"error": err,
})
}
status := "tersedia"
if req.Stock == 0 {
status = "habis"
}
err = h.productUC.UpdateProductStatus(fmt.Sprint(product.Product_status_id), status)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{
"message": "Failed to update product status",
"error": err,
})
}
err = h.productUC.UpdateProductDescription(fmt.Sprint(product.Product_status_id), req.Description)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{
"message": "Failed to update product description",
"error": err,
})
}
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "Successfully update product",
})
}