11use crate :: application:: adapters:: { StatementDTO , TransactionDTO } ;
2+ use crate :: application:: cache:: AccountCache ;
3+ use crate :: application:: repositories:: TransactionRepository ;
24use crate :: application:: ServerData ;
5+ use crate :: domain:: account:: Account ;
36use crate :: domain:: transaction:: Transaction ;
4- use crate :: infrastructure:: redis_lock:: RedisLock ;
57use crate :: infrastructure:: server_impl:: request:: Request ;
68use crate :: infrastructure:: server_impl:: response:: { JsonResponse , Response , StatusCode } ;
79use crate :: infrastructure:: server_impl:: server:: Method ;
8- use crate :: { AnyResult , Statement } ;
9- use compact_str:: CompactString ;
10+ use crate :: AnyResult ;
1011use eyre:: bail;
11- use redis:: streams:: StreamRangeReply ;
12- use redis:: { AsyncCommands , Client , Value } ;
13- use serde:: { Deserialize , Serialize } ;
14- use std:: mem;
15- use std:: sync:: Arc ;
16- use time:: OffsetDateTime ;
12+ use fnv:: FnvHashMap ;
13+ use std:: sync:: { Arc , Mutex } ;
1714
1815pub mod input_types;
1916
@@ -28,13 +25,16 @@ pub async fn statement_route(
2825 bail ! ( "Only GET available." )
2926 }
3027
31- let my = Statement {
32- balance : 0 ,
33- time_of_statement : OffsetDateTime :: now_utc ( ) ,
34- credit_limit : 0 ,
28+ let service = BankAccountService {
29+ re_conn : server_data. re_conn . clone ( ) ,
30+ pg_conn : server_data. pg_pool . clone ( ) ,
3531 } ;
3632
37- let a = JsonResponse :: from :: < StatementDTO > ( my. into ( ) ) ;
33+ let res = service
34+ . query ( AccountQueries :: Statement { account : client_id } )
35+ . await ;
36+
37+ let a = JsonResponse :: from :: < StatementDTO > ( StatementDTO :: from_other ( res) ) ;
3838 Ok ( a. 0 )
3939}
4040
@@ -54,7 +54,7 @@ pub async fn transaction_route(
5454 . unwrap ( ) ;
5555
5656 let mapping = serde_json:: from_slice :: < TransactionDTO > ( body)
57- . map_err ( |e | ( ) )
57+ . map_err ( |_ | ( ) )
5858 . and_then ( |c| c. try_into ( ) ) ;
5959
6060 let transaction: Transaction = match mapping {
@@ -83,69 +83,78 @@ pub async fn transaction_route(
8383 // -> insert transaction and balance on redis
8484 // -> remove lock
8585
86- let command = AccountCommands :: WithdrawMoney {
86+ let command = AccountCommands :: HandleMoney {
8787 account : client_id,
88- transaction : & transaction ,
88+ transaction,
8989 } ;
9090
91- let res = BankAccountService {
91+ let bank_service = BankAccountService {
9292 re_conn : server_data. re_conn . clone ( ) ,
9393 pg_conn : server_data. pg_pool . clone ( ) ,
94- }
95- . handler ( command)
96- . await ;
94+ } ;
95+ let res = bank_service. handler ( command) . await ;
96+
97+ // if res.is_err() {
98+ // return Ok(Response::from_status_code(
99+ // StatusCode::UnprocessableEntity,
100+ // None,
101+ // ));
102+ // }
97103
98- let a = JsonResponse :: from :: < TransactionDTO > ( transaction. into ( ) ) ;
104+ let a =
105+ JsonResponse :: from :: < TransactionDTO > ( TransactionDTO :: from ( Transaction :: generate ( 1 , None ) ) ) ;
99106 Ok ( a. 0 )
100107}
101108
109+ pub type AccountMapStorage = Arc < FnvHashMap < i32 , ( u32 , Mutex < Account > ) > > ;
110+
102111struct BankAccountService {
103112 re_conn : redis:: aio:: ConnectionManager ,
104113 pg_conn : deadpool_postgres:: Pool ,
114+ // storage: AccountMapStorage,
105115}
106116
107- enum AccountCommands < ' a > {
108- DepositMoney {
109- user : i32 ,
110- amount : & ' a Transaction ,
111- } ,
112- WithdrawMoney {
117+ #[ derive( Debug ) ]
118+ enum AccountCommands {
119+ HandleMoney {
113120 account : i32 ,
114- transaction : & ' a Transaction ,
121+ transaction : Transaction ,
115122 } ,
116123}
117124
125+ #[ derive( Debug ) ]
118126enum AccountQueries {
119- Statement ,
127+ Statement { account : i32 } ,
120128}
121129
122130impl BankAccountService {
123- async fn handler ( & self , command : AccountCommands < ' _ > ) {
131+ async fn query ( & self , command : AccountQueries ) -> ( Account , impl Iterator < Item = Transaction > ) {
124132 match command {
125- AccountCommands :: DepositMoney { .. } => unimplemented ! ( ) ,
126- AccountCommands :: WithdrawMoney {
133+ AccountQueries :: Statement { account : user } => {
134+ let trans_cache = AccountCache {
135+ re_conn : self . re_conn . clone ( ) ,
136+ } ;
137+
138+ let res = trans_cache. get_account ( user, true ) . await ;
139+ ( res. 0 , res. 1 . unwrap ( ) )
140+ }
141+ }
142+ }
143+ async fn handler ( & self , command : AccountCommands ) -> AnyResult < ( ) > {
144+ match command {
145+ AccountCommands :: HandleMoney {
127146 account : user,
128147 transaction,
129148 } => {
130149 let trans_repo = TransactionRepository {
131150 conn : self . pg_conn . clone ( ) ,
132151 } ;
133152
134- let trans_cache = TransactionCache {
153+ let trans_cache = AccountCache {
135154 re_conn : self . re_conn . clone ( ) ,
136155 } ;
137156
138- let redis_lock = RedisLock :: new ( self . re_conn . clone ( ) , user, 100 ) ;
139- {
140- let guard = redis_lock. acquire ( ) . await . unwrap ( ) ;
141- trans_repo. save_transaction ( user, transaction) . await ;
142- trans_cache. append ( user, transaction) . await ;
143- guard. release ( ) . await ;
144- }
145- }
146- }
147- }
148- }
157+ // let redis_lock = RedisLock::new(self.re_conn.clone(), user, 100);
149158
150159 let ( acc, _) = trans_cache. get_account ( user, false ) . await ;
151160 let acc = acc
0 commit comments