mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-19 22:41:13 +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::Entity;
|
||||
use sea_orm::ModelTrait;
|
||||
use sea_orm::{DbConn, DbErr};
|
||||
use sea_orm::EntityTrait;
|
||||
|
||||
use crate::Query;
|
||||
|
||||
impl Query {
|
||||
#[deprecated(note = "Use find_candidate_parents instead")]
|
||||
pub async fn find_parent_by_id(
|
||||
db: &DbConn,
|
||||
application_id: i32,
|
||||
|
|
@ -14,6 +18,17 @@ impl Query {
|
|||
|
||||
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)]
|
||||
|
|
|
|||
|
|
@ -31,9 +31,8 @@ impl ApplicationService {
|
|||
.await?
|
||||
.ok_or(ServiceError::CandidateNotFound)?;
|
||||
|
||||
let parent = Query::find_parent_by_id(db, application)
|
||||
.await?
|
||||
.ok_or(ServiceError::ParentNotFound)?;
|
||||
let parent = Query::find_candidate_parents(db, candidate.clone())
|
||||
.await?;
|
||||
|
||||
let recipients = get_recipients(db, &candidate.public_key).await?;
|
||||
|
||||
|
|
@ -42,7 +41,7 @@ impl ApplicationService {
|
|||
Ok(
|
||||
tokio::try_join!(
|
||||
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> {
|
||||
let candidate = Query::find_candidate_by_id(db, application_id).await?
|
||||
.ok_or(ServiceError::CandidateNotFound)?;
|
||||
let parent = Query::find_parent_by_id(db, application_id).await?
|
||||
.ok_or(ServiceError::ParentNotFound)?;
|
||||
let enc_details = EncryptedApplicationDetails::try_from((candidate, parent))?;
|
||||
let parent = Query::find_candidate_parents(db, candidate.clone()).await?; // TODO
|
||||
let enc_details = EncryptedApplicationDetails::try_from((candidate, parent[0].clone()))?;
|
||||
|
||||
enc_details.decrypt(private_key).await
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,8 +102,7 @@ impl CandidateService {
|
|||
) -> Result<CreateCandidateResponse, ServiceError> {
|
||||
let candidate = Query::find_candidate_by_id(db, id).await?
|
||||
.ok_or(ServiceError::CandidateNotFound)?;
|
||||
let parent = Query::find_parent_by_id(db, id).await?
|
||||
.ok_or(ServiceError::CandidateNotFound)?;
|
||||
let parent = Query::find_candidate_parents(db, candidate.clone()).await?; // TODO
|
||||
|
||||
|
||||
let new_password_plain = crypto::random_8_char_string();
|
||||
|
|
@ -125,7 +124,7 @@ impl CandidateService {
|
|||
.await?;
|
||||
|
||||
let enc_details_opt = EncryptedApplicationDetails::try_from(
|
||||
(candidate, parent)
|
||||
(candidate, parent[0].clone())
|
||||
);
|
||||
|
||||
if let Ok(enc_details) = enc_details_opt {
|
||||
|
|
|
|||
|
|
@ -11,12 +11,17 @@ impl MigrationTrait for Migration {
|
|||
Table::create()
|
||||
.table(Parent::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Parent::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Parent::Application)
|
||||
.integer()
|
||||
.not_null()
|
||||
.primary_key()
|
||||
.unique_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Parent::Name).string())
|
||||
.col(ColumnDef::new(Parent::Surname).string())
|
||||
|
|
@ -26,7 +31,7 @@ impl MigrationTrait for Migration {
|
|||
.col(ColumnDef::new(Parent::UpdatedAt).date_time().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
|
|
@ -38,6 +43,7 @@ impl MigrationTrait for Migration {
|
|||
|
||||
#[derive(Iden)]
|
||||
pub enum Parent {
|
||||
Id,
|
||||
Table,
|
||||
Application,
|
||||
Name,
|
||||
|
|
|
|||
Loading…
Reference in a new issue