refactor: remove where in favor of <T: > for generics
All checks were successful
/ test (push) Successful in 3m22s

This commit is contained in:
Ninjdai 2025-08-20 09:39:35 +02:00
parent 44ba33035f
commit 1566363e8f
6 changed files with 12 additions and 27 deletions

View file

@ -25,8 +25,7 @@ pub enum BalState {
Ended
}
pub async fn get_user_ongoing_bal<C>(db_conn: &C, user_id: u32) -> Option<Model>
where C: ConnectionTrait {
pub async fn get_user_ongoing_bal<C: ConnectionTrait>(db_conn: &C, user_id: u32) -> Option<Model> {
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<super::book_instance::Entity> for Entity {
}
impl Entity {
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
where C: ConnectionTrait,
{
pub async fn get_by_id<C: ConnectionTrait>(db_conn: &C, id: u32) -> Option<Model> {
match Self::find_by_id(id).one(db_conn).await {
Ok(res) => res,
Err(_) => None

View file

@ -16,9 +16,7 @@ pub struct Model {
}
impl Entity {
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
where C: ConnectionTrait,
{
pub async fn get_by_id<C: ConnectionTrait>(db_conn: &C, id: u32) -> Option<Model> {
match Self::find_by_id(id).one(db_conn).await {
Ok(res) => res,
Err(_) => None

View file

@ -15,9 +15,7 @@ pub struct Model {
}
impl Entity {
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
where C: ConnectionTrait,
{
pub async fn get_by_id<C: ConnectionTrait>(db_conn: &C, id: u32) -> Option<Model> {
match Self::find_by_id(id).one(db_conn).await {
Ok(res) => res,
Err(_) => None

View file

@ -79,8 +79,7 @@ pub static CLI: LazyLock<Cli> = LazyLock::new(|| {
Cli::parse()
});
pub async fn create_tables<C>(db_conn: &C) -> Result<(), DbErr>
where C: ConnectionTrait,{
pub async fn create_tables<C: ConnectionTrait>(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 {

View file

@ -4,39 +4,33 @@ use sea_orm::ConnectionTrait;
use crate::entities::prelude::*;
pub async fn user_is_bal_owner<C>(
pub async fn user_is_bal_owner<C: ConnectionTrait>(
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<C>(
pub async fn user_is_book_instance_owner<C: ConnectionTrait>(
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<C>(
pub async fn user_is_owner_owner<C: ConnectionTrait>(
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

View file

@ -56,8 +56,7 @@ fn free_local_ipv4_port() -> Option<u16> {
.ok()
}
async fn create_user<C>(db_conn: &C, username_t: impl ToString, password_t: impl ToString)
where C: ConnectionTrait {
async fn create_user<C: ConnectionTrait>(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");