add: change default avatar

This commit is contained in:
trisua 2025-07-14 22:05:59 -04:00
parent 8dfd307919
commit 959a125992
6 changed files with 92 additions and 13 deletions

View file

@ -75,6 +75,26 @@ impl DataManager {
Ok(res.unwrap())
}
pub async fn get_table_row_count_where(&self, table: &str, r#where: &str) -> Result<i32> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(
&conn,
&format!("SELECT COUNT(*)::int FROM {} {}", table, r#where),
params![],
|x| Ok(x.get::<usize, i32>(0))
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
Ok(res.unwrap())
}
}
#[macro_export]

View file

@ -30,7 +30,38 @@ impl DataManager {
///
/// # Arguments
/// * `id` - the ID of the user to fetch products for
pub async fn get_products_by_user(&self, id: usize) -> Result<Vec<Product>> {
/// * `batch`
/// * `page`
pub async fn get_products_by_user(
&self,
id: usize,
batch: usize,
page: usize,
) -> Result<Vec<Product>> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM products WHERE owner = $1 ORDER BY created DESC LIMIT {} OFFSET {}",
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_product_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("product".to_string()));
}
Ok(res.unwrap())
}
/// Get all products by user.
///
/// # Arguments
/// * `id` - the ID of the user to fetch products for
pub async fn get_products_by_user_all(&self, id: usize) -> Result<Vec<Product>> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
@ -68,9 +99,11 @@ impl DataManager {
let owner = self.get_user_by_id(data.owner).await?;
if !owner.permissions.check(FinePermission::SUPPORTER) {
let products = self.get_products_by_user(data.owner).await?;
let products = self
.get_table_row_count_where("products", &format!("owner = {}", owner.id))
.await? as usize;
if products.len() >= Self::MAXIMUM_FREE_PRODUCTS {
if products >= Self::MAXIMUM_FREE_PRODUCTS {
return Err(Error::MiscError(
"You already have the maximum number of products you can have".to_string(),
));

View file

@ -165,9 +165,11 @@ impl DataManager {
let owner = self.get_user_by_id(data.owner).await?;
if !owner.permissions.check(FinePermission::SUPPORTER) {
let stacks = self.get_stacks_by_user(data.owner).await?;
let stacks = self
.get_table_row_count_where("stacks", &format!("owner = {}", owner.id))
.await? as usize;
if stacks.len() >= Self::MAXIMUM_FREE_STACKS {
if stacks >= Self::MAXIMUM_FREE_STACKS {
return Err(Error::MiscError(
"You already have the maximum number of stacks you can have".to_string(),
));