Skip to content

Commit 6af8e03

Browse files
committed
Update code structure
1 parent bc07a36 commit 6af8e03

File tree

3 files changed

+146
-142
lines changed

3 files changed

+146
-142
lines changed

templates/patterns/complete-patterns/cdk-crud-api-with-event-publisher/lambdas/shared/src/lib.rs

+2-140
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@ mod create_order_command;
33
mod update_order_command;
44
mod get_order_query;
55
mod delete_order_command;
6+
mod order;
67

78
pub use view_models::OrderViewModel;
89
pub use create_order_command::{CreateOrderCommand, CreateOrderCommandHandler};
910
pub use update_order_command::{UpdateOrderCommand, UpdateOrderCommandHandler};
1011
pub use get_order_query::{GetOrderQuery, GetOrderQueryHandler};
1112
pub use delete_order_command::{DeleteOrderCommand, DeleteOrderCommandHandler};
13+
pub use order::{Order, OrderRepository};
1214

1315
use std::env::VarError;
1416
use std::fmt;
15-
use aws_config::meta::region::RegionProviderChain;
16-
use aws_config::Region;
17-
use aws_sdk_dynamodb::Client;
18-
use aws_sdk_dynamodb::error::ProvideErrorMetadata;
19-
use aws_sdk_dynamodb::types::AttributeValue;
20-
use uuid::Uuid;
2117
use thiserror::Error;
2218

