|
| 1 | +use log::debug; |
| 2 | +use log::error; |
| 3 | +use log::info; |
| 4 | +use log::trace; |
| 5 | +use log::warn; |
| 6 | +use diesel::prelude::*; |
| 7 | +use diesel::MysqlConnection; |
| 8 | +use dotenv::dotenv; |
| 9 | +use csv; |
| 10 | +use web_dev::users::models::{NewUser,UserRequest}; |
| 11 | +use web_dev::users::requests; |
| 12 | +use serde::Deserialize; |
| 13 | +use serde::Serialize; |
| 14 | + |
| 15 | +#[derive(Serialize, Deserialize, Debug)] |
| 16 | +//Struct to take the data from the csv |
| 17 | +struct Csv_User { |
| 18 | + #[serde(rename = "Banner ID")] |
| 19 | + banner_id: i32, |
| 20 | + #[serde(rename = "Last Name")] |
| 21 | + last_name: String, |
| 22 | + #[serde(rename = "First Name")] |
| 23 | + first_name: String, |
| 24 | + #[serde(rename = "Email")] |
| 25 | + email: String, |
| 26 | + #[serde(rename = "Year")] |
| 27 | + year: String, |
| 28 | + #[serde(rename = "Department")] |
| 29 | + department: String, |
| 30 | +} |
| 31 | +fn main(){ |
| 32 | + //Diesel things |
| 33 | + dotenv().ok(); |
| 34 | + |
| 35 | + simplelog::TermLogger::init(simplelog::LevelFilter::Trace, simplelog::Config::default()) |
| 36 | + .unwrap(); |
| 37 | + |
| 38 | + info!("Connecting to database"); |
| 39 | + |
| 40 | + let database_url = match env::var("DATABASE_URL") { |
| 41 | + Ok(url) => url, |
| 42 | + Err(e) => { |
| 43 | + error!("Could not read DATABASE_URL environment variable"); |
| 44 | + return; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + debug!("Connecting to {}", database_url); |
| 49 | + |
| 50 | + let connection = match MysqlConnection::establish(&database_url) { |
| 51 | + Ok(c) => c, |
| 52 | + Err(e) => { |
| 53 | + error!("Could not connect to database: {}", e); |
| 54 | + return; |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + debug!("Connected to database"); |
| 59 | + //Get file name and path from args |
| 60 | + use std::env; |
| 61 | + let arg = env::args().nth(1); |
| 62 | + let filename = match arg { |
| 63 | + Some(name) => name, |
| 64 | + None => { |
| 65 | + error!("Needs a filename"); |
| 66 | + return; |
| 67 | + } |
| 68 | + }; |
| 69 | + debug!("{}", filename); |
| 70 | + //Import the csv into an iterator |
| 71 | + let mut user_count = 0; |
| 72 | + let all_users_result = csv::Reader::from_path(filename); |
| 73 | + let mut all_users = match all_users_result{ |
| 74 | + Ok(data) => data, |
| 75 | + Err(e) => { |
| 76 | + error!("Bad file. Error {}",e); |
| 77 | + return; |
| 78 | + } |
| 79 | + }; |
| 80 | + //Go through each item in the iterator |
| 81 | + for result in all_users.deserialize(){ |
| 82 | + //Check to see if it's valid |
| 83 | + let csv_user: Csv_User = match result{ |
| 84 | + Ok(data) => data, |
| 85 | + Err(e) => { |
| 86 | + error!("Bad data, {:?}", e); |
| 87 | + return; |
| 88 | + } |
| 89 | + }; |
| 90 | + //Convert the user data from the csv and create a New User from it |
| 91 | + let new_user:NewUser = NewUser{ |
| 92 | + first_name: csv_user.first_name, |
| 93 | + last_name: csv_user.last_name, |
| 94 | + email: Some(csv_user.email), |
| 95 | + banner_id: csv_user.banner_id as u32, |
| 96 | + }; |
| 97 | + //Import new user into database |
| 98 | + let import_user = UserRequest::CreateUser(new_user); |
| 99 | + requests::handle_user(import_user, &connection); |
| 100 | + user_count = user_count+1; |
| 101 | + } |
| 102 | + info!("Imported {} user(s)",user_count); |
| 103 | +} |
| 104 | + |
0 commit comments