mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-19 22:41:13 +00:00
refactor: massive refactoring of query and mutation
This commit is contained in:
parent
89c66958bc
commit
493e3a3077
8 changed files with 111 additions and 67 deletions
2
core/src/database/mod.rs
Normal file
2
core/src/database/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod mutation;
|
||||
pub mod query;
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
use crate::Mutation;
|
||||
use std::vec;
|
||||
|
||||
use chrono::{Utc, Duration};
|
||||
use ::entity::{candidate, session};
|
||||
use sea_orm::{*, prelude::Uuid};
|
||||
use crate::crypto::{hash_password, self};
|
||||
|
||||
pub struct Mutation;
|
||||
use ::entity::candidate;
|
||||
use sea_orm::{*};
|
||||
use crate::{crypto::{hash_password, self}};
|
||||
|
||||
impl Mutation {
|
||||
pub async fn create_candidate(
|
||||
|
|
@ -37,36 +35,6 @@ impl Mutation {
|
|||
.insert(db)
|
||||
.await
|
||||
}
|
||||
|
||||
|
||||
pub async fn insert_session(
|
||||
db: &DbConn,
|
||||
user_id: i32,
|
||||
random_uuid: Uuid,
|
||||
ip_addr: String,
|
||||
) -> Result<session::Model, DbErr> {
|
||||
session::ActiveModel {
|
||||
id: Set(random_uuid),
|
||||
user_id: Set(user_id),
|
||||
ip_address: Set(ip_addr),
|
||||
created_at: Set(Utc::now().naive_local()),
|
||||
expires_at: Set(Utc::now().naive_local().checked_add_signed(Duration::days(1)).unwrap()),
|
||||
}
|
||||
.insert(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_session(
|
||||
db: &DbConn,
|
||||
session_id: Uuid
|
||||
) -> Result<DeleteResult, DbErr> {
|
||||
session::ActiveModel {
|
||||
id: Set(session_id),
|
||||
..Default::default()
|
||||
}
|
||||
.delete(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
4
core/src/database/mutation/mod.rs
Normal file
4
core/src/database/mutation/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub struct Mutation;
|
||||
|
||||
pub mod session;
|
||||
pub mod candidate;
|
||||
37
core/src/database/mutation/session.rs
Normal file
37
core/src/database/mutation/session.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use chrono::{Utc, Duration};
|
||||
use ::entity::session;
|
||||
use sea_orm::{*, prelude::Uuid};
|
||||
|
||||
use crate::Mutation;
|
||||
|
||||
|
||||
impl Mutation {
|
||||
pub async fn insert_session(
|
||||
db: &DbConn,
|
||||
user_id: i32,
|
||||
random_uuid: Uuid,
|
||||
ip_addr: String,
|
||||
) -> Result<session::Model, DbErr> {
|
||||
session::ActiveModel {
|
||||
id: Set(random_uuid),
|
||||
user_id: Set(user_id),
|
||||
ip_address: Set(ip_addr),
|
||||
created_at: Set(Utc::now().naive_local()),
|
||||
expires_at: Set(Utc::now()
|
||||
.naive_local()
|
||||
.checked_add_signed(Duration::days(1))
|
||||
.unwrap()),
|
||||
}
|
||||
.insert(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_session(db: &DbConn, session_id: Uuid) -> Result<DeleteResult, DbErr> {
|
||||
session::ActiveModel {
|
||||
id: Set(session_id),
|
||||
..Default::default()
|
||||
}
|
||||
.delete(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
|
@ -1,44 +1,35 @@
|
|||
use ::entity::{candidate, candidate::Entity as Candidate};
|
||||
use ::entity::{session, session::Entity as Session};
|
||||
use sea_orm::*;
|
||||
use sea_orm::prelude::Uuid;
|
||||
use crate::Query;
|
||||
|
||||
pub struct Query;
|
||||
use ::entity::{candidate, candidate::Entity as Candidate};
|
||||
use sea_orm::*;
|
||||
|
||||
impl Query {
|
||||
pub async fn find_candidate_by_id(db: &DbConn, id: i32) -> Result<Option<candidate::Model>, DbErr> {
|
||||
pub async fn find_candidate_by_id(
|
||||
db: &DbConn,
|
||||
id: i32,
|
||||
) -> Result<Option<candidate::Model>, DbErr> {
|
||||
Candidate::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn find_session_by_uuid(db: &DbConn, uuid: Uuid) -> Result<Option<session::Model>, DbErr> {
|
||||
Session::find_by_id(uuid).one(db).await
|
||||
}
|
||||
|
||||
// find session by user id
|
||||
pub async fn find_sessions_by_user_id(db: &DbConn, user_id: i32) -> Result<Vec<session::Model>, DbErr> {
|
||||
Session::find()
|
||||
.filter(session::Column::UserId.eq(user_id))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use sea_orm::{DbConn, Set, ActiveModelTrait};
|
||||
use entity::candidate;
|
||||
use sea_orm::{Schema, Database, DbBackend, sea_query::TableCreateStatement, ConnectionTrait};
|
||||
use sea_orm::{sea_query::TableCreateStatement, ConnectionTrait, Database, DbBackend, Schema};
|
||||
use sea_orm::{ActiveModelTrait, DbConn, Set};
|
||||
|
||||
use crate::Query;
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
async fn get_memory_sqlite_connection() -> DbConn {
|
||||
let base_url = "sqlite::memory:";
|
||||
let db: DbConn = Database::connect(base_url).await.unwrap();
|
||||
|
||||
|
||||
let schema = Schema::new(DbBackend::Sqlite);
|
||||
let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity);
|
||||
db.execute(db.get_database_backend().build(&stmt)).await.unwrap();
|
||||
db.execute(db.get_database_backend().build(&stmt))
|
||||
.await
|
||||
.unwrap();
|
||||
db
|
||||
}
|
||||
|
||||
|
|
@ -55,11 +46,13 @@ mod tests {
|
|||
updated_at: Set(chrono::offset::Local::now().naive_local()),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&db)
|
||||
.insert(&db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let candidate = Query::find_candidate_by_id(&db, candidate.application)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let candidate = Query::find_candidate_by_id(&db, candidate.application).await.unwrap();
|
||||
assert!(candidate.is_some());
|
||||
}
|
||||
}
|
||||
}
|
||||
4
core/src/database/query/mod.rs
Normal file
4
core/src/database/query/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub struct Query;
|
||||
|
||||
pub mod candidate;
|
||||
pub mod session;
|
||||
37
core/src/database/query/session.rs
Normal file
37
core/src/database/query/session.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use crate::Query;
|
||||
|
||||
use ::entity::{session, session::Entity as Session};
|
||||
use sea_orm::*;
|
||||
use sea_orm::prelude::Uuid;
|
||||
|
||||
impl Query {
|
||||
pub async fn find_session_by_uuid(db: &DbConn, uuid: Uuid) -> Result<Option<session::Model>, DbErr> {
|
||||
Session::find_by_id(uuid).one(db).await
|
||||
}
|
||||
|
||||
// find session by user id
|
||||
pub async fn find_sessions_by_user_id(db: &DbConn, user_id: i32) -> Result<Vec<session::Model>, DbErr> {
|
||||
Session::find()
|
||||
.filter(session::Column::UserId.eq(user_id))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use sea_orm::DbConn;
|
||||
use entity::candidate;
|
||||
use sea_orm::{Schema, Database, DbBackend, sea_query::TableCreateStatement, ConnectionTrait};
|
||||
|
||||
#[cfg(test)]
|
||||
async fn get_memory_sqlite_connection() -> DbConn {
|
||||
let base_url = "sqlite::memory:";
|
||||
let db: DbConn = Database::connect(base_url).await.unwrap();
|
||||
|
||||
let schema = Schema::new(DbBackend::Sqlite);
|
||||
let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity);
|
||||
db.execute(db.get_database_backend().build(&stmt)).await.unwrap();
|
||||
db
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,10 @@
|
|||
mod mutation;
|
||||
mod query;
|
||||
pub mod database;
|
||||
pub mod crypto;
|
||||
pub mod filetype;
|
||||
pub mod services;
|
||||
pub mod error;
|
||||
|
||||
pub use mutation::*;
|
||||
pub use query::*;
|
||||
pub use database::mutation::*;
|
||||
pub use database::query::*;
|
||||
|
||||
pub use sea_orm;
|
||||
|
|
|
|||
Loading…
Reference in a new issue