2319
#[derive(Debug)]
@@ -48,138 +44,4 @@ impl From<VarError> for ApplicationError {
4844
fn from(_: VarError) -> Self {
4945
ApplicationError::TableNameNotSet()
5046
}
51-
}
52-
53-
struct Order {
54-
order_id: String,
55-
customer_id: String,
56-
other_order_data: String
57-
}
58-
59-
impl Order{
60-
pub fn new (customer_id: String, other_order_data: String) -> Self {
61-
Self {
62-
customer_id,
63-
order_id: Uuid::new_v4().to_string(),
64-
other_order_data
65-
}
66-
}
67-
68-
pub fn update_order_data(&mut self, order_data: String) {
69-
self.other_order_data = order_data
70-
}
71-
72-
pub fn order_id(&self) -> String {
73-
self.order_id.clone()
74-
}
75-
76-
pub fn customer_id(&self) -> String {
77-
self.customer_id.clone()
78-
}
79-
}
80-
81-
pub struct OrderRepository {
82-
client: Client,
83-
table_name: String
84-
}
85-
86-
impl OrderRepository {
87-
pub async fn new(is_local: bool) -> Result<Self, ApplicationError> {
88-
let table_name = std::env::var("TABLE_NAME")?;
89-
90-
let region_provider = RegionProviderChain::default_provider()
91-
.or_else("us-west-2");
92-
let sdk_config = aws_config::from_env().region(region_provider).load().await;
93-
if is_local {
94-
let config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
95-
.endpoint_url("http://localhost:8000".to_string())
96-
.region(Region::from_static("us-east-1"))
97-
.build();
98-
return Ok(Self{
99-
client: Client::from_conf(config),
100-
table_name
101-
});
102-
}
103-
104-
let config = aws_sdk_dynamodb::config::Builder::from(&sdk_config).build();
105-
106-
return Ok(Self{
107-
client: Client::from_conf(config),
108-
table_name
109-
});
110-
}
111-
112-
async fn get_by_id(&self, customer_id: String, order_id: String) -> Result<Order, ApplicationError> {
113-
let get_res = &self.client
114-
.get_item()
115-
.key(DatabaseKeys::PK.to_string(), AttributeValue::S(customer_id))
116-
.key(DatabaseKeys::SK.to_string(), AttributeValue::S(order_id.clone()))
117-
.table_name(&self.table_name)
118-
.send()
119-
.await
120-
.map_err(|err|{
121-
let error_message = err.into_service_error().message().unwrap().to_string();
122-
ApplicationError::DatabaseError(error_message)
123-
})?;
124-
125-
match &get_res.item {
126-
None => Err(ApplicationError::OrderNotFound(order_id.clone())),
127-
Some(item) => {
128-
Ok(Order{
129-
order_id: item.get(&DatabaseKeys::SK.to_string()).unwrap().as_s().unwrap().clone(),
130-
customer_id: item.get(&DatabaseKeys::PK.to_string()).unwrap().as_s().unwrap().clone(),
131-
other_order_data: item.get(&DatabaseKeys::Data.to_string()).unwrap().as_s().unwrap().clone(),
132-
})
133-
}
134-
}
135-
}
136-
137-
async fn add(&self, order: &Order) -> Result<(), ApplicationError> {
138-
let _ = &self.client
139-
.put_item()
140-
.item(DatabaseKeys::PK.to_string(), AttributeValue::S(order.customer_id()))
141-
.item(DatabaseKeys::SK.to_string(), AttributeValue::S(order.order_id()))
142-
.item(DatabaseKeys::Data.to_string(), AttributeValue::S(order.other_order_data.clone()))
143-
.item(
144-
DatabaseKeys::Type.to_string(),
145-
AttributeValue::S("Order".to_string()),
146-
)
147-
.table_name(&self.table_name)
148-
.send()
149-
.await
150-
.map_err(|err| ApplicationError::DatabaseError(err.into_service_error().to_string()));
151-
152-
Ok(())
153-
}
154-
155-
async fn update(&self, order: &Order) -> Result<(), ApplicationError> {
156-
let _ = &self.client
157-
.put_item()
158-
.item(DatabaseKeys::PK.to_string(), AttributeValue::S(order.customer_id()))
159-
.item(DatabaseKeys::SK.to_string(), AttributeValue::S(order.order_id()))
160-
.item(DatabaseKeys::Data.to_string(), AttributeValue::S(order.other_order_data.clone()))
161-
.item(
162-
DatabaseKeys::Type.to_string(),
163-
AttributeValue::S("Order".to_string()),
164-
)
165-
.table_name(&self.table_name)
166-
.send()
167-
.await
168-
.map_err(|err| ApplicationError::DatabaseError(err.into_service_error().to_string()));
169-
170-
Ok(())
171-
}
172-
173-
async fn delete(&self, order: &Order) -> Result<(), ApplicationError> {
174-
let _ = &self.client
175-
.delete_item()
176-
.key(DatabaseKeys::PK.to_string(), AttributeValue::S(order.customer_id()))
177-
.key(DatabaseKeys::SK.to_string(), AttributeValue::S(order.order_id()))
178-
.table_name(&self.table_name)
179-
.send()
180-
.await
181-
.map_err(|err| ApplicationError::DatabaseError(err.into_service_error().to_string()));
182-
183-
Ok(())
184-
}
18547
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
use uuid::Uuid;
2+
use aws_config::meta::region::RegionProviderChain;
3+
use aws_config::Region;
4+
use aws_sdk_dynamodb::Client;
5+
use aws_sdk_dynamodb::error::ProvideErrorMetadata;
6+
use aws_sdk_dynamodb::types::AttributeValue;
7+
8+
use crate::{ApplicationError, DatabaseKeys};
9+
10+
pub struct Order {
11+
order_id: String,
12+
customer_id: String,
13+
other_order_data: String
14+
}
15+
16+
impl Order{
17+
pub fn new (customer_id: String, other_order_data: String) -> Self {
18+
Self {
19+
customer_id,
20+
order_id: Uuid::new_v4().to_string(),
21+
other_order_data
22+
}
23+
}
24+
25+
pub fn update_order_data(&mut self, order_data: String) {
26+
self.other_order_data = order_data
27+
}
28+
29+
pub fn order_id(&self) -> String {
30+
self.order_id.clone()
31+
}
32+
33+
pub fn customer_id(&self) -> String {
34+
self.customer_id.clone()
35+
}
36+
}
37+
38+
pub struct OrderRepository {
39+
client: Client,
40+
table_name: String
41+
}
42+
43+
impl OrderRepository {
44+
pub async fn new(is_local: bool) -> Result<Self, ApplicationError> {
45+
let table_name = std::env::var("TABLE_NAME")?;
46+
47+
let region_provider = RegionProviderChain::default_provider()
48+
.or_else("us-west-2");
49+
let sdk_config = aws_config::from_env().region(region_provider).load().await;
50+
if is_local {
51+
let config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
52+
.endpoint_url("http://localhost:8000".to_string())
53+
.region(Region::from_static("us-east-1"))
54+
.build();
55+
return Ok(Self{
56+
client: Client::from_conf(config),
57+
table_name
58+
});
59+
}
60+
61+
let config = aws_sdk_dynamodb::config::Builder::from(&sdk_config).build();
62+
63+
return Ok(Self{
64+
client: Client::from_conf(config),
65+
table_name
66+
});
67+
}
68+
69+
pub async fn get_by_id(&self, customer_id: String, order_id: String) -> Result<Order, ApplicationError> {
70+
let get_res = &self.client
71+
.get_item()
72+
.key(DatabaseKeys::PK.to_string(), AttributeValue::S(customer_id))
73+
.key(DatabaseKeys::SK.to_string(), AttributeValue::S(order_id.clone()))
74+
.table_name(&self.table_name)
75+
.send()
76+
.await
77+
.map_err(|err|{
78+
let error_message = err.into_service_error().message().unwrap().to_string();
79+
ApplicationError::DatabaseError(error_message)
80+
})?;
81+
82+
match &get_res.item {
83+
None => Err(ApplicationError::OrderNotFound(order_id.clone())),
84+
Some(item) => {
85+
Ok(Order{
86+
order_id: item.get(&DatabaseKeys::SK.to_string()).unwrap().as_s().unwrap().clone(),
87+
customer_id: item.get(&DatabaseKeys::PK.to_string()).unwrap().as_s().unwrap().clone(),
88+
other_order_data: item.get(&DatabaseKeys::Data.to_string()).unwrap().as_s().unwrap().clone(),
89+
})
90+
}
91+
}
92+
}
93+
94+
pub async fn add(&self, order: &Order) -> Result<(), ApplicationError> {
95+
let _ = &self.client
96+
.put_item()
97+
.item(DatabaseKeys::PK.to_string(), AttributeValue::S(order.customer_id()))
98+
.item(DatabaseKeys::SK.to_string(), AttributeValue::S(order.order_id()))
99+
.item(DatabaseKeys::Data.to_string(), AttributeValue::S(order.other_order_data.clone()))
100+
.item(
101+
DatabaseKeys::Type.to_string(),
102+
AttributeValue::S("Order".to_string()),
103+
)
104+
.table_name(&self.table_name)
105+
.send()
106+
.await
107+
.map_err(|err| ApplicationError::DatabaseError(err.into_service_error().to_string()));
108+
109+
Ok(())
110+
}
111+
112+
pub async fn update(&self, order: &Order) -> Result<(), ApplicationError> {
113+
let _ = &self.client
114+
.put_item()
115+
.item(DatabaseKeys::PK.to_string(), AttributeValue::S(order.customer_id()))
116+
.item(DatabaseKeys::SK.to_string(), AttributeValue::S(order.order_id()))
117+
.item(DatabaseKeys::Data.to_string(), AttributeValue::S(order.other_order_data.clone()))
118+
.item(
119+
DatabaseKeys::Type.to_string(),
120+
AttributeValue::S("Order".to_string()),
121+
)
122+
.table_name(&self.table_name)
123+
.send()
124+
.await
125+
.map_err(|err| ApplicationError::DatabaseError(err.into_service_error().to_string()));
126+
127+
Ok(())
128+
}
129+
130+
pub async fn delete(&self, order: &Order) -> Result<(), ApplicationError> {
131+
let _ = &self.client
132+
.delete_item()
133+
.key(DatabaseKeys::PK.to_string(), AttributeValue::S(order.customer_id()))
134+
.key(DatabaseKeys::SK.to_string(), AttributeValue::S(order.order_id()))
135+
.table_name(&self.table_name)
136+
.send()
137+
.await
138+
.map_err(|err| ApplicationError::DatabaseError(err.into_service_error().to_string()));
139+
140+
Ok(())
141+
}
142+
}

templates/patterns/complete-patterns/cdk-crud-api-with-event-publisher/lambdas/shared/src/view_models.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub struct OrderViewModel {
1111
impl From<Order> for OrderViewModel {
1212
fn from(value: Order) -> Self {
1313
Self {
14-
customer_id: value.customer_id,
15-
order_id: value.order_id
14+
customer_id: value.customer_id(),
15+
order_id: value.order_id()
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)