68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package mapper
|
|
|
|
import (
|
|
"encoding/json"
|
|
"wm-backend/internal/models"
|
|
db "wm-backend/sqlc_gen"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func numericToString(n pgtype.Numeric) string {
|
|
if !n.Valid {
|
|
return ""
|
|
}
|
|
b, _ := json.Marshal(n)
|
|
return string(b)
|
|
}
|
|
|
|
func stringToNumeric(s string) pgtype.Numeric {
|
|
var n pgtype.Numeric
|
|
if s == "" {
|
|
return n
|
|
}
|
|
_ = json.Unmarshal([]byte(s), &n)
|
|
return n
|
|
}
|
|
|
|
func ToDomainAlternativeComponent(r db.AlternativeComponent) *models.AlternativeComponent {
|
|
return &models.AlternativeComponent{
|
|
ID: r.ID,
|
|
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
|
AlternativeComponentID: r.AlternativeComponentID,
|
|
ConversionRatio: numericToString(r.ConversionRatio),
|
|
Priority: r.Priority,
|
|
Note: r.Note.String,
|
|
Metadata: r.Metadata,
|
|
}
|
|
}
|
|
|
|
func ToModelAlternativeComponent(r *models.AlternativeComponent) *db.CreateAlternativeComponentParams {
|
|
return &db.CreateAlternativeComponentParams{
|
|
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
|
AlternativeComponentID: r.AlternativeComponentID,
|
|
ConversionRatio: stringToNumeric(r.ConversionRatio),
|
|
Priority: r.Priority,
|
|
Note: pgtype.Text{
|
|
String: r.Note,
|
|
Valid: r.Note != "",
|
|
},
|
|
Metadata: r.Metadata,
|
|
}
|
|
}
|
|
|
|
func ToUpdateModelAlternativeComponent(r *models.AlternativeComponent) *db.UpdateAlternativeComponentParams {
|
|
return &db.UpdateAlternativeComponentParams{
|
|
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
|
AlternativeComponentID: r.AlternativeComponentID,
|
|
ConversionRatio: stringToNumeric(r.ConversionRatio),
|
|
Priority: r.Priority,
|
|
Note: pgtype.Text{
|
|
String: r.Note,
|
|
Valid: r.Note != "",
|
|
},
|
|
Metadata: r.Metadata,
|
|
ID: r.ID,
|
|
}
|
|
}
|