Skip to content

Commit

Permalink
Adjusted CSV loading to new into_db_proposal_and_challenge_info method
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsanchezq committed Feb 22, 2021
1 parent c2c990f commit dff6990
Showing 1 changed file with 32 additions and 39 deletions.
71 changes: 32 additions & 39 deletions vit-servicing-station-cli/src/csv/loaders.rs
Expand Up @@ -2,7 +2,7 @@ use crate::db_utils::{backup_db_file, restore_db_file};
use crate::{db_utils::db_file_exists, task::ExecTask};
use csv::Trim;
use serde::de::DeserializeOwned;
use std::convert::TryFrom;
use std::collections::HashMap;
use std::io;
use structopt::StructOpt;
use vit_servicing_station_lib::db::models::proposals::{
Expand Down Expand Up @@ -65,16 +65,6 @@ impl CSVDataCmd {
Ok(results)
}

fn proposals_info_from_csv_proposals(
proposals: &[super::models::Proposal],
) -> io::Result<Vec<ProposalChallengeInfo>> {
let mut res = Vec::new();
for proposal in proposals {
res.push(ProposalChallengeInfo::try_from(proposal.clone())?);
}
Ok(res)
}

fn handle_load(
db_url: &str,
funds_path: &str,
Expand All @@ -94,34 +84,37 @@ impl CSVDataCmd {
));
}
let mut voteplans = CSVDataCmd::load_from_csv::<Voteplan>(voteplans_path)?;
let mut challenges = CSVDataCmd::load_from_csv::<Challenge>(challenges_path)?;
let mut challenges: HashMap<i32, Challenge> =
CSVDataCmd::load_from_csv::<Challenge>(challenges_path)?
.into_iter()
.map(|c| (c.id, c))
.collect();
let csv_proposals = CSVDataCmd::load_from_csv::<super::models::Proposal>(proposals_path)?;
let mut proposals: Vec<Proposal> = csv_proposals.iter().cloned().map(Into::into).collect();
let proposals_challenge_info =
CSVDataCmd::proposals_info_from_csv_proposals(&csv_proposals)?;

let simple_proposals_data: Vec<simple::ChallengeSqlValues> = proposals_challenge_info
.iter()
.zip(proposals.iter())
.filter_map(|(data, proposal)| match data {
ProposalChallengeInfo::Simple(res) => {
Some(res.to_sql_values_with_proposal_id(&proposal.proposal_id))
let mut proposals: Vec<Proposal> = Vec::new();
let mut simple_proposals_data: Vec<simple::ChallengeSqlValues> = Vec::new();
let mut community_proposals_data: Vec<community_choice::ChallengeSqlValues> = Vec::new();

for proposal in csv_proposals {
let challenge_type = challenges
.get(&proposal.challenge_id)
.ok_or(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Challenge with id {} not found", proposal.challenge_id),
))?
.challenge_type
.clone();
let (proposal, challenge_info) =
proposal.into_db_proposal_and_challenge_info(challenge_type)?;
match challenge_info {
ProposalChallengeInfo::Simple(simple) => simple_proposals_data
.push(simple.to_sql_values_with_proposal_id(&proposal.proposal_id)),
ProposalChallengeInfo::CommunityChoice(community_choice) => {
community_proposals_data.push(
community_choice.to_sql_values_with_proposal_id(&proposal.proposal_id),
)
}
ProposalChallengeInfo::CommunityChoice(_) => None,
})
.collect();

let community_proposals_data: Vec<community_choice::ChallengeSqlValues> =
proposals_challenge_info
.iter()
.zip(proposals.iter())
.filter_map(|(data, proposal)| match data {
ProposalChallengeInfo::Simple(_) => None,
ProposalChallengeInfo::CommunityChoice(res) => {
Some(res.to_sql_values_with_proposal_id(&proposal.proposal_id))
}
})
.collect();
};
}

// start db connection
let pool = load_db_connection_pool(db_url)
Expand All @@ -146,7 +139,7 @@ impl CSVDataCmd {
}

// apply fund id to challenges
for challenge in challenges.iter_mut() {
for challenge in challenges.values_mut() {
challenge.fund_id = fund.id;
}

Expand All @@ -173,7 +166,7 @@ impl CSVDataCmd {
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{}", e)))?;

vit_servicing_station_lib::db::queries::challenges::batch_insert_challenges(
&challenges,
&challenges.values().cloned().collect::<Vec<Challenge>>(),
&db_conn,
)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{}", e)))?;
Expand Down

0 comments on commit dff6990

Please sign in to comment.