Prep import of csv files
This commit is contained in:
parent
2b7cc00fb6
commit
1288d72678
@ -5,7 +5,7 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1.0", features = ["full"] }
|
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"] }
|
clap = { version = "4.4.3", features = ["derive", "env"] }
|
||||||
serde = { version = "1.0.188", features = ["derive"] }
|
serde = { version = "1.0.188", features = ["derive"] }
|
||||||
serde_json = "1.0.106"
|
serde_json = "1.0.106"
|
||||||
@ -31,4 +31,5 @@ migration = { path = "./migration" }
|
|||||||
chrono = { version = "0.4.31", features = ["serde"] }
|
chrono = { version = "0.4.31", features = ["serde"] }
|
||||||
futures = "0.3.29"
|
futures = "0.3.29"
|
||||||
rucron = "0.1.5"
|
rucron = "0.1.5"
|
||||||
|
csv = "1.3.0"
|
||||||
#tokio-cron-scheduler = "0.9.4"
|
#tokio-cron-scheduler = "0.9.4"
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
use crate::entity::{client, project, time_entry};
|
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::sea_query::OnConflict;
|
||||||
use sea_orm::{NotSet, Set};
|
use sea_orm::{NotSet, Set};
|
||||||
|
|
||||||
|
|||||||
30
src/main.rs
30
src/main.rs
@ -1,9 +1,11 @@
|
|||||||
use crate::client::TogglClient;
|
use crate::client::TogglClient;
|
||||||
use crate::entity::prelude::TimeEntry;
|
use crate::entity::prelude::TimeEntry;
|
||||||
use crate::entity::time_entry;
|
use crate::entity::time_entry;
|
||||||
|
use crate::entity::time_entry::ActiveModel;
|
||||||
use crate::types::{Current, Project, ProjectClient, ReportEntry, TogglQuery};
|
use crate::types::{Current, Project, ProjectClient, ReportEntry, TogglQuery};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use axum::extract::Query;
|
use axum::extract::multipart::Field;
|
||||||
|
use axum::extract::{Multipart, Query};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
use axum::routing::{get, post};
|
use axum::routing::{get, post};
|
||||||
@ -193,6 +195,31 @@ fn day_exclusivity_condition(start: NaiveDate, end: NaiveDate) -> Condition {
|
|||||||
.into_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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// install global collector configured based on RUST_LOG env var.
|
// install global collector configured based on RUST_LOG env var.
|
||||||
@ -218,6 +245,7 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
|
.route("/import_csv", post(import_csv))
|
||||||
.route("/health", get(health))
|
.route("/health", get(health))
|
||||||
.route("/current", get(current))
|
.route("/current", get(current))
|
||||||
.route("/refresh", post(refresh))
|
.route("/refresh", post(refresh))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user