refactor: NaiveDate parsing

This commit is contained in:
Sebastian Pravda 2023-01-15 20:00:56 +01:00
parent 12e86d5dff
commit f054130c12
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
4 changed files with 20 additions and 3 deletions

View file

@ -28,6 +28,8 @@ pub enum ServiceError {
InternalServerError, InternalServerError,
#[error("Parrent not found")] #[error("Parrent not found")]
ParentNotFound, ParentNotFound,
#[error("Invalid date")]
InvalidDate,
#[error("Database error")] #[error("Database error")]
DbError(#[from] sea_orm::DbErr), DbError(#[from] sea_orm::DbErr),
#[error("Too many parents")] #[error("Too many parents")]
@ -92,6 +94,7 @@ impl ServiceError {
ServiceError::TooManyApplications => 409, ServiceError::TooManyApplications => 409,
// 500 // 500
ServiceError::InternalServerError => 500, ServiceError::InternalServerError => 500,
ServiceError::InvalidDate => 500,
ServiceError::ParentNotFound => 500, ServiceError::ParentNotFound => 500,
ServiceError::DbError(_) => 500, ServiceError::DbError(_) => 500,
ServiceError::UserNotFoundBySessionId => 500, ServiceError::UserNotFoundBySessionId => 500,

View file

@ -3,7 +3,7 @@ use chrono::NaiveDate;
use entity::{candidate, parent}; use entity::{candidate, parent};
use futures::future; use futures::future;
use crate::{crypto, models::candidate::{ApplicationDetails}, error::ServiceError}; use crate::{crypto, models::candidate::{ApplicationDetails}, error::ServiceError, utils::date::parse_naive_date_from_opt_str};
use super::{candidate::{CandidateDetails, ParentDetails}, application::ApplicationRow}; use super::{candidate::{CandidateDetails, ParentDetails}, application::ApplicationRow};
@ -171,7 +171,7 @@ impl EncryptedCandidateDetails {
name: d.0.unwrap_or_default(), name: d.0.unwrap_or_default(),
surname: d.1.unwrap_or_default(), surname: d.1.unwrap_or_default(),
birthplace: d.2.unwrap_or_default(), birthplace: d.2.unwrap_or_default(),
birthdate: NaiveDate::parse_from_str(&d.3.unwrap_or_default(), NAIVE_DATE_FMT).unwrap_or(NaiveDate::from_ymd(1, 1, 1)), birthdate: parse_naive_date_from_opt_str(d.3, NAIVE_DATE_FMT)?,
address: d.4.unwrap_or_default(), address: d.4.unwrap_or_default(),
telephone: d.5.unwrap_or_default(), telephone: d.5.unwrap_or_default(),
citizenship: d.6.unwrap_or_default(), citizenship: d.6.unwrap_or_default(),

13
core/src/utils/date.rs Normal file
View file

@ -0,0 +1,13 @@
use chrono::NaiveDate;
use crate::error::ServiceError;
pub fn parse_naive_date_from_opt_str(date: Option<String>, fmt: &str) -> Result<NaiveDate, ServiceError> {
Ok(
NaiveDate::parse_from_str(&date.unwrap_or_default(), fmt)
.unwrap_or(
NaiveDate::from_ymd_opt(1, 1, 1)
.ok_or(ServiceError::InvalidDate)?
)
)
}

View file

@ -1,3 +1,4 @@
pub mod csv; pub mod csv;
pub mod filetype; pub mod filetype;
pub mod db; pub mod db;
pub mod date;