Skip to content

Commit

Permalink
feat: priced line order
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy-lmao committed May 31, 2024
1 parent 32855b0 commit a2b9ddd
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/order_taking/common/simple_types/gizmo_code.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub type GizmoCode {
GizmoCode(String)
}

/// Return the value inside an GizmoCode
pub fn value(id) -> String {
let GizmoCode(str) = id
str
}

/// Create an GizmoCode from a string
/// Return Error if input is null, empty, or not matching pattern
pub fn create(str, field_name) -> Result(GizmoCode, String) {
Expand Down
10 changes: 8 additions & 2 deletions src/order_taking/common/simple_types/kilogram_quantity.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ pub type KilogramQuantity {
KilogramQuantity(Decimal)
}

// /// Create a KilogramQuantity from a int
// /// Return Error if input is not an integer between 1 and 1000
/// Return the value inside an KilogramQuantity
pub fn value(qty) -> Decimal {
let KilogramQuantity(i) = qty
i
}

/// Create a KilogramQuantity from a int
/// Return Error if input is not an integer between 1 and 1000
pub fn create(
quantity: Decimal,
field_name: String,
Expand Down
6 changes: 6 additions & 0 deletions src/order_taking/common/simple_types/order_line_id.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub type OrderLineId {
OrderLineId(String)
}

/// Return the value inside an OrderLineId
pub fn value(id) -> String {
let OrderLineId(str) = id
str
}

/// Create an OrderLineId from a string
/// Return Error if input is null, empty, or length > 50
pub fn create(str, field_name) -> Result(OrderLineId, String) {
Expand Down
8 changes: 8 additions & 0 deletions src/order_taking/common/simple_types/order_quantity.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ pub type OrderQuantity {
Kilogram(KilogramQuantity)
}

/// Return the value inside an OrderQuantity
pub fn value(qty) {
case qty {
Unit(qty) -> unit_quantity.value(qty) |> decimal.from_int
Kilogram(qty) -> kilogram_quantity.value(qty)
}
}

pub fn create(
code: ProductCode,
quantity: String,
Expand Down
8 changes: 8 additions & 0 deletions src/order_taking/common/simple_types/product_code.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ pub type ProductCode {
Gizmo(GizmoCode)
}

/// Return the value inside an ProductCode
pub fn value(id) -> String {
case id {
Widget(code) -> widget_code.value(code)
Gizmo(code) -> gizmo_code.value(code)
}
}

/// Create an ProductCode from a string
/// Return Error if input is null, empty, or not matching pattern
pub fn create(code, field_name) -> Result(ProductCode, String) {
Expand Down
6 changes: 6 additions & 0 deletions src/order_taking/common/simple_types/unit_quantity.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub type UnitQuantity {
UnitQuantity(Int)
}

/// Return the value inside an UnitQuantity
pub fn value(qty) -> Int {
let UnitQuantity(i) = qty
i
}

// /// Create a UnitQuantity from a int
// /// Return Error if input is not an integer between 1 and 1000
pub fn create(quantity: Int, field_name: String) -> Result(UnitQuantity, String) {
Expand Down
6 changes: 6 additions & 0 deletions src/order_taking/common/simple_types/widget_code.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub type WidgetCode {
WidgetCode(String)
}

/// Return the value inside an WidgetCode
pub fn value(id) -> String {
let WidgetCode(str) = id
str
}

/// Create an WidgetCode from a string
/// Return Error if input is null. empty, or not matching pattern
pub fn create(str, field_name) -> Result(WidgetCode, String) {
Expand Down
25 changes: 25 additions & 0 deletions src/order_taking/place_order/dto/order_line.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import order_taking/common/decimal.{type Decimal}
import order_taking/common/public_types

/// From the order form used as input
pub type OrderFormLineDto {
OrderFormLineDto(
order_line_id: String,
product_code: String,
quantity: Decimal,
)
}

/// Convert the OrderFormLine into a UnvalidatedOrderLine
/// This always succeeds because there is no validation.
/// Used when importing an OrderForm from the outside world into the domain.
pub fn to_unvalidated_order_line(
dto: OrderFormLineDto,
) -> public_types.UnvalidatedOrderLine {
// this is a simple 1:1 copy
public_types.UnvalidatedOrderLine(
order_line_id: dto.order_line_id,
product_code: dto.product_code,
quantity: dto.quantity,
)
}
30 changes: 30 additions & 0 deletions src/order_taking/place_order/dto/priced_order_line.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import order_taking/common/decimal.{type Decimal}
import order_taking/common/public_types
import order_taking/common/simple_types/order_line_id
import order_taking/common/simple_types/order_quantity
import order_taking/common/simple_types/price
import order_taking/common/simple_types/product_code

/// Used in the output of the workflow
pub type PricedOrderLineDto {
PricedOrderLineDto(
order_line_id: String,
product_code: String,
quantity: Decimal,
line_price: Decimal,
)
}

/// Convert a PricedOrderLine object into the corresponding DTO.
/// Used when exporting from the domain to the outside world.
pub fn from_domain(
domain_obj: public_types.PricedOrderLine,
) -> PricedOrderLineDto {
// this is a simple 1:1 copy
PricedOrderLineDto(
order_line_id: domain_obj.order_line_id |> order_line_id.value,
product_code: domain_obj.product_code |> product_code.value,
quantity: domain_obj.quantity |> order_quantity.value,
line_price: domain_obj.line_price |> price.value,
)
}

0 comments on commit a2b9ddd

Please sign in to comment.