-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Leonid Kozarin
committed
Feb 21, 2024
1 parent
dfedef3
commit 0f0c3f1
Showing
5 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
.sqlx/query-716d3c846ae1c0dcf53c8035b7c987e5d579cca262fa37afcd8b5b7fa0dea0bf.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
.sqlx/query-ec8e198184da36681b997c8a7d59ec350784e471158846cf152e6aa2589389dc.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE IF NOT EXISTS Loans ( | ||
id serial PRIMARY KEY, | ||
uid bigint NOT NULL REFERENCES Users(uid), | ||
chat_id bigint NOT NULL REFERENCES Chats(id), | ||
left_to_pay int NOT NULL CHECK ( left_to_pay >= 0 ), | ||
created_at date NOT NULL DEFAULT current_date, | ||
repaid_at date | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS idx_loans_uid ON Loans(uid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use teloxide::types::UserId; | ||
use tokio::sync::RwLock; | ||
use crate::config::FeatureToggles; | ||
use crate::repo; | ||
use crate::repo::{ChatIdKind, ChatIdPartiality}; | ||
|
||
#[derive(Clone)] | ||
pub struct Loans { | ||
pool: sqlx::Pool<sqlx::Postgres>, | ||
features: FeatureToggles, | ||
chat_id_cache: Arc<RwLock<HashMap<ChatIdKind, i64>>>, | ||
chats_repo: repo::Chats, | ||
} | ||
|
||
impl Loans { | ||
pub fn new(pool: sqlx::Pool<sqlx::Postgres>, features: FeatureToggles) -> Self { | ||
Self { | ||
chats_repo: repo::Chats::new(pool.clone(), features), | ||
pool, | ||
features, | ||
chat_id_cache: Arc::new(RwLock::new(HashMap::new())), | ||
} | ||
} | ||
|
||
pub async fn get_active_loan(&self, uid: UserId, chat_id: ChatIdPartiality) -> anyhow::Result<u32> { | ||
let internal_chat_id = match self.get_internal_chat_id(chat_id.kind()).await? { | ||
Some(id) => id, | ||
None => return Ok(Default::default()) | ||
}; | ||
sqlx::query_scalar!("SELECT left_to_pay FROM loans WHERE uid = $1 AND chat_id = $2 AND repaid_at IS NULL", | ||
uid.0 as i64, internal_chat_id) | ||
.fetch_optional(&self.pool) | ||
.await | ||
.map(|maybe_loan| maybe_loan.map(|value| value as u32).unwrap_or_default()) | ||
.map_err(|e| e.into()) | ||
} | ||
|
||
pub async fn pay(&self, uid: UserId, chat_id: ChatIdPartiality, value: u32) -> anyhow::Result<()> { | ||
let internal_chat_id = match self.get_internal_chat_id(chat_id.kind()).await? { | ||
Some(id) => id, | ||
None => return Ok(()) // TODO: check or logging? | ||
}; | ||
sqlx::query!("UPDATE Loans SET left_to_pay = left_to_pay - $3 WHERE uid = $1 AND chat_id = $2 AND repaid_at IS NULL", | ||
uid.0 as i64, internal_chat_id, value as i64) | ||
.execute(&self.pool) | ||
.await | ||
.map(|_| ()) | ||
.map_err(|e| e.into()) | ||
} | ||
|
||
async fn get_internal_chat_id(&self, chat_id: ChatIdKind) -> anyhow::Result<Option<i64>> { | ||
let maybe_internal_id = self.chat_id_cache | ||
.read().await | ||
.get(&chat_id).copied(); | ||
let internal_id = match maybe_internal_id { | ||
None => { | ||
let maybe_id = self.chats_repo.get_chat(chat_id.clone()) | ||
.await? | ||
.map(|chat| chat.internal_id); | ||
if let Some(id) = maybe_id { | ||
self.chat_id_cache | ||
.write().await | ||
.insert(chat_id, id); | ||
} | ||
maybe_id | ||
} | ||
Some(id) => Some(id) | ||
}; | ||
Ok(internal_id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters