@@ -3,21 +3,17 @@ mod create_order_command;
3
3
mod update_order_command;
4
4
mod get_order_query;
5
5
mod delete_order_command;
6
+ mod order;
6
7
7
8
pub use view_models:: OrderViewModel ;
8
9
pub use create_order_command:: { CreateOrderCommand , CreateOrderCommandHandler } ;
9
10
pub use update_order_command:: { UpdateOrderCommand , UpdateOrderCommandHandler } ;
10
11
pub use get_order_query:: { GetOrderQuery , GetOrderQueryHandler } ;
11
12
pub use delete_order_command:: { DeleteOrderCommand , DeleteOrderCommandHandler } ;
13
+ pub use order:: { Order , OrderRepository } ;
12
14
13
15
use std:: env:: VarError ;
14
16
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 ;
21
17
use thiserror:: Error ;
22
18
23
19
#[ derive( Debug ) ]
@@ -48,138 +44,4 @@ impl From<VarError> for ApplicationError {
48
44
fn from ( _: VarError ) -> Self {
49
45
ApplicationError :: TableNameNotSet ( )
50
46
}
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
- }
185
47
}
0 commit comments