Update API

This commit is contained in:
2025-06-22 23:23:26 +07:00
parent b077ef9f06
commit 40705fbf3b
63 changed files with 1976 additions and 81 deletions
@@ -0,0 +1,59 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type CollectionHandler struct {
Repo *repository.CollectionRepository
}
func NewCollectionHandler() *CollectionHandler {
return &CollectionHandler{
Repo: repository.NewCollectionRepository(database.DB),
}
}
func (h *CollectionHandler) GetAll(c *fiber.Ctx) error {
collections, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch collections"})
}
return c.JSON(collections)
}
func (h *CollectionHandler) Create(c *fiber.Ctx) error {
var input models.Collection
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create collection"})
}
return c.JSON(input)
}
func (h *CollectionHandler) Update(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
var input models.Collection
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Update(uint(id), &input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to update collection"})
}
return c.JSON(fiber.Map{"message": "Collection updated"})
}
func (h *CollectionHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete collection"})
}
return c.JSON(fiber.Map{"message": "Collection deleted"})
}
@@ -0,0 +1,59 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type ColourHandler struct {
Repo *repository.ColourRepository
}
func NewColourHandler() *ColourHandler {
return &ColourHandler{
Repo: repository.NewColourRepository(database.DB),
}
}
func (h *ColourHandler) GetAll(c *fiber.Ctx) error {
colours, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch colours"})
}
return c.JSON(colours)
}
func (h *ColourHandler) Create(c *fiber.Ctx) error {
var input models.Colour
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create colour"})
}
return c.JSON(input)
}
func (h *ColourHandler) Update(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
var input models.Colour
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Update(uint(id), &input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to update colour"})
}
return c.JSON(fiber.Map{"message": "Colour updated"})
}
func (h *ColourHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete colour"})
}
return c.JSON(fiber.Map{"message": "Colour deleted"})
}
@@ -0,0 +1,59 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type CategoryHandler struct {
Repo *repository.CategoryRepository
}
func NewCategoryHandler() *CategoryHandler {
return &CategoryHandler{
Repo: repository.NewCategoryRepository(database.DB),
}
}
func (h *CategoryHandler) GetAll(c *fiber.Ctx) error {
categories, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch categories"})
}
return c.JSON(categories)
}
func (h *CategoryHandler) Create(c *fiber.Ctx) error {
var input models.ProductCategory
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create category"})
}
return c.JSON(input)
}
func (h *CategoryHandler) Update(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
var input models.ProductCategory
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Update(uint(id), &input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to update category"})
}
return c.JSON(fiber.Map{"message": "Category updated"})
}
func (h *CategoryHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete category"})
}
return c.JSON(fiber.Map{"message": "Category deleted"})
}
@@ -0,0 +1,47 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type ProductComponentHandler struct {
Repo *repository.ProductComponentRepository
}
func NewProductComponentHandler() *ProductComponentHandler {
return &ProductComponentHandler{
Repo: repository.NewProductComponentRepository(database.DB),
}
}
func (h *ProductComponentHandler) GetAll(c *fiber.Ctx) error {
result, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch components"})
}
return c.JSON(result)
}
func (h *ProductComponentHandler) Create(c *fiber.Ctx) error {
var input models.ProductComponent
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create component entry"})
}
return c.JSON(input)
}
func (h *ProductComponentHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete component entry"})
}
return c.JSON(fiber.Map{"message": "Deleted successfully"})
}
@@ -0,0 +1,59 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type ProductHandler struct {
Repo *repository.ProductRepository
}
func NewProductHandler() *ProductHandler {
return &ProductHandler{
Repo: repository.NewProductRepository(database.DB),
}
}
func (h *ProductHandler) GetAll(c *fiber.Ctx) error {
products, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch products"})
}
return c.JSON(products)
}
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"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create product"})
}
return c.JSON(input)
}
func (h *ProductHandler) Update(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
var input models.Product
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Update(uint(id), &input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to update product"})
}
return c.JSON(fiber.Map{"message": "Product updated"})
}
func (h *ProductHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete product"})
}
return c.JSON(fiber.Map{"message": "Product deleted"})
}
@@ -0,0 +1,59 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type ProductionOrderHandler struct {
Repo *repository.ProductionOrderRepository
}
func NewProductionOrderHandler() *ProductionOrderHandler {
return &ProductionOrderHandler{
Repo: repository.NewProductionOrderRepository(database.DB),
}
}
func (h *ProductionOrderHandler) GetAll(c *fiber.Ctx) error {
data, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch production orders"})
}
return c.JSON(data)
}
func (h *ProductionOrderHandler) Create(c *fiber.Ctx) error {
var input models.ProductionOrder
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create production order"})
}
return c.JSON(input)
}
func (h *ProductionOrderHandler) Update(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
var input models.ProductionOrder
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Update(uint(id), &input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to update production order"})
}
return c.JSON(fiber.Map{"message": "Updated"})
}
func (h *ProductionOrderHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete production order"})
}
return c.JSON(fiber.Map{"message": "Deleted"})
}
+59
View File
@@ -0,0 +1,59 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type SizeHandler struct {
Repo *repository.SizeRepository
}
func NewSizeHandler() *SizeHandler {
return &SizeHandler{
Repo: repository.NewSizeRepository(database.DB),
}
}
func (h *SizeHandler) GetAll(c *fiber.Ctx) error {
sizes, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch sizes"})
}
return c.JSON(sizes)
}
func (h *SizeHandler) Create(c *fiber.Ctx) error {
var input models.Size
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create size"})
}
return c.JSON(input)
}
func (h *SizeHandler) Update(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
var input models.Size
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Update(uint(id), &input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to update size"})
}
return c.JSON(fiber.Map{"message": "Size updated"})
}
func (h *SizeHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete size"})
}
return c.JSON(fiber.Map{"message": "Size deleted"})
}
@@ -0,0 +1,40 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"github.com/gofiber/fiber/v2"
)
type StockMovementHandler struct {
Repo *repository.StockMovementRepository
}
func NewStockMovementHandler() *StockMovementHandler {
return &StockMovementHandler{
Repo: repository.NewStockMovementRepository(database.DB),
}
}
func (h *StockMovementHandler) GetAll(c *fiber.Ctx) error {
data, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch stock movements"})
}
return c.JSON(data)
}
func (h *StockMovementHandler) Create(c *fiber.Ctx) error {
var input models.StockMovement
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create stock movement"})
}
return c.JSON(input)
}
@@ -0,0 +1,59 @@
package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type WarehouseHandler struct {
Repo *repository.WarehouseRepository
}
func NewWarehouseHandler() *WarehouseHandler {
return &WarehouseHandler{
Repo: repository.NewWarehouseRepository(database.DB),
}
}
func (h *WarehouseHandler) GetAll(c *fiber.Ctx) error {
warehouses, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch warehouses"})
}
return c.JSON(warehouses)
}
func (h *WarehouseHandler) Create(c *fiber.Ctx) error {
var input models.Warehouse
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create warehouse"})
}
return c.JSON(input)
}
func (h *WarehouseHandler) Update(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
var input models.Warehouse
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Update(uint(id), &input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to update warehouse"})
}
return c.JSON(fiber.Map{"message": "Warehouse updated"})
}
func (h *WarehouseHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete warehouse"})
}
return c.JSON(fiber.Map{"message": "Warehouse deleted"})
}