|
| 1 | +// ====================================================== |
| 2 | +// This file contains the JSON API interface to the PlaceOrder workflow |
| 3 | +// |
| 4 | +// 1) The HttpRequest is turned into a DTO, which is then turned into a Domain object |
| 5 | +// 2) The main workflow function is called |
| 6 | +// 3) The output is turned into a DTO which is turned into a HttpResponse |
| 7 | +// ====================================================== |
| 8 | + |
| 9 | +import gleam/http/request |
| 10 | +import gleam/http/response |
| 11 | +import gleam/json |
| 12 | +import gleam/list |
| 13 | +import gleam/result |
| 14 | +import order_taking/common/public_types |
| 15 | +import order_taking/common/simple_types/price |
| 16 | +import order_taking/place_order/dto/place_order_event_dto |
| 17 | +import order_taking/place_order/implementation |
| 18 | + |
| 19 | +pub type JsonString = |
| 20 | + String |
| 21 | + |
| 22 | +// pub type PlaceOrderApi = |
| 23 | +// fn(request.Request) -> response.Response |
| 24 | + |
| 25 | +// ============================= |
| 26 | +// Implementation |
| 27 | +// ============================= |
| 28 | + |
| 29 | +/// Very simplified version! |
| 30 | +/// An API takes a HttpRequest as input and returns a async response |
| 31 | +/// setup dummy dependencies |
| 32 | +pub fn check_product_exists(_product_code) { |
| 33 | + True |
| 34 | +} |
| 35 | + |
| 36 | +/// dummy implementation |
| 37 | +pub fn check_address_exists(unvalidated_address) { |
| 38 | + let checked_address = implementation.CheckedAddress(unvalidated_address) |
| 39 | + |
| 40 | + checked_address |
| 41 | +} |
| 42 | + |
| 43 | +/// dummy implementation |
| 44 | +pub fn get_product_price(_product_code) { |
| 45 | + price.from_int(1) |
| 46 | +} |
| 47 | + |
| 48 | +/// dummy implementation |
| 49 | +pub fn create_order_acknowledgment_letter(_priced_order) { |
| 50 | + let letter_test = implementation.HtmlString("some text") |
| 51 | + letter_test |
| 52 | +} |
| 53 | + |
| 54 | +/// dummy implementation |
| 55 | +pub fn send_order_acknowledgment(_order_acknowledgement) { |
| 56 | + implementation.Sent |
| 57 | +} |
| 58 | + |
| 59 | +// // ------------------------------- |
| 60 | +// // workflow |
| 61 | +// // ------------------------------- |
| 62 | + |
| 63 | +/// This function converts the workflow output into a HttpResponse |
| 64 | +pub fn workflow_result_to_http_reponse(result) -> response.Response(String) { |
| 65 | + case result { |
| 66 | + Ok(events) -> { |
| 67 | + // turn domain events into dtos |
| 68 | + let dtos = |
| 69 | + events |
| 70 | + |> list.map(place_order_event_dto.from_domain) |
| 71 | + |
| 72 | + // and serialize to JSON |
| 73 | + let json = |
| 74 | + dtos |
| 75 | + |> list.map(fn(event) { todo }) |
| 76 | + |
| 77 | + let response = response.Response() |
| 78 | + response |
| 79 | + } |
| 80 | + Error(err) -> |
| 81 | + // // turn domain errors into a dto |
| 82 | + // let dto = err |> PlaceOrderErrorDto.fromDomain |
| 83 | + // // and serialize to JSON |
| 84 | + // let json = serializeJson(dto ) |
| 85 | + // let response = |
| 86 | + // { |
| 87 | + // HttpStatusCode = 401 |
| 88 | + // Body = json |
| 89 | + // } |
| 90 | + // response |
| 91 | + todo |
| 92 | + } |
| 93 | +} |
| 94 | +// let placeOrderApi : PlaceOrderApi = |
| 95 | +// fun request -> |
| 96 | +// // following the approach in "A Complete Serialization Pipeline" in chapter 11 |
| 97 | + |
| 98 | +// // start with a string |
| 99 | +// let orderFormJson = request.Body |
| 100 | +// let orderForm = deserializeJson<OrderFormDto>(orderFormJson) |
| 101 | +// // convert to domain object |
| 102 | +// let unvalidatedOrder = orderForm |> OrderFormDto.toUnvalidatedOrder |
| 103 | + |
| 104 | +// // setup the dependencies. See "Injecting Dependencies" in chapter 9 |
| 105 | +// let workflow = |
| 106 | +// Implementation.placeOrder |
| 107 | +// checkProductExists // dependency |
| 108 | +// checkAddressExists // dependency |
| 109 | +// getProductPrice // dependency |
| 110 | +// createOrderAcknowledgmentLetter // dependency |
| 111 | +// sendOrderAcknowledgment // dependency |
| 112 | + |
| 113 | +// // now we are in the pure domain |
| 114 | +// let asyncResult = workflow unvalidatedOrder |
| 115 | + |
| 116 | +// // now convert from the pure domain back to a HttpResponse |
| 117 | +// asyncResult |
| 118 | +// |> Async.map (workflowResultToHttpReponse) |
0 commit comments