feat: multiple related parents in database

This commit is contained in:
Sebastian Pravda 2022-12-05 16:20:52 +01:00
parent a453117e89
commit fa9af8f8ee
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
4 changed files with 31 additions and 13 deletions

View file

@ -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)]

View file

@ -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
}

View file

@ -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 {

View file

@ -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,