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"})
}
+10
View File
@@ -0,0 +1,10 @@
package models
import "time"
type Collection struct {
ID uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"not null" json:"name"`
CreatedAt time.Time
UpdatedAt time.Time
}
+11
View File
@@ -0,0 +1,11 @@
package models
import "time"
type Colour struct {
ID uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"not null" json:"name"`
Code string `json:"code"` // contoh: HEX (#000000) atau RGB
CreatedAt time.Time
UpdatedAt time.Time
}
+29
View File
@@ -0,0 +1,29 @@
package models
import "time"
type Product struct {
ID uint `gorm:"primaryKey" json:"id"`
CategoryID uint `json:"category_id"`
Category ProductCategory `gorm:"foreignKey:CategoryID" json:"category"`
CollectionID uint `json:"collection_id"`
Collection Collection `gorm:"foreignKey:CollectionID" json:"collection"`
ColourID uint `json:"colour_id"`
Colour Colour `gorm:"foreignKey:ColourID" json:"colour"`
SizeID uint `json:"size_id"`
Size Size `gorm:"foreignKey:SizeID" json:"size"`
SKU string `gorm:"not null;unique" json:"sku"`
Name string `gorm:"not null" json:"name"`
Description string `json:"description"`
UnitOfMeasure string `json:"unit_of_measure"`
IsRawMaterial bool `json:"is_raw_material"`
IsFinishedGood bool `json:"is_finished_good"`
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -0,0 +1,10 @@
package models
import "time"
type ProductCategory struct {
ID uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"not null" json:"name"`
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -0,0 +1,18 @@
package models
import "time"
type ProductComponent struct {
ID uint `gorm:"primaryKey" json:"id"`
ProductID uint `json:"product_id"` // Produk utama
Product Product `gorm:"foreignKey:ProductID" json:"product"`
ComponenID uint `json:"componen_id"` // Komponen produk
Component Product `gorm:"foreignKey:ComponenID" json:"component"`
Quantity float64 `json:"quantity"`
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -0,0 +1,21 @@
package models
import "time"
type ProductionOrder struct {
ID uint `gorm:"primaryKey" json:"id"`
ProductID uint `json:"product_id"`
Product Product `gorm:"foreignKey:ProductID" json:"product"`
ProductionID string `gorm:"not null;unique" json:"production_id"` // kode batch
TargetQuantity float64 `json:"target_quantity"`
Status string `json:"status"` // contoh: "planned", "in_progress", "done"
StartDate time.Time `json:"start_date"`
EndDate time.Time `json:"end_date"`
Note string `json:"note"`
CreatedAt time.Time
UpdatedAt time.Time
}
+11
View File
@@ -0,0 +1,11 @@
package models
import "time"
type Size struct {
ID uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"not null" json:"name"` // contoh: S, M, L, XL
Description string `json:"description"` // opsional, seperti "Small", "Large"
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -0,0 +1,24 @@
package models
import "time"
type StockMovement struct {
ID uint `gorm:"primaryKey" json:"id"`
ProductID uint `json:"product_id"`
Product Product `gorm:"foreignKey:ProductID" json:"product"`
OriginWarehouseID uint `json:"origin_warehouse_id"`
OriginWarehouse Warehouse `gorm:"foreignKey:OriginWarehouseID" json:"origin_warehouse"`
StockType string `json:"stock_type"` // contoh: "in", "out", "transfer"
DestinationWarehouseID *uint `json:"destination_warehouse_id"`
DestinationWarehouse *Warehouse `gorm:"foreignKey:DestinationWarehouseID" json:"destination_warehouse"`
Date time.Time `json:"date"`
Note string `json:"note"`
CreatedAt time.Time
UpdatedAt time.Time
}
+15
View File
@@ -0,0 +1,15 @@
package models
import "time"
type Warehouse struct {
ID uint `gorm:"primaryKey" json:"id"`
Kode string `gorm:"not null;unique" json:"kode"`
Name string `gorm:"not null" json:"name"`
Address string `json:"address"`
Stackholder string `json:"stackholder"`
Number string `json:"number"`
Description string `json:"description"`
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -0,0 +1,33 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type CollectionRepository struct {
DB *gorm.DB
}
func NewCollectionRepository(db *gorm.DB) *CollectionRepository {
return &CollectionRepository{DB: db}
}
func (r *CollectionRepository) FindAll() ([]models.Collection, error) {
var collections []models.Collection
err := r.DB.Find(&collections).Error
return collections, err
}
func (r *CollectionRepository) Create(col *models.Collection) error {
return r.DB.Create(col).Error
}
func (r *CollectionRepository) Update(id uint, data *models.Collection) error {
return r.DB.Model(&models.Collection{}).Where("id = ?", id).Updates(data).Error
}
func (r *CollectionRepository) Delete(id uint) error {
return r.DB.Delete(&models.Collection{}, id).Error
}
@@ -0,0 +1,33 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type ColourRepository struct {
DB *gorm.DB
}
func NewColourRepository(db *gorm.DB) *ColourRepository {
return &ColourRepository{DB: db}
}
func (r *ColourRepository) FindAll() ([]models.Colour, error) {
var colours []models.Colour
err := r.DB.Find(&colours).Error
return colours, err
}
func (r *ColourRepository) Create(colour *models.Colour) error {
return r.DB.Create(colour).Error
}
func (r *ColourRepository) Update(id uint, data *models.Colour) error {
return r.DB.Model(&models.Colour{}).Where("id = ?", id).Updates(data).Error
}
func (r *ColourRepository) Delete(id uint) error {
return r.DB.Delete(&models.Colour{}, id).Error
}
@@ -0,0 +1,38 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type ProductRepository struct {
DB *gorm.DB
}
func NewProductRepository(db *gorm.DB) *ProductRepository {
return &ProductRepository{DB: db}
}
func (r *ProductRepository) FindAll() ([]models.Product, error) {
var products []models.Product
err := r.DB.
Preload("Category").
Preload("Collection").
Preload("Colour").
Preload("Size").
Find(&products).Error
return products, err
}
func (r *ProductRepository) Create(product *models.Product) error {
return r.DB.Create(product).Error
}
func (r *ProductRepository) Update(id uint, product *models.Product) error {
return r.DB.Model(&models.Product{}).Where("id = ?", id).Updates(product).Error
}
func (r *ProductRepository) Delete(id uint) error {
return r.DB.Delete(&models.Product{}, id).Error
}
@@ -0,0 +1,33 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type CategoryRepository struct {
DB *gorm.DB
}
func NewCategoryRepository(db *gorm.DB) *CategoryRepository {
return &CategoryRepository{DB: db}
}
func (r *CategoryRepository) FindAll() ([]models.ProductCategory, error) {
var categories []models.ProductCategory
err := r.DB.Find(&categories).Error
return categories, err
}
func (r *CategoryRepository) Create(cat *models.ProductCategory) error {
return r.DB.Create(cat).Error
}
func (r *CategoryRepository) Update(id uint, data *models.ProductCategory) error {
return r.DB.Model(&models.ProductCategory{}).Where("id = ?", id).Updates(data).Error
}
func (r *CategoryRepository) Delete(id uint) error {
return r.DB.Delete(&models.ProductCategory{}, id).Error
}
@@ -0,0 +1,32 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type ProductComponentRepository struct {
DB *gorm.DB
}
func NewProductComponentRepository(db *gorm.DB) *ProductComponentRepository {
return &ProductComponentRepository{DB: db}
}
func (r *ProductComponentRepository) FindAll() ([]models.ProductComponent, error) {
var data []models.ProductComponent
err := r.DB.
Preload("Product").
Preload("Component").
Find(&data).Error
return data, err
}
func (r *ProductComponentRepository) Create(pc *models.ProductComponent) error {
return r.DB.Create(pc).Error
}
func (r *ProductComponentRepository) Delete(id uint) error {
return r.DB.Delete(&models.ProductComponent{}, id).Error
}
@@ -0,0 +1,33 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type ProductionOrderRepository struct {
DB *gorm.DB
}
func NewProductionOrderRepository(db *gorm.DB) *ProductionOrderRepository {
return &ProductionOrderRepository{DB: db}
}
func (r *ProductionOrderRepository) FindAll() ([]models.ProductionOrder, error) {
var orders []models.ProductionOrder
err := r.DB.Preload("Product").Find(&orders).Error
return orders, err
}
func (r *ProductionOrderRepository) Create(order *models.ProductionOrder) error {
return r.DB.Create(order).Error
}
func (r *ProductionOrderRepository) Update(id uint, data *models.ProductionOrder) error {
return r.DB.Model(&models.ProductionOrder{}).Where("id = ?", id).Updates(data).Error
}
func (r *ProductionOrderRepository) Delete(id uint) error {
return r.DB.Delete(&models.ProductionOrder{}, id).Error
}
+33
View File
@@ -0,0 +1,33 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type SizeRepository struct {
DB *gorm.DB
}
func NewSizeRepository(db *gorm.DB) *SizeRepository {
return &SizeRepository{DB: db}
}
func (r *SizeRepository) FindAll() ([]models.Size, error) {
var sizes []models.Size
err := r.DB.Find(&sizes).Error
return sizes, err
}
func (r *SizeRepository) Create(size *models.Size) error {
return r.DB.Create(size).Error
}
func (r *SizeRepository) Update(id uint, data *models.Size) error {
return r.DB.Model(&models.Size{}).Where("id = ?", id).Updates(data).Error
}
func (r *SizeRepository) Delete(id uint) error {
return r.DB.Delete(&models.Size{}, id).Error
}
@@ -0,0 +1,29 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type StockMovementRepository struct {
DB *gorm.DB
}
func NewStockMovementRepository(db *gorm.DB) *StockMovementRepository {
return &StockMovementRepository{DB: db}
}
func (r *StockMovementRepository) FindAll() ([]models.StockMovement, error) {
var movements []models.StockMovement
err := r.DB.
Preload("Product").
Preload("OriginWarehouse").
Preload("DestinationWarehouse").
Find(&movements).Error
return movements, err
}
func (r *StockMovementRepository) Create(movement *models.StockMovement) error {
return r.DB.Create(movement).Error
}
@@ -0,0 +1,33 @@
package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type WarehouseRepository struct {
DB *gorm.DB
}
func NewWarehouseRepository(db *gorm.DB) *WarehouseRepository {
return &WarehouseRepository{DB: db}
}
func (r *WarehouseRepository) FindAll() ([]models.Warehouse, error) {
var warehouses []models.Warehouse
err := r.DB.Find(&warehouses).Error
return warehouses, err
}
func (r *WarehouseRepository) Create(wh *models.Warehouse) error {
return r.DB.Create(wh).Error
}
func (r *WarehouseRepository) Update(id uint, wh *models.Warehouse) error {
return r.DB.Model(&models.Warehouse{}).Where("id = ?", id).Updates(wh).Error
}
func (r *WarehouseRepository) Delete(id uint) error {
return r.DB.Delete(&models.Warehouse{}, id).Error
}
+60
View File
@@ -0,0 +1,60 @@
package inventory
import (
"BE-MiniERP/modules/inventory/handler"
"github.com/gofiber/fiber/v2"
)
func RegisterRoutes(r fiber.Router) {
category := handler.NewCategoryHandler()
r.Get("/categories", category.GetAll)
r.Post("/categories", category.Create)
r.Put("/categories/:id", category.Update)
r.Delete("/categories/:id", category.Delete)
collection := handler.NewCollectionHandler()
r.Get("/collections", collection.GetAll)
r.Post("/collections", collection.Create)
r.Put("/collections/:id", collection.Update)
r.Delete("/collections/:id", collection.Delete)
colour := handler.NewColourHandler()
r.Get("/colours", colour.GetAll)
r.Post("/colours", colour.Create)
r.Put("/colours/:id", colour.Update)
r.Delete("/colours/:id", colour.Delete)
size := handler.NewSizeHandler()
r.Get("/sizes", size.GetAll)
r.Post("/sizes", size.Create)
r.Put("/sizes/:id", size.Update)
r.Delete("/sizes/:id", size.Delete)
product := handler.NewProductHandler()
r.Get("/products", product.GetAll)
r.Post("/products", product.Create)
r.Put("/products/:id", product.Update)
r.Delete("/products/:id", product.Delete)
warehouse := handler.NewWarehouseHandler()
r.Get("/warehouses", warehouse.GetAll)
r.Post("/warehouses", warehouse.Create)
r.Put("/warehouses/:id", warehouse.Update)
r.Delete("/warehouses/:id", warehouse.Delete)
stock := handler.NewStockMovementHandler()
r.Get("/stock-movements", stock.GetAll)
r.Post("/stock-movements", stock.Create)
component := handler.NewProductComponentHandler()
r.Get("/product-components", component.GetAll)
r.Post("/product-components", component.Create)
r.Delete("/product-components/:id", component.Delete)
po := handler.NewProductionOrderHandler()
r.Get("/production-orders", po.GetAll)
r.Post("/production-orders", po.Create)
r.Put("/production-orders/:id", po.Update)
r.Delete("/production-orders/:id", po.Delete)
}