apskel-pos-backend/internal/transformer/ingredient_unit_converter_transformer.go

179 lines
4.7 KiB
Go
Raw Normal View History

2025-09-12 01:12:11 +07:00
package transformer
import (
"apskel-pos-be/internal/contract"
"apskel-pos-be/internal/models"
)
// Request transformations
func CreateIngredientUnitConverterRequestToModel(req *contract.CreateIngredientUnitConverterRequest) *models.CreateIngredientUnitConverterRequest {
if req == nil {
return nil
}
return &models.CreateIngredientUnitConverterRequest{
IngredientID: req.IngredientID,
FromUnitID: req.FromUnitID,
ToUnitID: req.ToUnitID,
ConversionFactor: req.ConversionFactor,
IsActive: req.IsActive,
}
}
func UpdateIngredientUnitConverterRequestToModel(req *contract.UpdateIngredientUnitConverterRequest) *models.UpdateIngredientUnitConverterRequest {
if req == nil {
return nil
}
return &models.UpdateIngredientUnitConverterRequest{
FromUnitID: req.FromUnitID,
ToUnitID: req.ToUnitID,
ConversionFactor: req.ConversionFactor,
IsActive: req.IsActive,
}
}
func ListIngredientUnitConvertersRequestToModel(req *contract.ListIngredientUnitConvertersRequest) *models.ListIngredientUnitConvertersRequest {
if req == nil {
return nil
}
return &models.ListIngredientUnitConvertersRequest{
IngredientID: req.IngredientID,
FromUnitID: req.FromUnitID,
ToUnitID: req.ToUnitID,
IsActive: req.IsActive,
Search: req.Search,
Page: req.Page,
Limit: req.Limit,
}
}
func ConvertUnitRequestToModel(req *contract.ConvertUnitRequest) *models.ConvertUnitRequest {
if req == nil {
return nil
}
return &models.ConvertUnitRequest{
IngredientID: req.IngredientID,
FromUnitID: req.FromUnitID,
ToUnitID: req.ToUnitID,
Quantity: req.Quantity,
}
}
// Response transformations
func IngredientUnitConverterModelResponseToResponse(model *models.IngredientUnitConverterResponse) contract.IngredientUnitConverterResponse {
if model == nil {
return contract.IngredientUnitConverterResponse{}
}
response := contract.IngredientUnitConverterResponse{
ID: model.ID,
OrganizationID: model.OrganizationID,
IngredientID: model.IngredientID,
FromUnitID: model.FromUnitID,
ToUnitID: model.ToUnitID,
ConversionFactor: model.ConversionFactor,
IsActive: model.IsActive,
CreatedAt: model.CreatedAt,
UpdatedAt: model.UpdatedAt,
CreatedBy: model.CreatedBy,
UpdatedBy: model.UpdatedBy,
}
// Map related entities
if model.Ingredient != nil {
ingredientResp := IngredientModelResponseToResponse(model.Ingredient)
response.Ingredient = &ingredientResp
}
if model.FromUnit != nil {
fromUnitResp := UnitModelResponseToResponse(model.FromUnit)
response.FromUnit = &fromUnitResp
}
if model.ToUnit != nil {
toUnitResp := UnitModelResponseToResponse(model.ToUnit)
response.ToUnit = &toUnitResp
}
return response
}
func ConvertUnitModelResponseToResponse(model *models.ConvertUnitResponse) contract.ConvertUnitResponse {
if model == nil {
return contract.ConvertUnitResponse{}
}
response := contract.ConvertUnitResponse{
FromQuantity: model.FromQuantity,
ToQuantity: model.ToQuantity,
ConversionFactor: model.ConversionFactor,
}
// Map units
if model.FromUnit != nil {
fromUnitResp := UnitModelResponseToResponse(model.FromUnit)
response.FromUnit = &fromUnitResp
}
if model.ToUnit != nil {
toUnitResp := UnitModelResponseToResponse(model.ToUnit)
response.ToUnit = &toUnitResp
}
// Map ingredient
if model.Ingredient != nil {
ingredientResp := IngredientModelResponseToResponse(model.Ingredient)
response.Ingredient = &ingredientResp
}
return response
}
// Helper functions for related entities
func IngredientModelResponseToResponse(model *models.IngredientResponse) contract.IngredientResponse {
if model == nil {
return contract.IngredientResponse{}
}
return contract.IngredientResponse{
ID: model.ID,
Name: model.Name,
}
}
func UnitModelResponseToResponse(model *models.UnitResponse) contract.UnitResponse {
if model == nil {
return contract.UnitResponse{}
}
return contract.UnitResponse{
ID: model.ID,
Name: model.Name,
}
}
2025-09-12 15:37:19 +07:00
func IngredientUnitsModelResponseToResponse(model *models.IngredientUnitsResponse) contract.IngredientUnitsResponse {
if model == nil {
return contract.IngredientUnitsResponse{}
}
response := contract.IngredientUnitsResponse{
IngredientID: model.IngredientID,
IngredientName: model.IngredientName,
BaseUnitID: model.BaseUnitID,
BaseUnitName: model.BaseUnitName,
Units: make([]*contract.UnitResponse, 0),
}
// Map units
for _, unit := range model.Units {
if unit != nil {
unitResp := UnitModelResponseToResponse(unit)
response.Units = append(response.Units, &unitResp)
}
}
return response
}