Skip to content

Commit f61e05a

Browse files
committed
feat: json for billing order
1 parent 3905577 commit f61e05a

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/order_taking/common/decimal.gleam

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import gleam/int
2-
import gleam/option.{type Option, None, Some}
2+
import gleam/option
33
import gleam/string
44

55
pub type Decimal {
66
Decimal(before: Int, after: Int)
77
}
88

9-
pub fn parse(d: String) -> Option(Decimal) {
9+
pub fn parse(d: String) -> option.Option(Decimal) {
1010
case string.split(d, ".") {
1111
[whole, dec] ->
1212
case int.parse(whole), int.parse(dec) {
13-
Ok(i1), Ok(i2) -> Some(Decimal(before: i1, after: i2))
14-
_, _ -> None
13+
Ok(i1), Ok(i2) -> option.Some(Decimal(before: i1, after: i2))
14+
_, _ -> option.None
1515
}
16-
_ -> None
16+
_ -> option.None
1717
}
1818
}
1919

src/order_taking/place_order/dto/address_dto.gleam

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub type AddressDto {
1717
)
1818
}
1919

20+
// For serialising
2021
pub fn to_json(dto: AddressDto) {
2122
json.object([
2223
#("address_line_1", json.string(dto.address_line_1)),
@@ -38,6 +39,8 @@ pub fn to_json(dto: AddressDto) {
3839
|> option.map(json.string)
3940
|> option.unwrap(json.null()),
4041
),
42+
#("city", json.string(dto.city)),
43+
#("zip_code", json.string(dto.zip_code)),
4144
])
4245
}
4346

src/order_taking/place_order/dto/billable_order_placed_dto.gleam

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
import order_taking/common/decimal.{type Decimal}
1+
import gleam/json
2+
import order_taking/common/decimal
23
import order_taking/common/public_types
34
import order_taking/common/simple_types/billing_amount
45
import order_taking/common/simple_types/order_id
5-
import order_taking/place_order/dto/address_dto.{type AddressDto}
6+
import order_taking/place_order/dto/address_dto
67

78
pub type BillableOrderPlacedDto {
89
BillableOrderPlacedDto(
910
order_id: String,
10-
billing_address: AddressDto,
11-
amount_to_bill: Decimal,
11+
billing_address: address_dto.AddressDto,
12+
amount_to_bill: decimal.Decimal,
1213
)
1314
}
1415

16+
// For serialising
17+
pub fn to_json(dto: BillableOrderPlacedDto) {
18+
json.object([
19+
#("order_id", json.string(dto.order_id)),
20+
#("amount_to_bill", json.string(decimal.to_string(dto.amount_to_bill))),
21+
#(
22+
"address",
23+
dto.billing_address
24+
|> address_dto.to_json,
25+
),
26+
])
27+
}
28+
1529
/// Convert a BillableOrderPlaced object into the corresponding DTO.
1630
/// Used when exporting from the domain to the outside world.
1731
pub fn from_domain(

0 commit comments

Comments
 (0)