From b6e4af6ef7da0d62f744c13b90dae8759912cf78 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Tue, 15 Nov 2022 18:38:21 +0100 Subject: [PATCH] refactor: methods order --- core/src/services/candidate_service.rs | 57 +++++++++++++++++++++++++- core/src/services/mod.rs | 3 +- core/src/services/portfolio_service.rs | 31 ++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 core/src/services/portfolio_service.rs diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index f4ff3b1..59c2f1e 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -5,14 +5,42 @@ use sea_orm::{prelude::Uuid, DbConn}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use crate::{ - candidate_details::EncryptedApplicationDetails, + candidate_details::{EncryptedApplicationDetails}, crypto::{self, hash_password}, error::ServiceError, - Mutation, Query, + Mutation, Query, responses::CandidateResponse, }; use super::session_service::{AdminUser, SessionService}; +// TODO + +/* pub struct FieldOfStudy { + pub short_name: String, + pub full_name: String, + pub code: i32, +} + +impl FieldOfStudy { + pub fn new(short_name: String, full_name: String, code: i32) -> Self { + Self { + short_name, + full_name, + code, + } + } + + pub fn code_str(&self) -> String { + format!("{:04}", self.code) + } +} + +pub enum FieldsOfStudy { + KB(FieldOfStudy), + IT(FieldOfStudy), + G(FieldOfStudy), +} */ + const FIELD_OF_STUDY_PREFIXES: [&str; 3] = ["101", "102", "103"]; pub struct CandidateService; @@ -81,6 +109,31 @@ impl CandidateService { Ok(model) } + pub async fn list_candidates( + private_key: String, + db: &DbConn, + field_of_study: Option, + ) -> Result, ServiceError> { + + let candidates = Query::list_candidates(db, None).await?; + let mut result: Vec = vec![]; + + for candidate in candidates { + result.push( + CandidateResponse::from_encrypted( + &private_key, + candidate.application, + candidate.name, + candidate.surname, + candidate.study, + true + ).await? + ) + } + + Ok(result) + } + pub fn is_candidate_info(candidate: &candidate::Model) -> bool { candidate.name.is_some() && candidate.surname.is_some() diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index 28fe987..9cea043 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -2,4 +2,5 @@ pub mod session_service; pub mod candidate_service; pub mod admin_service; pub mod parent_service; -pub mod application_service; \ No newline at end of file +pub mod application_service; +pub mod portfolio_service; \ No newline at end of file diff --git a/core/src/services/portfolio_service.rs b/core/src/services/portfolio_service.rs new file mode 100644 index 0000000..3c62219 --- /dev/null +++ b/core/src/services/portfolio_service.rs @@ -0,0 +1,31 @@ +use std::path::Path; + +use tokio::io::AsyncWriteExt; + +use crate::error::ServiceError; + +pub struct PortfolioService; + +impl PortfolioService { + pub async fn write_portfolio_file( + candidate_id: i32, + data: Vec, + filename: &str, + ) -> Result<(), ServiceError> { + let cache_path = Path::new(&candidate_id.to_string()).join("cache"); + + let mut file = tokio::fs::File::create(cache_path.join(filename)).await?; + + file.write_all(&data).await?; + + Ok(()) + } + + pub async fn is_portfolio_complete(candidate_id: i32) -> bool { + let cache_path = Path::new(&candidate_id.to_string()).join("cache"); + + tokio::fs::metadata(cache_path.join("MOTIVACNI_DOPIS.pdf")).await.is_ok() + && tokio::fs::metadata(cache_path.join("PORTFOLIO.pdf")).await.is_ok() + && tokio::fs::metadata(cache_path.join("PORTFOLIO.zip")).await.is_ok() + } +} \ No newline at end of file