mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package dto
|
|
|
|
import (
|
|
"time"
|
|
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
supplierDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto"
|
|
uomDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/dto"
|
|
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
|
)
|
|
|
|
// === DTO Structs ===
|
|
|
|
type NonstockRelationDTO struct {
|
|
Id uint `json:"id"`
|
|
Name string `json:"name"`
|
|
Uom *uomDTO.UomRelationDTO `json:"uom,omitempty"`
|
|
Flags []string `json:"flags"`
|
|
}
|
|
|
|
type NonstockListDTO struct {
|
|
Id uint `json:"id"`
|
|
Name string `json:"name"`
|
|
Flags []string `json:"flags"`
|
|
Uom *uomDTO.UomRelationDTO `json:"uom"`
|
|
Suppliers []supplierDTO.SupplierRelationDTO `json:"suppliers"`
|
|
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
type NonstockDetailDTO struct {
|
|
NonstockListDTO
|
|
}
|
|
|
|
// === Mapper Functions ===
|
|
|
|
func ToNonstockRelationDTO(e entity.Nonstock) NonstockRelationDTO {
|
|
var uomRef *uomDTO.UomRelationDTO
|
|
if e.Uom.Id != 0 {
|
|
mapped := uomDTO.ToUomRelationDTO(e.Uom)
|
|
uomRef = &mapped
|
|
}
|
|
|
|
flags := make([]string, len(e.Flags))
|
|
for i, f := range e.Flags {
|
|
flags[i] = f.Name
|
|
}
|
|
|
|
return NonstockRelationDTO{
|
|
Id: e.Id,
|
|
Name: e.Name,
|
|
Uom: uomRef,
|
|
Flags: flags,
|
|
}
|
|
}
|
|
|
|
func ToNonstockListDTO(e entity.Nonstock) NonstockListDTO {
|
|
var createdUser *userDTO.UserRelationDTO
|
|
if e.CreatedUser.Id != 0 {
|
|
mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
|
|
createdUser = &mapped
|
|
}
|
|
|
|
var uomRef *uomDTO.UomRelationDTO
|
|
if e.Uom.Id != 0 {
|
|
mapped := uomDTO.ToUomRelationDTO(e.Uom)
|
|
uomRef = &mapped
|
|
}
|
|
|
|
flags := make([]string, len(e.Flags))
|
|
for i, f := range e.Flags {
|
|
flags[i] = f.Name
|
|
}
|
|
|
|
return NonstockListDTO{
|
|
Id: e.Id,
|
|
Name: e.Name,
|
|
Flags: flags,
|
|
Uom: uomRef,
|
|
Suppliers: toNonstockSupplierDTOs(e.NonstockSuppliers),
|
|
CreatedAt: e.CreatedAt,
|
|
UpdatedAt: e.UpdatedAt,
|
|
CreatedUser: createdUser,
|
|
}
|
|
}
|
|
|
|
func ToNonstockListDTOs(e []entity.Nonstock) []NonstockListDTO {
|
|
result := make([]NonstockListDTO, len(e))
|
|
for i, r := range e {
|
|
result[i] = ToNonstockListDTO(r)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func ToNonstockDetailDTO(e entity.Nonstock) NonstockDetailDTO {
|
|
return NonstockDetailDTO{
|
|
NonstockListDTO: ToNonstockListDTO(e),
|
|
}
|
|
}
|
|
|
|
func toNonstockSupplierDTOs(relations []entity.NonstockSupplier) []supplierDTO.SupplierRelationDTO {
|
|
if len(relations) == 0 {
|
|
return make([]supplierDTO.SupplierRelationDTO, 0)
|
|
}
|
|
|
|
result := make([]supplierDTO.SupplierRelationDTO, 0, len(relations))
|
|
for _, relation := range relations {
|
|
if relation.Supplier.Id == 0 {
|
|
continue
|
|
}
|
|
result = append(result, supplierDTO.ToSupplierRelationDTO(relation.Supplier))
|
|
}
|
|
|
|
if len(result) == 0 {
|
|
return make([]supplierDTO.SupplierRelationDTO, 0)
|
|
}
|
|
|
|
return result
|
|
}
|