-
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
Jul 1, 2024
1 parent
88dac4c
commit 5d896df
Showing
14 changed files
with
197 additions
and
12 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
.sqlx/query-0a395aec4d06c00629e6101b9873985ff060522ba62503133edb89725768c111.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
14 changes: 10 additions & 4 deletions
14
...288d681616d65983efb2e56e6f29d1e6a9c5.json → ...b06d91e9462b2d9ec9c7133a7755c996e8f7.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
.sqlx/query-9da9a4e1e69cf4b0c0b67b4ddb1ea0e30230b774b7bda7cbddc4cde463750e14.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
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
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
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
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
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
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
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,42 @@ | ||
use num_traits::ToPrimitive; | ||
use sqlx::FromRow; | ||
use teloxide::types::UserId; | ||
use crate::repository; | ||
|
||
#[derive(FromRow)] | ||
struct PersonalStatsEntity { | ||
chats: Option<i64>, | ||
max_length: Option<i32>, | ||
total_length: Option<i64>, | ||
} | ||
|
||
pub struct PersonalStats { | ||
pub chats: u64, | ||
pub max_length: u32, | ||
pub total_length: u64, | ||
} | ||
|
||
impl From<PersonalStatsEntity> for PersonalStats { | ||
fn from(value: PersonalStatsEntity) -> Self { | ||
Self { | ||
chats: value.chats.map(|x| x.to_u64().expect("chats count, fetched from the database, must fit into u64")).unwrap_or_default(), | ||
max_length: value.max_length.map(|x| x.to_u32().expect("max_length, fetched from the database, must fit into u32")).unwrap_or_default(), | ||
total_length: value.total_length.map(|x| x.to_u64().expect("total_length, fetched from the database, must fit into u64")).unwrap_or_default(), | ||
} | ||
} | ||
} | ||
|
||
repository!(PersonalStatsRepo, | ||
pub async fn get(&self, user_id: UserId) -> anyhow::Result<PersonalStats> { | ||
sqlx::query_as!(PersonalStatsEntity, | ||
r#"SELECT count(chat_id) AS chats, | ||
max(length) AS max_length, | ||
sum(length) AS total_length | ||
FROM Dicks WHERE uid = $1"#, | ||
user_id.0 as i64) | ||
.fetch_one(&self.pool) | ||
.await | ||
.map(PersonalStats::from) | ||
.map_err(Into::into) | ||
} | ||
); |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ mod import; | |
mod promo; | ||
mod loans; | ||
mod pvpstats; | ||
mod stats; | ||
|
||
use std::str::FromStr; | ||
use reqwest::Url; | ||
|
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
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,36 @@ | ||
use teloxide::prelude::{ChatId, UserId}; | ||
use testcontainers::clients; | ||
use crate::repo; | ||
use crate::repo::{ChatIdKind, ChatIdPartiality}; | ||
use crate::repo::test::{CHAT_ID, start_postgres, UID}; | ||
use crate::repo::test::dicks::create_user; | ||
|
||
#[tokio::test] | ||
async fn test_all() { | ||
let docker = clients::Cli::default(); | ||
let (_container, db) = start_postgres(&docker).await; | ||
let personal_stats = repo::PersonalStatsRepo::new(db.clone()); | ||
let dicks = repo::Dicks::new(db.clone(), Default::default()); | ||
|
||
let chat_id_1 = ChatIdKind::ID(ChatId(CHAT_ID)); | ||
let chat_id_2 = ChatIdKind::ID(ChatId(CHAT_ID + 1)); | ||
let uid = UserId(UID as u64); | ||
create_user(&db).await; | ||
|
||
let stats = personal_stats.get(uid).await | ||
.expect("couldn't fetch the empty stats"); | ||
assert_eq!(stats.chats, 0); | ||
assert_eq!(stats.max_length, 0); | ||
assert_eq!(stats.total_length, 0); | ||
|
||
dicks.create_or_grow(uid, &ChatIdPartiality::Specific(chat_id_1), 10).await | ||
.expect("couldn't grow the dick in the first chat"); | ||
dicks.create_or_grow(uid, &ChatIdPartiality::Specific(chat_id_2), 20).await | ||
.expect("couldn't grow the dick in the second chat"); | ||
|
||
let stats = personal_stats.get(uid).await | ||
.expect("couldn't fetch the non-null stats"); | ||
assert_eq!(stats.chats, 2); | ||
assert_eq!(stats.max_length, 20); | ||
assert_eq!(stats.total_length, 30); | ||
} |