feat: add invoice and alternative_componen management functionality
This commit is contained in:
67
internal/mapper/alternative_component_mapper.go
Normal file
67
internal/mapper/alternative_component_mapper.go
Normal file
@@ -0,0 +1,67 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user