mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-24 12:35:31 +00:00
Merge pull request #55 from EETagent/list_candidates_improvements
feat: list candidates improvements
This commit is contained in:
commit
1af4e0f703
3 changed files with 25 additions and 10 deletions
|
|
@ -86,11 +86,12 @@ pub async fn create_candidate(
|
|||
Ok(plain_text_password)
|
||||
}
|
||||
|
||||
#[get("/candidates?<field>")]
|
||||
#[get("/candidates?<field>&<page>")]
|
||||
pub async fn list_candidates(
|
||||
conn: Connection<'_, Db>,
|
||||
session: AdminAuth,
|
||||
field: Option<String>,
|
||||
page: Option<u64>,
|
||||
) -> Result<Json<Vec<CandidateResponse>>, Custom<String>> {
|
||||
let db = conn.into_inner();
|
||||
let private_key = session.get_private_key();
|
||||
|
|
@ -101,7 +102,7 @@ pub async fn list_candidates(
|
|||
|
||||
}
|
||||
|
||||
let candidates = CandidateService::list_candidates(private_key, db, field)
|
||||
let candidates = CandidateService::list_candidates(private_key, db, field, page)
|
||||
.await
|
||||
.map_err(|e| Custom(Status::from_code(e.code()).unwrap(), e.to_string()))?;
|
||||
|
||||
|
|
|
|||
|
|
@ -24,26 +24,34 @@ impl Query {
|
|||
db: &DbConn,
|
||||
id: i32,
|
||||
) -> Result<Option<candidate::Model>, DbErr> {
|
||||
Candidate::find_by_id(id).one(db).await
|
||||
Candidate::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn list_candidates(
|
||||
db: &DbConn,
|
||||
field_of_study_opt: Option<String>,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<CandidateParentResult>, DbErr> {
|
||||
let select = Candidate::find();
|
||||
if let Some(study) = field_of_study_opt {
|
||||
let query = if let Some(study) = field_of_study_opt {
|
||||
select.filter(candidate::Column::Study.eq(study))
|
||||
} else {
|
||||
select
|
||||
}
|
||||
.order_by(candidate::Column::Application, Order::Asc)
|
||||
.join(JoinType::InnerJoin, candidate::Relation::Parent.def())
|
||||
.column_as(parent::Column::Name, "parent_name")
|
||||
.column_as(parent::Column::Surname, "parent_surname")
|
||||
.into_model::<CandidateParentResult>()
|
||||
.paginate(db, PAGE_SIZE)
|
||||
.fetch()
|
||||
.await
|
||||
.paginate(db, PAGE_SIZE);
|
||||
|
||||
if let Some(page) = page {
|
||||
query.fetch_page(page).await
|
||||
} else {
|
||||
query.fetch().await
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -144,9 +144,15 @@ impl CandidateService {
|
|||
private_key: String,
|
||||
db: &DbConn,
|
||||
field_of_study: Option<String>,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<CandidateResponse>, ServiceError> {
|
||||
|
||||
let candidates = Query::list_candidates(db, field_of_study).await?;
|
||||
let candidates = Query::list_candidates(
|
||||
db,
|
||||
field_of_study,
|
||||
page
|
||||
).await?;
|
||||
|
||||
let mut result: Vec<CandidateResponse> = vec![];
|
||||
|
||||
for candidate in candidates {
|
||||
|
|
@ -288,12 +294,12 @@ pub mod tests {
|
|||
let db = get_memory_sqlite_connection().await;
|
||||
let admin = create_admin(&db).await;
|
||||
let private_key = crypto::decrypt_password(admin.private_key, "admin".to_string()).await.unwrap();
|
||||
let candidates = CandidateService::list_candidates(private_key.clone(), &db, None).await.unwrap();
|
||||
let candidates = CandidateService::list_candidates(private_key.clone(), &db, None, None).await.unwrap();
|
||||
assert_eq!(candidates.len(), 0);
|
||||
|
||||
put_user_data(&db).await;
|
||||
|
||||
let candidates = CandidateService::list_candidates(private_key.clone(), &db, None).await.unwrap();
|
||||
let candidates = CandidateService::list_candidates(private_key.clone(), &db, None, None).await.unwrap();
|
||||
assert_eq!(candidates.len(), 1);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue