Skip to content

Commit

Permalink
fix: address converstion
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy-lmao committed May 31, 2024
1 parent a4454d3 commit 79f3d2a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/order_taking/common/public_types.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gleam/option.{type Option}
import order_taking/common/compound_types.{type Address, type CustomerInfo}
import order_taking/common/decimal.{type Decimal}
import order_taking/common/simple_types/billing_amount.{type BillingAmount}
Expand Down Expand Up @@ -27,9 +28,9 @@ pub type UnvalidatedCustomerInfo {
pub type UnvalidatedAddress {
UnvalidatedAddress(
address_line_1: String,
address_line_2: String,
address_line_3: String,
address_line_4: String,
address_line_2: Option(String),
address_line_3: Option(String),
address_line_4: Option(String),
city: String,
zip_code: String,
)
Expand Down
6 changes: 6 additions & 0 deletions src/order_taking/common/simple_types/zip_code.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub type ZipCode {
ZipCode(String)
}

/// Return the value inside a ZipCode
pub fn value(zip_code: ZipCode) -> String {
let ZipCode(str) = zip_code
str
}

/// Create a ZipCode from a string
/// Return Error if input is null, empty, or doesn't have 5 digits
pub fn create(str, field_name) -> Result(ZipCode, String) {
Expand Down
86 changes: 86 additions & 0 deletions src/order_taking/place_order/dto/address.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import gleam/option.{type Option, None, Some}
import gleam/result.{try}
import order_taking/common/compound_types
import order_taking/common/public_types
import order_taking/common/simple_types/string50
import order_taking/common/simple_types/zip_code

pub type AddressDto {
AddressDto(
address_line_1: String,
address_line_2: Option(String),
address_line_3: Option(String),
address_line_4: Option(String),
city: String,
zip_code: String,
)
}

/// This always succeeds because there is no validation.
/// Used when importing an OrderForm from the outside world into the domain.
pub fn to_unvalidated_address(
dto: AddressDto,
) -> public_types.UnvalidatedAddress {
// this is a simple 1:1 copy
public_types.UnvalidatedAddress(
address_line_1: dto.address_line_1,
address_line_2: dto.address_line_2,
address_line_3: dto.address_line_3,
address_line_4: dto.address_line_4,
city: dto.city,
zip_code: dto.zip_code,
)
}

/// Convert the DTO into a Address object
/// Used when importing from the outside world into the domain, eg loading from a database.
pub fn to_address(dto: AddressDto) -> Result(compound_types.Address, String) {
// get each (validated) simple type from the DTO as a success or failure
use address_line_1 <- try(
dto.address_line_1
|> string50.create("address_line_1"),
)
use address_line_2 <- try(case dto.address_line_2 {
Some(addr) -> string50.create_option(addr, "address_line_2")
None -> Ok(None)
})
use address_line_3 <- try(case dto.address_line_2 {
Some(addr) -> string50.create_option(addr, "address_line_3")
None -> Ok(None)
})
use address_line_4 <- try(case dto.address_line_2 {
Some(addr) -> string50.create_option(addr, "address_line_4")
None -> Ok(None)
})
use city <- try(dto.city |> string50.create("city"))
use zip_code <- try(dto.zip_code |> zip_code.create("zip_code"))

// combine the components to create the domain object
let address =
compound_types.Address(
address_line_1: address_line_1,
address_line_2: address_line_2,
address_line_3: address_line_3,
address_line_4: address_line_4,
city: city,
zip_code: zip_code,
)
Ok(address)
}

/// Convert a Address object into the corresponding DTO.
/// Used when exporting from the domain to the outside world.
pub fn from_address(domain_obj: compound_types.Address) -> AddressDto {
// this is a simple 1:1 copy
AddressDto(
address_line_1: domain_obj.address_line_1 |> string50.value,
address_line_2: domain_obj.address_line_2
|> option.map(string50.value),
address_line_3: domain_obj.address_line_3
|> option.map(string50.value),
address_line_4: domain_obj.address_line_4
|> option.map(string50.value),
city: domain_obj.city |> string50.value,
zip_code: domain_obj.zip_code |> zip_code.value,
)
}

0 comments on commit 79f3d2a

Please sign in to comment.