Update get User & Refresh Token
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"BE-MiniERP/modules/inventory/repository"
|
||||
"BE-MiniERP/modules/inventory/service"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@@ -39,27 +40,61 @@ func (h *ProductHandler) GetAll(c *fiber.Ctx) error {
|
||||
|
||||
func (h *ProductHandler) Create(c *fiber.Ctx) error {
|
||||
var input models.Product
|
||||
|
||||
if err := c.BodyParser(&input); err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Invalid input format"})
|
||||
}
|
||||
|
||||
if input.Name == "" {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Product name is required"})
|
||||
}
|
||||
if input.Price <= 0 {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Price must be greater than 0"})
|
||||
}
|
||||
if input.Hpp <= 0 {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "HPP must be greater than 0"})
|
||||
}
|
||||
if input.CategoryID == 0 {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Category ID is required"})
|
||||
}
|
||||
if input.CollectionID == 0 {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Collection ID is required"})
|
||||
}
|
||||
if input.ColourID == 0 {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Colour ID is required"})
|
||||
}
|
||||
if input.SizeID == 0 {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Size ID is required"})
|
||||
}
|
||||
if input.UnitOfMeasure == "" {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Unit of measure is required"})
|
||||
}
|
||||
|
||||
// Jika hanya terima ID, lakukan preload relasi
|
||||
if err := h.Repo.PreloadRelations(&input); err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Failed to preload relations"})
|
||||
return c.Status(400).JSON(fiber.Map{
|
||||
"error": "Failed to preload relations: " + err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
// Generate SKU
|
||||
sku, err := service.GenerateSKU(&input)
|
||||
if err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "Failed to generate SKU: " + err.Error()})
|
||||
return c.Status(400).JSON(fiber.Map{
|
||||
"error": "Failed to generate SKU: " + err.Error(),
|
||||
})
|
||||
}
|
||||
input.SKU = sku
|
||||
|
||||
if err := h.Repo.Create(&input); err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Failed to create product"})
|
||||
if strings.Contains(err.Error(), "duplicate key value") && strings.Contains(err.Error(), "sku") {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "SKU already exists"})
|
||||
}
|
||||
|
||||
return c.Status(500).JSON(fiber.Map{
|
||||
"error": "Failed to create product: " + err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(input)
|
||||
return c.Status(201).JSON(input)
|
||||
}
|
||||
|
||||
func (h *ProductHandler) Update(c *fiber.Ctx) error {
|
||||
|
||||
@@ -28,7 +28,7 @@ func (h *ProductionOrderHandler) GetAll(c *fiber.Ctx) error {
|
||||
productID := c.QueryInt("product_id", 0)
|
||||
startDateStr := c.Query("start_date", "")
|
||||
endDateStr := c.Query("end_date", "")
|
||||
warehouseID := c.QueryInt("origin_id", 0)
|
||||
warehouseID := c.QueryInt("warehouse_id", 0)
|
||||
|
||||
var startDate, endDate time.Time
|
||||
var err error
|
||||
|
||||
Reference in New Issue
Block a user