mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-06 11:30:44 +00:00
feat: code cleanup
This commit is contained in:
parent
da538a50b3
commit
a96c0ce9f7
3 changed files with 19 additions and 24 deletions
|
|
@ -22,11 +22,17 @@ impl Query {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn find_related_candidate_sessions(db: &DbConn, candidate: candidate::Model) -> Result<Vec<session::Model>, DbErr> {
|
pub async fn find_related_candidate_sessions(db: &DbConn, candidate: candidate::Model) -> Result<Vec<session::Model>, DbErr> {
|
||||||
candidate.find_related(Session).all(db).await
|
candidate.find_related(Session)
|
||||||
|
.order_by_asc(session::Column::UpdatedAt)
|
||||||
|
.all(db)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn find_related_admin_sessions(db: &DbConn, admin: admin::Model) -> Result<Vec<admin_session::Model>, DbErr> {
|
pub async fn find_related_admin_sessions(db: &DbConn, admin: admin::Model) -> Result<Vec<admin_session::Model>, DbErr> {
|
||||||
admin.find_related(admin_session::Entity).all(db).await
|
admin.find_related(admin_session::Entity)
|
||||||
|
.order_by_asc(admin_session::Column::UpdatedAt)
|
||||||
|
.all(db)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
// find session by user id
|
// find session by user id
|
||||||
|
|
@ -50,7 +56,6 @@ impl Query {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use entity::{session, admin, candidate, admin_session};
|
use entity::{session, admin, candidate, admin_session};
|
||||||
use sea_orm::ActiveValue::NotSet;
|
|
||||||
use sea_orm::{prelude::Uuid, ActiveModelTrait, Set};
|
use sea_orm::{prelude::Uuid, ActiveModelTrait, Set};
|
||||||
|
|
||||||
use crate::utils::db::get_memory_sqlite_connection;
|
use crate::utils::db::get_memory_sqlite_connection;
|
||||||
|
|
|
||||||
|
|
@ -82,8 +82,7 @@ impl AuthenticableTrait for AdminService {
|
||||||
|
|
||||||
let session = Mutation::insert_admin_session(db, admin.id, random_uuid, ip_addr).await?;
|
let session = Mutation::insert_admin_session(db, admin.id, random_uuid, ip_addr).await?;
|
||||||
|
|
||||||
Self::delete_old_sessions(db, admin, 1)
|
Self::delete_old_sessions(db, admin, 1).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(session.id.to_string())
|
Ok(session.id.to_string())
|
||||||
}
|
}
|
||||||
|
|
@ -92,14 +91,11 @@ impl AuthenticableTrait for AdminService {
|
||||||
admin: admin::Model,
|
admin: admin::Model,
|
||||||
keep_n_recent: usize,
|
keep_n_recent: usize,
|
||||||
) -> Result<(), ServiceError> {
|
) -> Result<(), ServiceError> {
|
||||||
let mut sessions = Query::find_related_admin_sessions(db, admin)
|
let sessions = Query::find_related_admin_sessions(db, admin)
|
||||||
.await?;
|
.await?
|
||||||
|
.iter()
|
||||||
sessions.sort_by_key(|s| s.created_at);
|
|
||||||
|
|
||||||
let sessions = sessions.iter()
|
|
||||||
.map(|s| s.clone().into_active_model())
|
.map(|s| s.clone().into_active_model())
|
||||||
.collect::<Vec<admin_session::ActiveModel>>();
|
.collect();
|
||||||
|
|
||||||
SessionService::delete_sessions(db, sessions, keep_n_recent).await?;
|
SessionService::delete_sessions(db, sessions, keep_n_recent).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -250,8 +250,7 @@ impl AuthenticableTrait for CandidateService {
|
||||||
.await?
|
.await?
|
||||||
.ok_or(ServiceError::CandidateNotFound)?;
|
.ok_or(ServiceError::CandidateNotFound)?;
|
||||||
|
|
||||||
let session_id = Self::new_session(db, candidate.clone(), password.clone(), ip_addr)
|
let session_id = Self::new_session(db, candidate.clone(), password.clone(), ip_addr).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
let private_key = Self::decrypt_private_key(candidate, password).await?;
|
let private_key = Self::decrypt_private_key(candidate, password).await?;
|
||||||
Ok((session_id, private_key))
|
Ok((session_id, private_key))
|
||||||
|
|
@ -296,9 +295,7 @@ impl AuthenticableTrait for CandidateService {
|
||||||
|
|
||||||
let session = Mutation::insert_candidate_session(db, random_uuid, candidate.application, ip_addr).await?;
|
let session = Mutation::insert_candidate_session(db, random_uuid, candidate.application, ip_addr).await?;
|
||||||
|
|
||||||
Self::delete_old_sessions(db, candidate, 3)
|
Self::delete_old_sessions(db, candidate, 3).await?;
|
||||||
.await
|
|
||||||
.ok();
|
|
||||||
|
|
||||||
Ok(session.id.to_string())
|
Ok(session.id.to_string())
|
||||||
}
|
}
|
||||||
|
|
@ -307,14 +304,11 @@ impl AuthenticableTrait for CandidateService {
|
||||||
candidate: candidate::Model,
|
candidate: candidate::Model,
|
||||||
keep_n_recent: usize,
|
keep_n_recent: usize,
|
||||||
) -> Result<(), ServiceError> {
|
) -> Result<(), ServiceError> {
|
||||||
let mut sessions = Query::find_related_candidate_sessions(db, candidate)
|
let sessions = Query::find_related_candidate_sessions(db, candidate)
|
||||||
.await?;
|
.await?
|
||||||
|
.iter()
|
||||||
sessions.sort_by_key(|s| s.created_at);
|
|
||||||
|
|
||||||
let sessions = sessions.iter()
|
|
||||||
.map(|s| s.clone().into_active_model())
|
.map(|s| s.clone().into_active_model())
|
||||||
.collect::<Vec<session::ActiveModel>>();
|
.collect();
|
||||||
|
|
||||||
SessionService::delete_sessions(db, sessions, keep_n_recent).await?;
|
SessionService::delete_sessions(db, sessions, keep_n_recent).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue