feat(product): Enhance product entities and DTOs with client and category relationships

This commit is contained in:
Habib Fatkhul Rohman 2025-10-31 10:48:04 +07:00
parent 26d8f997ed
commit 14cbd567b3
9 changed files with 134 additions and 70 deletions

3
.gitignore vendored
View File

@ -5,4 +5,5 @@ volumes/
.idea/
*.log
/WMS Wareify
/WMS Wareify
modules/menu_client

View File

@ -13,7 +13,8 @@ type MCategoryEntity struct {
ClientID uuid.UUID `gorm:"type:uuid;index;" json:"client_id"`
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
Products []MProductEntity `gorm:"foreignKey:CategoryID;references:ID"`
FullAuditTrail
}

View File

@ -40,16 +40,16 @@ type MProductEntity struct {
LeadTimeUomID uuid.UUID `gorm:"type:uuid;index" json:"lead_time_uom_id"`
UomToUomID uuid.UUID `gorm:"type:uuid;index" json:"uom_to_uom_id"`
Client M_Client `gorm:"foreignKey:ClientID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
// Category M_Category `gorm:"foreignKey:CategoryID;references:ID"`
// Uom M_Uom `gorm:"foreignKey:UomID;references:ID"`
// DimUom M_Uom `gorm:"foreignKey:DimUomID;references:ID"`
// WeightUom M_Uom `gorm:"foreignKey:WeightUomID;references:ID"`
// VolumeUom M_Uom `gorm:"foreignKey:VolumeUomID;references:ID"`
// MinStockUom M_Uom `gorm:"foreignKey:MinStockUomID;references:ID"`
// MaxStockUom M_Uom `gorm:"foreignKey:MaxStockUomID;references:ID"`
// LeadTimeUom M_Uom `gorm:"foreignKey:LeadTimeUomID;references:ID"`
// UomToUom M_Uom `gorm:"foreignKey:UomToUomID;references:ID"`
Client M_Client `gorm:"foreignKey:ClientID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
Category MCategoryEntity `gorm:"foreignKey:CategoryID;references:ID"`
Uom MUomEntity `gorm:"foreignKey:UomID;references:ID"`
DimUom MUomEntity `gorm:"foreignKey:DimUomID;references:ID"`
WeightUom MUomEntity `gorm:"foreignKey:WeightUomID;references:ID"`
VolumeUom MUomEntity `gorm:"foreignKey:VolumeUomID;references:ID"`
MinStockUom MUomEntity `gorm:"foreignKey:MinStockUomID;references:ID"`
MaxStockUom MUomEntity `gorm:"foreignKey:MaxStockUomID;references:ID"`
LeadTimeUom MUomEntity `gorm:"foreignKey:LeadTimeUomID;references:ID"`
UomToUom MUomEntity `gorm:"foreignKey:UomToUomID;references:ID"`
FullAuditTrail
}

View File

@ -15,7 +15,15 @@ type MUomEntity struct {
ClientID uuid.UUID `gorm:"type:uuid;index;" json:"client_id"`
Client M_Client `gorm:"foreignKey:ClientID;references:ID" json:"client"`
Client M_Client `gorm:"foreignKey:ClientID;references:ID" json:"client"`
Products []MProductEntity `gorm:"foreignKey:UomID;references:ID"`
DimProducts []MProductEntity `gorm:"foreignKey:DimUomID;references:ID"`
WeightProducts []MProductEntity `gorm:"foreignKey:WeightUomID;references:ID"`
VolumeProducts []MProductEntity `gorm:"foreignKey:VolumeUomID;references:ID"`
MinStockProducts []MProductEntity `gorm:"foreignKey:MinStockUomID;references:ID"`
MaxStockProducts []MProductEntity `gorm:"foreignKey:MaxStockUomID;references:ID"`
LeadTimeProducts []MProductEntity `gorm:"foreignKey:LeadTimeUomID;references:ID"`
UomToUomProducts []MProductEntity `gorm:"foreignKey:UomToUomID;references:ID"`
FullAuditTrail
}

View File

