Skip to content

Commit 5de97f4

Browse files
committed
feat: more work on api, errors
Signed-off-by: Martin <martin@hotmail.com.br>
1 parent 677486e commit 5de97f4

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

src/api.rs

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,82 @@
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};
37
use eyre::bail;
4-
use crate::server_impl::request::Request;
8+
use redis::Client;
9+
use std::sync::Arc;
10+
use time::OffsetDateTime;
511

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();
818

919
if req.method != Method::GET {
1020
bail!("Only GET available.")
1121
}
1222

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)
1531
}
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> {
1737
if req.method != Method::POST {
1838
bail!("Only POST available.")
1939
}
2040

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+
2155
todo!()
2256
}
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+
}

src/domain/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Domain Errors
22
3-
#[derive(Debug)]
3+
#[derive(Debug, Copy, Clone)]
44
pub enum TransactionError {
55
InvalidDescription,
66
}
77

8-
#[derive(Debug)]
8+
#[derive(Debug, Copy, Clone)]
99
pub enum AccountError {
1010
InsufficientCredit,
1111
}

0 commit comments

Comments
 (0)