Prep import of csv files

This commit is contained in:
Joshua Coles 2024-02-22 13:49:17 +00:00
parent 2b7cc00fb6
commit 1288d72678
3 changed files with 32 additions and 3 deletions

View File

@ -5,7 +5,7 @@ edition = "2021"
[dependencies]
tokio = { version = "1.0", features = ["full"] }
axum = "0.6.20"
axum = { version = "0.6.20", features = ["multipart"] }
clap = { version = "4.4.3", features = ["derive", "env"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.106"
@ -31,4 +31,5 @@ migration = { path = "./migration" }
chrono = { version = "0.4.31", features = ["serde"] }
futures = "0.3.29"
rucron = "0.1.5"
csv = "1.3.0"
#tokio-cron-scheduler = "0.9.4"

View File

@ -1,5 +1,5 @@
use crate::entity::{client, project, time_entry};
use crate::types::{Project, ProjectClient, ReportEntry};
use crate::types::{Project, ProjectClient, ReportEntry, TimeEntry};
use sea_orm::sea_query::OnConflict;
use sea_orm::{NotSet, Set};

View File

@ -1,9 +1,11 @@
use crate::client::TogglClient;
use crate::entity::prelude::TimeEntry;
use crate::entity::time_entry;
use crate::entity::time_entry::ActiveModel;
use crate::types::{Current, Project, ProjectClient, ReportEntry, TogglQuery};
use anyhow::anyhow;
use axum::extract::Query;
use axum::extract::multipart::Field;
use axum::extract::{Multipart, Query};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::routing::{get, post};
@ -193,6 +195,31 @@ fn day_exclusivity_condition(start: NaiveDate, end: NaiveDate) -> Condition {
.into_condition()
}
fn from_csv_row(row: csv::StringRecord) -> ActiveModel {
unimplemented!("Need to refactor db first")
}
async fn import_csv(
Extension(db): Extension<DatabaseConnection>,
mut multipart: Multipart,
) -> impl IntoResponse {
return (StatusCode::NOT_IMPLEMENTED, "Not implemented");
while let Some(field) = multipart.next_field().await? {
if field.name() == "csv" {
let csv = field.bytes().await?;
let mut csv = csv::Reader::from_reader(csv.as_ref());
let data = csv.records().filter_map(|f| f.ok()).map(from_csv_row);
time_entry::Entity::insert_many(data.collect::<Result<_>>().unwrap())
.on_conflict(ReportEntry::grafting_conflict_statement())
.exec(&db)
.await
.unwrap()
}
}
}
#[tokio::main]
async fn main() -> Result<()> {
// install global collector configured based on RUST_LOG env var.
@ -218,6 +245,7 @@ async fn main() -> Result<()> {
// build our application with a route
let app = Router::new()
.route("/import_csv", post(import_csv))
.route("/health", get(health))
.route("/current", get(current))
.route("/refresh", post(refresh))