From 1566363e8fda477c8feb717a0a25582bfc500915 Mon Sep 17 00:00:00 2001 From: Ninjdai Date: Wed, 20 Aug 2025 09:39:35 +0200 Subject: [PATCH] refactor: remove where in favor of for generics --- src/entities/bal.rs | 7 ++----- src/entities/book_instance.rs | 4 +--- src/entities/owner.rs | 4 +--- src/lib.rs | 3 +-- src/utils/auth.rs | 18 ++++++------------ tests/common/mod.rs | 3 +-- 6 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/entities/bal.rs b/src/entities/bal.rs index dd5f8e5..132871e 100644 --- a/src/entities/bal.rs +++ b/src/entities/bal.rs @@ -25,8 +25,7 @@ pub enum BalState { Ended } -pub async fn get_user_ongoing_bal(db_conn: &C, user_id: u32) -> Option -where C: ConnectionTrait { +pub async fn get_user_ongoing_bal(db_conn: &C, user_id: u32) -> Option { Entity::find().filter(Column::UserId.eq(user_id)).filter(Column::State.eq(BalState::Ongoing)).one(db_conn).await.unwrap_or(None) } @@ -45,9 +44,7 @@ impl Related for Entity { } impl Entity { - pub async fn get_by_id(db_conn: &C, id: u32) -> Option - where C: ConnectionTrait, - { + pub async fn get_by_id(db_conn: &C, id: u32) -> Option { match Self::find_by_id(id).one(db_conn).await { Ok(res) => res, Err(_) => None diff --git a/src/entities/book_instance.rs b/src/entities/book_instance.rs index 64f7a78..a86de2c 100644 --- a/src/entities/book_instance.rs +++ b/src/entities/book_instance.rs @@ -16,9 +16,7 @@ pub struct Model { } impl Entity { - pub async fn get_by_id(db_conn: &C, id: u32) -> Option - where C: ConnectionTrait, - { + pub async fn get_by_id(db_conn: &C, id: u32) -> Option { match Self::find_by_id(id).one(db_conn).await { Ok(res) => res, Err(_) => None diff --git a/src/entities/owner.rs b/src/entities/owner.rs index ec8300f..d7e0a7e 100644 --- a/src/entities/owner.rs +++ b/src/entities/owner.rs @@ -15,9 +15,7 @@ pub struct Model { } impl Entity { - pub async fn get_by_id(db_conn: &C, id: u32) -> Option - where C: ConnectionTrait, - { + pub async fn get_by_id(db_conn: &C, id: u32) -> Option { match Self::find_by_id(id).one(db_conn).await { Ok(res) => res, Err(_) => None diff --git a/src/lib.rs b/src/lib.rs index 287fdb7..4f014c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,8 +79,7 @@ pub static CLI: LazyLock = LazyLock::new(|| { Cli::parse() }); -pub async fn create_tables(db_conn: &C) -> Result<(), DbErr> - where C: ConnectionTrait,{ +pub async fn create_tables(db_conn: &C) -> Result<(), DbErr> { let builder = db_conn.get_database_backend(); let schema = Schema::new(builder); if let Err(err) = db_conn.execute(builder.build(schema.create_table_from_entity(crate::entities::prelude::Book).if_not_exists())).await { diff --git a/src/utils/auth.rs b/src/utils/auth.rs index 5e44b3c..1ffbf00 100644 --- a/src/utils/auth.rs +++ b/src/utils/auth.rs @@ -4,39 +4,33 @@ use sea_orm::ConnectionTrait; use crate::entities::prelude::*; -pub async fn user_is_bal_owner( +pub async fn user_is_bal_owner( user_id: u32, bal_id: u32, db_conn: &C -) -> bool -where C: ConnectionTrait, -{ +) -> bool { match Bal::get_by_id(db_conn, bal_id).await { Some(bal) => user_id == bal.user_id, None => false } } -pub async fn user_is_book_instance_owner( +pub async fn user_is_book_instance_owner( user_id: u32, book_instance_id: u32, db_conn: &C -) -> bool -where C: ConnectionTrait, -{ +) -> bool { match BookInstance::get_by_id(db_conn, book_instance_id).await { Some(instance) => user_is_bal_owner(user_id, instance.bal_id, db_conn).await, None => false } } -pub async fn user_is_owner_owner( +pub async fn user_is_owner_owner( user_id: u32, owner_id: u32, db_conn: &C -) -> bool -where C: ConnectionTrait, -{ +) -> bool { match Owner::get_by_id(db_conn, owner_id).await { Some(owner) => owner.user_id == user_id, None => false diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 184727e..69f2ef3 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -56,8 +56,7 @@ fn free_local_ipv4_port() -> Option { .ok() } -async fn create_user(db_conn: &C, username_t: impl ToString, password_t: impl ToString) -where C: ConnectionTrait { +async fn create_user(db_conn: &C, username_t: impl ToString, password_t: impl ToString) { let username = username_t.to_string(); if User::find().filter(user::Column::Username.eq(username.clone())).one(db_conn).await.is_ok_and(|r| r.is_some()) { panic!("Username {username} already in use");