mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-03 10:00:48 +00:00
feat: application sorting
This commit is contained in:
parent
2c6f3a16c4
commit
89e6ca773b
5 changed files with 41 additions and 6 deletions
|
|
@ -120,12 +120,13 @@ pub async fn create_candidate(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
#[get("/candidates?<field>&<page>")]
|
#[get("/candidates?<field>&<page>&<sort>")]
|
||||||
pub async fn list_candidates(
|
pub async fn list_candidates(
|
||||||
conn: Connection<'_, Db>,
|
conn: Connection<'_, Db>,
|
||||||
session: AdminAuth,
|
session: AdminAuth,
|
||||||
field: Option<String>,
|
field: Option<String>,
|
||||||
page: Option<u64>,
|
page: Option<u64>,
|
||||||
|
sort: Option<String>,
|
||||||
) -> Result<Json<Vec<ApplicationResponse>>, Custom<String>> {
|
) -> Result<Json<Vec<ApplicationResponse>>, Custom<String>> {
|
||||||
let db = conn.into_inner();
|
let db = conn.into_inner();
|
||||||
let private_key = session.get_private_key();
|
let private_key = session.get_private_key();
|
||||||
|
|
@ -135,7 +136,7 @@ pub async fn list_candidates(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let candidates = ApplicationService::list_applications(&private_key, db, field, page)
|
let candidates = ApplicationService::list_applications(&private_key, db, field, page, sort)
|
||||||
.await.map_err(to_custom_error)?;
|
.await.map_err(to_custom_error)?;
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use chrono::NaiveDateTime;
|
||||||
use entity::{application, candidate};
|
use entity::{application, candidate};
|
||||||
use sea_orm::{EntityTrait, DbErr, DbConn, ModelTrait, FromQueryResult, QuerySelect, JoinType, RelationTrait, QueryFilter, ColumnTrait, QueryOrder, PaginatorTrait};
|
use sea_orm::{EntityTrait, DbErr, DbConn, ModelTrait, FromQueryResult, QuerySelect, JoinType, RelationTrait, QueryFilter, ColumnTrait, QueryOrder, PaginatorTrait};
|
||||||
|
|
||||||
|
|
@ -13,10 +14,32 @@ pub struct ApplicationCandidateJoin {
|
||||||
pub email: Option<String>,
|
pub email: Option<String>,
|
||||||
pub telephone: Option<String>,
|
pub telephone: Option<String>,
|
||||||
pub field_of_study: Option<String>,
|
pub field_of_study: Option<String>,
|
||||||
|
pub created_at: NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::{Query};
|
use crate::{Query};
|
||||||
|
|
||||||
|
fn get_ordering(sort: String) -> (application::Column, sea_orm::Order)
|
||||||
|
{
|
||||||
|
let mut split = sort.split("_");
|
||||||
|
let column = split.next();
|
||||||
|
let order = split.next();
|
||||||
|
|
||||||
|
let column = match column {
|
||||||
|
Some("id") => application::Column::Id,
|
||||||
|
Some("createdAt") => application::Column::CreatedAt,
|
||||||
|
_ => application::Column::Id
|
||||||
|
};
|
||||||
|
|
||||||
|
let order = match order {
|
||||||
|
Some("asc") => sea_orm::Order::Asc,
|
||||||
|
Some("desc") => sea_orm::Order::Desc,
|
||||||
|
_ => sea_orm::Order::Asc,
|
||||||
|
};
|
||||||
|
|
||||||
|
(column, order)
|
||||||
|
}
|
||||||
|
|
||||||
impl Query {
|
impl Query {
|
||||||
pub async fn find_application_by_id(
|
pub async fn find_application_by_id(
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
|
|
@ -41,14 +64,20 @@ impl Query {
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
field_of_study: Option<String>,
|
field_of_study: Option<String>,
|
||||||
page: Option<u64>,
|
page: Option<u64>,
|
||||||
|
sort: Option<String>,
|
||||||
) -> Result<Vec<ApplicationCandidateJoin>, DbErr> {
|
) -> Result<Vec<ApplicationCandidateJoin>, DbErr> {
|
||||||
let select = application::Entity::find();
|
let select = application::Entity::find();
|
||||||
|
let (column, order) = if let Some(sort) = sort {
|
||||||
|
get_ordering(sort)
|
||||||
|
} else {
|
||||||
|
(application::Column::Id, sea_orm::Order::Asc)
|
||||||
|
};
|
||||||
let query = if let Some(field) = field_of_study {
|
let query = if let Some(field) = field_of_study {
|
||||||
select.filter(application::Column::FieldOfStudy.eq(field))
|
select.filter(application::Column::FieldOfStudy.eq(field))
|
||||||
} else {
|
} else {
|
||||||
select
|
select
|
||||||
}
|
}
|
||||||
.order_by(application::Column::Id, sea_orm::Order::Asc)
|
.order_by(column, order)
|
||||||
.join(JoinType::InnerJoin, application::Relation::Candidate.def())
|
.join(JoinType::InnerJoin, application::Relation::Candidate.def())
|
||||||
.column_as(application::Column::Id, "application_id")
|
.column_as(application::Column::Id, "application_id")
|
||||||
.column_as(candidate::Column::Id, "candidate_id")
|
.column_as(candidate::Column::Id, "candidate_id")
|
||||||
|
|
@ -56,6 +85,7 @@ impl Query {
|
||||||
.column_as(candidate::Column::Surname, "surname")
|
.column_as(candidate::Column::Surname, "surname")
|
||||||
.column_as(candidate::Column::Email, "email")
|
.column_as(candidate::Column::Email, "email")
|
||||||
.column_as(candidate::Column::Telephone, "telephone")
|
.column_as(candidate::Column::Telephone, "telephone")
|
||||||
|
.column_as(candidate::Column::CreatedAt, "created_at")
|
||||||
.into_model::<ApplicationCandidateJoin>();
|
.into_model::<ApplicationCandidateJoin>();
|
||||||
|
|
||||||
if let Some(page) = page {
|
if let Some(page) = page {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use chrono::NaiveDateTime;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
use crate::{database::query::application::ApplicationCandidateJoin, error::ServiceError};
|
use crate::{database::query::application::ApplicationCandidateJoin, error::ServiceError};
|
||||||
|
|
@ -16,6 +17,7 @@ pub struct ApplicationResponse {
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub telephone: String,
|
pub telephone: String,
|
||||||
pub field_of_study: Option<String>,
|
pub field_of_study: Option<String>,
|
||||||
|
pub created_at: NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ApplicationResponse {
|
impl ApplicationResponse {
|
||||||
|
|
@ -40,6 +42,7 @@ impl ApplicationResponse {
|
||||||
email: email.unwrap_or_default(),
|
email: email.unwrap_or_default(),
|
||||||
telephone: telephone.unwrap_or_default(),
|
telephone: telephone.unwrap_or_default(),
|
||||||
field_of_study: c.field_of_study,
|
field_of_study: c.field_of_study,
|
||||||
|
created_at: c.created_at,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -267,8 +267,9 @@ impl ApplicationService {
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
field_of_study: Option<String>,
|
field_of_study: Option<String>,
|
||||||
page: Option<u64>,
|
page: Option<u64>,
|
||||||
|
sort: Option<String>,
|
||||||
) -> Result<Vec<ApplicationResponse>, ServiceError> {
|
) -> Result<Vec<ApplicationResponse>, ServiceError> {
|
||||||
let applications = Query::list_applications(db, field_of_study, page).await?;
|
let applications = Query::list_applications(db, field_of_study, page, sort).await?;
|
||||||
|
|
||||||
futures::future::try_join_all(
|
futures::future::try_join_all(
|
||||||
applications
|
applications
|
||||||
|
|
|
||||||
|
|
@ -79,12 +79,12 @@ pub mod tests {
|
||||||
let db = get_memory_sqlite_connection().await;
|
let db = get_memory_sqlite_connection().await;
|
||||||
let admin = create_admin(&db).await;
|
let admin = create_admin(&db).await;
|
||||||
let private_key = crypto::decrypt_password(admin.private_key, "admin".to_string()).await.unwrap();
|
let private_key = crypto::decrypt_password(admin.private_key, "admin".to_string()).await.unwrap();
|
||||||
let candidates = ApplicationService::list_applications(&private_key, &db, None, None).await.unwrap();
|
let candidates = ApplicationService::list_applications(&private_key, &db, None, None, None).await.unwrap();
|
||||||
assert_eq!(candidates.len(), 0);
|
assert_eq!(candidates.len(), 0);
|
||||||
|
|
||||||
put_user_data(&db).await;
|
put_user_data(&db).await;
|
||||||
|
|
||||||
let candidates = ApplicationService::list_applications(&private_key, &db, None, None).await.unwrap();
|
let candidates = ApplicationService::list_applications(&private_key, &db, None, None, None).await.unwrap();
|
||||||
assert_eq!(candidates.len(), 1);
|
assert_eq!(candidates.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue