61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package mapper
|
|
|
|
import (
|
|
"wm-backend/internal/models"
|
|
db "wm-backend/sqlc_gen"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func ToDomainComponent(r db.Component) *models.Component {
|
|
return &models.Component{
|
|
ID: r.ID,
|
|
ComponentTypeID: r.ComponentTypeID,
|
|
Name: r.Name,
|
|
Description: r.Description.String,
|
|
Unit: r.Unit,
|
|
TotalQuantity: r.TotalQuantity,
|
|
MinQuantity: r.MinQuantity,
|
|
Metadata: json.RawMessage(r.Metadata),
|
|
CreatedAt: r.CreatedAt,
|
|
UpdatedAt: r.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func ToModelComponent(r *models.Component) *db.CreateComponentParams {
|
|
return &db.CreateComponentParams{
|
|
ComponentTypeID: r.ComponentTypeID,
|
|
Name: r.Name,
|
|
Description: pgtype.Text{
|
|
String: r.Description,
|
|
Valid: r.Description != "",
|
|
},
|
|
Unit: r.Unit,
|
|
MinQuantity: r.MinQuantity,
|
|
Metadata: []byte(r.Metadata),
|
|
CreatedAt: r.CreatedAt,
|
|
}
|
|
}
|
|
|
|
func ToUpdateModelComponent(r *models.Component) *db.UpdateComponentParams {
|
|
var metadata []byte
|
|
if len(r.Metadata) > 0 {
|
|
metadata = []byte(r.Metadata)
|
|
}
|
|
return &db.UpdateComponentParams{
|
|
ComponentTypeID: r.ComponentTypeID,
|
|
Name: r.Name,
|
|
Description: pgtype.Text{
|
|
String: r.Description,
|
|
Valid: r.Description != "",
|
|
},
|
|
Unit: r.Unit,
|
|
MinQuantity: r.MinQuantity,
|
|
Metadata: metadata,
|
|
UpdatedAt: r.UpdatedAt,
|
|
ID: r.ID,
|
|
}
|
|
}
|