mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-06 11:30:44 +00:00
feat: multiple related parents in database
This commit is contained in:
parent
a453117e89
commit
fa9af8f8ee
4 changed files with 31 additions and 13 deletions
|
|
@ -1,12 +1,16 @@
|
||||||
|
|
||||||
|
use entity::candidate;
|
||||||
|
use entity::parent;
|
||||||
use entity::parent::Model;
|
use entity::parent::Model;
|
||||||
use entity::parent::Entity;
|
use entity::parent::Entity;
|
||||||
|
use sea_orm::ModelTrait;
|
||||||
use sea_orm::{DbConn, DbErr};
|
use sea_orm::{DbConn, DbErr};
|
||||||
use sea_orm::EntityTrait;
|
use sea_orm::EntityTrait;
|
||||||
|
|
||||||
use crate::Query;
|
use crate::Query;
|
||||||
|
|
||||||
impl Query {
|
impl Query {
|
||||||
|
#[deprecated(note = "Use find_candidate_parents instead")]
|
||||||
pub async fn find_parent_by_id(
|
pub async fn find_parent_by_id(
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
application_id: i32,
|
application_id: i32,
|
||||||
|
|
@ -14,6 +18,17 @@ impl Query {
|
||||||
|
|
||||||
Entity::find_by_id(application_id).one(db).await
|
Entity::find_by_id(application_id).one(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO limit to two parents??
|
||||||
|
pub async fn find_candidate_parents(
|
||||||
|
db: &DbConn,
|
||||||
|
candidate: candidate::Model,
|
||||||
|
) -> Result<Vec<Model>, DbErr> {
|
||||||
|
|
||||||
|
candidate.find_related(parent::Entity)
|
||||||
|
.all(db)
|
||||||
|
.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -31,9 +31,8 @@ impl ApplicationService {
|
||||||
.await?
|
.await?
|
||||||
.ok_or(ServiceError::CandidateNotFound)?;
|
.ok_or(ServiceError::CandidateNotFound)?;
|
||||||
|
|
||||||
let parent = Query::find_parent_by_id(db, application)
|
let parent = Query::find_candidate_parents(db, candidate.clone())
|
||||||
.await?
|
.await?;
|
||||||
.ok_or(ServiceError::ParentNotFound)?;
|
|
||||||
|
|
||||||
let recipients = get_recipients(db, &candidate.public_key).await?;
|
let recipients = get_recipients(db, &candidate.public_key).await?;
|
||||||
|
|
||||||
|
|
@ -42,7 +41,7 @@ impl ApplicationService {
|
||||||
Ok(
|
Ok(
|
||||||
tokio::try_join!(
|
tokio::try_join!(
|
||||||
CandidateService::add_candidate_details(db, candidate, enc_details.candidate),
|
CandidateService::add_candidate_details(db, candidate, enc_details.candidate),
|
||||||
ParentService::add_parent_details(db, parent, enc_details.parents[0].clone())
|
ParentService::add_parent_details(db, parent[0].clone(), enc_details.parents[0].clone()) // TODO
|
||||||
)?
|
)?
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -54,9 +53,8 @@ impl ApplicationService {
|
||||||
) -> Result<ApplicationDetails, ServiceError> {
|
) -> Result<ApplicationDetails, ServiceError> {
|
||||||
let candidate = Query::find_candidate_by_id(db, application_id).await?
|
let candidate = Query::find_candidate_by_id(db, application_id).await?
|
||||||
.ok_or(ServiceError::CandidateNotFound)?;
|
.ok_or(ServiceError::CandidateNotFound)?;
|
||||||
let parent = Query::find_parent_by_id(db, application_id).await?
|
let parent = Query::find_candidate_parents(db, candidate.clone()).await?; // TODO
|
||||||
.ok_or(ServiceError::ParentNotFound)?;
|
let enc_details = EncryptedApplicationDetails::try_from((candidate, parent[0].clone()))?;
|
||||||
let enc_details = EncryptedApplicationDetails::try_from((candidate, parent))?;
|
|
||||||
|
|
||||||
enc_details.decrypt(private_key).await
|
enc_details.decrypt(private_key).await
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -102,8 +102,7 @@ impl CandidateService {
|
||||||
) -> Result<CreateCandidateResponse, ServiceError> {
|
) -> Result<CreateCandidateResponse, ServiceError> {
|
||||||
let candidate = Query::find_candidate_by_id(db, id).await?
|
let candidate = Query::find_candidate_by_id(db, id).await?
|
||||||
.ok_or(ServiceError::CandidateNotFound)?;
|
.ok_or(ServiceError::CandidateNotFound)?;
|
||||||
let parent = Query::find_parent_by_id(db, id).await?
|
let parent = Query::find_candidate_parents(db, candidate.clone()).await?; // TODO
|
||||||
.ok_or(ServiceError::CandidateNotFound)?;
|
|
||||||
|
|
||||||
|
|
||||||
let new_password_plain = crypto::random_8_char_string();
|
let new_password_plain = crypto::random_8_char_string();
|
||||||
|
|
@ -125,7 +124,7 @@ impl CandidateService {
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let enc_details_opt = EncryptedApplicationDetails::try_from(
|
let enc_details_opt = EncryptedApplicationDetails::try_from(
|
||||||
(candidate, parent)
|
(candidate, parent[0].clone())
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Ok(enc_details) = enc_details_opt {
|
if let Ok(enc_details) = enc_details_opt {
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,17 @@ impl MigrationTrait for Migration {
|
||||||
Table::create()
|
Table::create()
|
||||||
.table(Parent::Table)
|
.table(Parent::Table)
|
||||||
.if_not_exists()
|
.if_not_exists()
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(Parent::Id)
|
||||||
|
.integer()
|
||||||
|
.not_null()
|
||||||
|
.auto_increment()
|
||||||
|
.primary_key(),
|
||||||
|
)
|
||||||
.col(
|
.col(
|
||||||
ColumnDef::new(Parent::Application)
|
ColumnDef::new(Parent::Application)
|
||||||
.integer()
|
.integer()
|
||||||
.not_null()
|
.not_null()
|
||||||
.primary_key()
|
|
||||||
.unique_key(),
|
|
||||||
)
|
)
|
||||||
.col(ColumnDef::new(Parent::Name).string())
|
.col(ColumnDef::new(Parent::Name).string())
|
||||||
.col(ColumnDef::new(Parent::Surname).string())
|
.col(ColumnDef::new(Parent::Surname).string())
|
||||||
|
|
@ -26,7 +31,7 @@ impl MigrationTrait for Migration {
|
||||||
.col(ColumnDef::new(Parent::UpdatedAt).date_time().not_null())
|
.col(ColumnDef::new(Parent::UpdatedAt).date_time().not_null())
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
|
@ -38,6 +43,7 @@ impl MigrationTrait for Migration {
|
||||||
|
|
||||||
#[derive(Iden)]
|
#[derive(Iden)]
|
||||||
pub enum Parent {
|
pub enum Parent {
|
||||||
|
Id,
|
||||||
Table,
|
Table,
|
||||||
Application,
|
Application,
|
||||||
Name,
|
Name,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue