refactor: remove where in favor of <T: > for generics
All checks were successful
/ test (push) Successful in 3m22s
All checks were successful
/ test (push) Successful in 3m22s
This commit is contained in:
parent
44ba33035f
commit
1566363e8f
6 changed files with 12 additions and 27 deletions
|
|
@ -25,8 +25,7 @@ pub enum BalState {
|
||||||
Ended
|
Ended
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_user_ongoing_bal<C>(db_conn: &C, user_id: u32) -> Option<Model>
|
pub async fn get_user_ongoing_bal<C: ConnectionTrait>(db_conn: &C, user_id: u32) -> Option<Model> {
|
||||||
where C: ConnectionTrait {
|
|
||||||
Entity::find().filter(Column::UserId.eq(user_id)).filter(Column::State.eq(BalState::Ongoing)).one(db_conn).await.unwrap_or(None)
|
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 {
|
impl Entity {
|
||||||
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
|
pub async fn get_by_id<C: ConnectionTrait>(db_conn: &C, id: u32) -> Option<Model> {
|
||||||
where C: ConnectionTrait,
|
|
||||||
{
|
|
||||||
match Self::find_by_id(id).one(db_conn).await {
|
match Self::find_by_id(id).one(db_conn).await {
|
||||||
Ok(res) => res,
|
Ok(res) => res,
|
||||||
Err(_) => None
|
Err(_) => None
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,7 @@ pub struct Model {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Entity {
|
impl Entity {
|
||||||
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
|
pub async fn get_by_id<C: ConnectionTrait>(db_conn: &C, id: u32) -> Option<Model> {
|
||||||
where C: ConnectionTrait,
|
|
||||||
{
|
|
||||||
match Self::find_by_id(id).one(db_conn).await {
|
match Self::find_by_id(id).one(db_conn).await {
|
||||||
Ok(res) => res,
|
Ok(res) => res,
|
||||||
Err(_) => None
|
Err(_) => None
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,7 @@ pub struct Model {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Entity {
|
impl Entity {
|
||||||
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
|
pub async fn get_by_id<C: ConnectionTrait>(db_conn: &C, id: u32) -> Option<Model> {
|
||||||
where C: ConnectionTrait,
|
|
||||||
{
|
|
||||||
match Self::find_by_id(id).one(db_conn).await {
|
match Self::find_by_id(id).one(db_conn).await {
|
||||||
Ok(res) => res,
|
Ok(res) => res,
|
||||||
Err(_) => None
|
Err(_) => None
|
||||||
|
|
|
||||||
|
|
@ -79,8 +79,7 @@ pub static CLI: LazyLock<Cli> = LazyLock::new(|| {
|
||||||
Cli::parse()
|
Cli::parse()
|
||||||
});
|
});
|
||||||
|
|
||||||
pub async fn create_tables<C>(db_conn: &C) -> Result<(), DbErr>
|
pub async fn create_tables<C: ConnectionTrait>(db_conn: &C) -> Result<(), DbErr> {
|
||||||
where C: ConnectionTrait,{
|
|
||||||
let builder = db_conn.get_database_backend();
|
let builder = db_conn.get_database_backend();
|
||||||
let schema = Schema::new(builder);
|
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 {
|
if let Err(err) = db_conn.execute(builder.build(schema.create_table_from_entity(crate::entities::prelude::Book).if_not_exists())).await {
|
||||||
|
|
|
||||||
|
|
@ -4,39 +4,33 @@ use sea_orm::ConnectionTrait;
|
||||||
|
|
||||||
use crate::entities::prelude::*;
|
use crate::entities::prelude::*;
|
||||||
|
|
||||||
pub async fn user_is_bal_owner<C>(
|
pub async fn user_is_bal_owner<C: ConnectionTrait>(
|
||||||
user_id: u32,
|
user_id: u32,
|
||||||
bal_id: u32,
|
bal_id: u32,
|
||||||
db_conn: &C
|
db_conn: &C
|
||||||
) -> bool
|
) -> bool {
|
||||||
where C: ConnectionTrait,
|
|
||||||
{
|
|
||||||
match Bal::get_by_id(db_conn, bal_id).await {
|
match Bal::get_by_id(db_conn, bal_id).await {
|
||||||
Some(bal) => user_id == bal.user_id,
|
Some(bal) => user_id == bal.user_id,
|
||||||
None => false
|
None => false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn user_is_book_instance_owner<C>(
|
pub async fn user_is_book_instance_owner<C: ConnectionTrait>(
|
||||||
user_id: u32,
|
user_id: u32,
|
||||||
book_instance_id: u32,
|
book_instance_id: u32,
|
||||||
db_conn: &C
|
db_conn: &C
|
||||||
) -> bool
|
) -> bool {
|
||||||
where C: ConnectionTrait,
|
|
||||||
{
|
|
||||||
match BookInstance::get_by_id(db_conn, book_instance_id).await {
|
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,
|
Some(instance) => user_is_bal_owner(user_id, instance.bal_id, db_conn).await,
|
||||||
None => false
|
None => false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn user_is_owner_owner<C>(
|
pub async fn user_is_owner_owner<C: ConnectionTrait>(
|
||||||
user_id: u32,
|
user_id: u32,
|
||||||
owner_id: u32,
|
owner_id: u32,
|
||||||
db_conn: &C
|
db_conn: &C
|
||||||
) -> bool
|
) -> bool {
|
||||||
where C: ConnectionTrait,
|
|
||||||
{
|
|
||||||
match Owner::get_by_id(db_conn, owner_id).await {
|
match Owner::get_by_id(db_conn, owner_id).await {
|
||||||
Some(owner) => owner.user_id == user_id,
|
Some(owner) => owner.user_id == user_id,
|
||||||
None => false
|
None => false
|
||||||
|
|
|
||||||
|
|
@ -56,8 +56,7 @@ fn free_local_ipv4_port() -> Option<u16> {
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_user<C>(db_conn: &C, username_t: impl ToString, password_t: impl ToString)
|
async fn create_user<C: ConnectionTrait>(db_conn: &C, username_t: impl ToString, password_t: impl ToString) {
|
||||||
where C: ConnectionTrait {
|
|
||||||
let username = username_t.to_string();
|
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()) {
|
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");
|
panic!("Username {username} already in use");
|
||||||
|
|
|
||||||
Reference in a new issue