@ -33,19 +33,19 @@ func Migrate(db *gorm.DB) error {
func MigrateFresh(db *gorm.DB) error {
// Drop tables
if err := db.Migrator().DropTable(
&entities.M_Client{},
&entities.M_User{},
&entities.RefreshToken{},
&entities.M_Menu{},
&entities.M_Role{},
&entities.M_Permissions{},
&entities.M_User_Role{},
&entities.M_Role_Menu{},
&entities.M_Menu_Client{},
&entities.M_Role_Permission{},
&entities.M_MaintenanceGroup{},
&entities.M_MaintenanceGroupRole{},
&entities.M_MaintenanceGroupRoleUser{},
// &entities.M_Client{},
// &entities.M_User{},
// &entities.RefreshToken{},
// &entities.M_Menu{},
// &entities.M_Role{},
// &entities.M_Permissions{},
// &entities.M_User_Role{},
// &entities.M_Role_Menu{},
// &entities.M_Menu_Client{},
// &entities.M_Role_Permission{},
// &entities.M_MaintenanceGroup{},
// &entities.M_MaintenanceGroupRole{},
// &entities.M_MaintenanceGroupRoleUser{},
&entities.MCategoryEntity{},
&entities.MProductEntity{},
&entities.MUomEntity{},

View File

@ -2,6 +2,8 @@ package dto
import (
"errors"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
)
const (
@ -95,38 +97,38 @@ type (
}
ProductResponse struct {
ID string `json:"id"`
Name string `json:"name"`
RefNumber string `json:"ref_number"`
SKU string `json:"sku"`
Description string `json:"description"`
Status string `json:"status"`
IsReturnable bool `json:"is_returnable"`
DimLength float64 `json:"dim_length"`
DimWidth float64 `json:"dim_width"`
DimHeight float64 `json:"dim_height"`
Weight float64 `json:"weight"`
Volume float64 `json:"volume"`
MaxStackHeight int `json:"max_stack_height"`
Temperature string `json:"temperature"`
IsHazardous bool `json:"is_hazardous"`
MinStock int `json:"min_stock"`
MaxStock int `json:"max_stock"`
ReplenishType string `json:"replenish_type"`
CycleCount string `json:"cycle_count"`
LotRules string `json:"lot_rules"`
LeadTime int `json:"lead_time"`
MultiplyRate string `json:"multiply_rate"`
DivideRate float64 `json:"divide_rate"`
ClientID string `json:"client_id"`
CategoryID string `json:"category_id"`
UomID string `json:"uom_id"`
DimUomID string `json:"dim_uom_id"`
WeightUomID string `json:"weight_uom_id"`
VolumeUomID string `json:"volume_uom_id"`
MinStockUomID string `json:"min_stock_uom_id"`
MaxStockUomID string `json:"max_stock_uom_id"`
LeadTimeUomID string `json:"lead_time_uom_id"`
UomToUomID string `json:"uom_to_uom_id"`
ID string `json:"id"`
Name string `json:"name"`
RefNumber string `json:"ref_number"`
SKU string `json:"sku"`
Description string `json:"description"`
Status string `json:"status"`
IsReturnable bool `json:"is_returnable"`
DimLength float64 `json:"dim_length"`
DimWidth float64 `json:"dim_width"`
DimHeight float64 `json:"dim_height"`
Weight float64 `json:"weight"`
Volume float64 `json:"volume"`
MaxStackHeight int `json:"max_stack_height"`
Temperature string `json:"temperature"`
IsHazardous bool `json:"is_hazardous"`
MinStock int `json:"min_stock"`
MaxStock int `json:"max_stock"`
ReplenishType string `json:"replenish_type"`
CycleCount string `json:"cycle_count"`
LotRules string `json:"lot_rules"`
LeadTime int `json:"lead_time"`
MultiplyRate string `json:"multiply_rate"`
DivideRate float64 `json:"divide_rate"`
Client pkgdto.IdNameResponse `json:"client"`
Category pkgdto.IdNameResponse `json:"category"`
Uom pkgdto.IdNameResponse `json:"uom"`
DimUom pkgdto.IdNameResponse `json:"dim_uom"`
WeightUom pkgdto.IdNameResponse `json:"weight_uom"`
VolumeUom pkgdto.IdNameResponse `json:"volume_uom"`
MinStockUom pkgdto.IdNameResponse `json:"min_stock_uom"`
MaxStockUom pkgdto.IdNameResponse `json:"max_stock_uom"`
LeadTimeUom pkgdto.IdNameResponse `json:"lead_time_uom"`
UomToUom pkgdto.IdNameResponse `json:"uom_to_uom"`
}
)

View File

@ -39,7 +39,18 @@ func (r *productRepository) GetById(ctx context.Context, tx *gorm.DB, productId
tx = r.db
}
var product entities.MProductEntity
if err := tx.WithContext(ctx).First(&product, "id = ?", productId).Error; err != nil {
if err := tx.WithContext(ctx).
Preload("Client").
Preload("Category").
Preload("Uom").
Preload("DimUom").
Preload("WeightUom").
Preload("VolumeUom").
Preload("MinStockUom").
Preload("MaxStockUom").
Preload("LeadTimeUom").
Preload("UomToUom").
First(&product, "id = ?", productId).Error; err != nil {
return product, err
}
return product, nil

View File

@ -7,6 +7,7 @@ import (
"github.com/Caknoooo/go-gin-clean-starter/modules/product/dto"
"github.com/Caknoooo/go-gin-clean-starter/modules/product/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/product/repository"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/google/uuid"
"gorm.io/gorm"
)
@ -286,15 +287,45 @@ func mapProductToResponse(product entities.MProductEntity) dto.ProductResponse {
LeadTime: product.LeadTime,
MultiplyRate: product.MultiplyRate,
DivideRate: product.DivideRate,
ClientID: product.ClientID.String(),
CategoryID: product.CategoryID.String(),
UomID: product.UomID.String(),
DimUomID: product.DimUomID.String(),
WeightUomID: product.WeightUomID.String(),
VolumeUomID: product.VolumeUomID.String(),
MinStockUomID: product.MinStockUomID.String(),
MaxStockUomID: product.MaxStockUomID.String(),
LeadTimeUomID: product.LeadTimeUomID.String(),
UomToUomID: product.UomToUomID.String(),
Client: pkgdto.IdNameResponse{
ID: product.Client.ID.String(),
Name: product.Client.Name,
},
Category: pkgdto.IdNameResponse{
ID: product.Category.ID.String(),
Name: product.Category.Name,
},
Uom: pkgdto.IdNameResponse{
ID: product.Uom.ID.String(),
Name: product.Uom.Name,
},
DimUom: pkgdto.IdNameResponse{
ID: product.DimUom.ID.String(),
Name: product.DimUom.Name,
},
WeightUom: pkgdto.IdNameResponse{
ID: product.WeightUom.ID.String(),
Name: product.WeightUom.Name,
},
VolumeUom: pkgdto.IdNameResponse{
ID: product.VolumeUom.ID.String(),
Name: product.VolumeUom.Name,
},
MinStockUom: pkgdto.IdNameResponse{
ID: product.MinStockUom.ID.String(),
Name: product.MinStockUom.Name,
},
MaxStockUom: pkgdto.IdNameResponse{
ID: product.MaxStockUom.ID.String(),
Name: product.MaxStockUom.Name,
},
LeadTimeUom: pkgdto.IdNameResponse{
ID: product.LeadTimeUom.ID.String(),
Name: product.LeadTimeUom.Name,
},
UomToUom: pkgdto.IdNameResponse{
ID: product.UomToUom.ID.String(),
Name: product.UomToUom.Name,
},
}
}

View File

@ -1,6 +1,11 @@
package dto
type (
IdNameResponse struct {
ID string `json:"id"`
Name string `json:"name"`
}
UserResponse struct {
ID string `json:"id"`
Name string `json:"name"`
@ -20,4 +25,9 @@ type (
ID string `json:"id"`
Name string `json:"name"`
}
UomResponse struct {
ID string `json:"id"`
Name string `json:"name"`
}
)