|
1 | | -use crate::server_impl::response::Response; |
2 | | -use crate::server_impl::server::{AnyResult, Method}; |
| 1 | +use crate::application::ServerData; |
| 2 | +use crate::infrastructure::redis_lock::RedisLock; |
| 3 | +use crate::infrastructure::server_impl::request::Request; |
| 4 | +use crate::infrastructure::server_impl::response::{JsonResponse, Response}; |
| 5 | +use crate::infrastructure::server_impl::server::Method; |
| 6 | +use crate::{AnyResult, Statement}; |
3 | 7 | use eyre::bail; |
4 | | -use crate::server_impl::request::Request; |
| 8 | +use redis::Client; |
| 9 | +use std::sync::Arc; |
| 10 | +use time::OffsetDateTime; |
5 | 11 |
|
6 | | -pub fn statement_route(req: Request) -> AnyResult<Response> { |
7 | | - let body = req.body.unwrap(); |
| 12 | +pub fn statement_route( |
| 13 | + server_data: &ServerData, |
| 14 | + req: Request, |
| 15 | + client_id: i32, |
| 16 | +) -> AnyResult<Response> { |
| 17 | + // let body = req.body.unwrap(); |
8 | 18 |
|
9 | 19 | if req.method != Method::GET { |
10 | 20 | bail!("Only GET available.") |
11 | 21 | } |
12 | 22 |
|
13 | | - // let a = simd_json::from_slice::<Statement>(body.as_str()); |
14 | | - todo!() |
| 23 | + let my = Statement { |
| 24 | + total: 0, |
| 25 | + data_extrato: OffsetDateTime::now_utc(), |
| 26 | + limite: 0, |
| 27 | + }; |
| 28 | + |
| 29 | + let a = JsonResponse::from::<Statement>(my); |
| 30 | + Ok(a.0) |
15 | 31 | } |
16 | | -pub fn transaction_route(req: Request) -> AnyResult<Response> { |
| 32 | +pub fn transaction_route( |
| 33 | + server_data: &ServerData, |
| 34 | + req: Request, |
| 35 | + client_id: i32, |
| 36 | +) -> AnyResult<Response> { |
17 | 37 | if req.method != Method::POST { |
18 | 38 | bail!("Only POST available.") |
19 | 39 | } |
20 | 40 |
|
| 41 | + // let command = BankAccountCommand::Debit { amount: 1 }; |
| 42 | + // bank_query.execute(connection, command) |
| 43 | + |
| 44 | + // Debit Transaction |
| 45 | + // |
| 46 | + // need locks to ensure balance isn't screwed up |
| 47 | + // let lock = queue.lock.await; |
| 48 | + // pg.persist(transaction_debit); |
| 49 | + |
| 50 | + // Credit Transaction |
| 51 | + // |
| 52 | + // don't need locks to insert one more into a list |
| 53 | + // pg.persist(transaction_debit); |
| 54 | + |
21 | 55 | todo!() |
22 | 56 | } |
| 57 | + |
| 58 | +struct BankAccountService { |
| 59 | + redis: Arc<Client>, |
| 60 | + // users_lock: FnvHashSet<i32> |
| 61 | +} |
| 62 | + |
| 63 | +enum AccountCommands { |
| 64 | + DepositMoney { amount: i64 }, |
| 65 | + WithdrawMoney { user: i32, amount: i64 }, |
| 66 | +} |
| 67 | + |
| 68 | +enum AccountQueries { |
| 69 | + Statement, |
| 70 | +} |
| 71 | + |
| 72 | +impl BankAccountService { |
| 73 | + async fn handler(&self, command: AccountCommands) { |
| 74 | + match command { |
| 75 | + AccountCommands::DepositMoney { .. } => {} |
| 76 | + AccountCommands::WithdrawMoney { user, amount } => { |
| 77 | + let lock = RedisLock::new(self.redis.clone(), user, 300); |
| 78 | + let _ = lock.acquire(); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments