Add /shortcuts-csv-import
Some checks failed
Build and Publish / Build and Test (push) Has been cancelled

This commit is contained in:
Joshua Coles 2024-10-31 12:03:36 +00:00
parent 6d2150d1b2
commit e60ab43abd
2 changed files with 28 additions and 1 deletions

View File

@ -50,6 +50,32 @@ async fn extract_csv_and_account_name(mut multipart: Multipart) -> Result<(Optio
Ok((csv, account_name))
}
#[derive(serde::Deserialize, Debug, Clone)]
struct ShortcutBody {
pub body: String,
pub account_name: String,
}
pub async fn shortcuts_csv(
Extension(db): Extension<DatabaseConnection>,
Json(shortcut_body): Json<ShortcutBody>
) -> Result<&'static str, AppError> {
let account_id = db::get_account_id(&db, Some(shortcut_body.account_name)).await?;
let csv = Cursor::new(shortcut_body.body.as_bytes());
let mut csv = csv::Reader::from_reader(csv);
let data = csv.records();
let data = data
.filter_map(|f| f.ok())
.map(from_csv_row)
.collect::<Result<_, _>>()?;
db::insert(&db, data, account_id).await?;
Ok("Ok")
}
pub async fn monzo_batched_csv(
Extension(db): Extension<DatabaseConnection>,
multipart: Multipart,

View File

@ -4,7 +4,7 @@ mod ingestion;
use crate::error::AppError;
use crate::ingestion::db;
use crate::ingestion::ingestion_logic::from_csv_row;
use crate::ingestion::routes::{monzo_batched_csv, monzo_batched_json};
use crate::ingestion::routes::{monzo_batched_csv, monzo_batched_json, shortcuts_csv};
use axum::routing::{get, post};
use axum::{Extension, Router};
use clap::{Parser, Subcommand};
@ -119,6 +119,7 @@ async fn serve_web(address: SocketAddr, connection: DatabaseConnection) -> anyho
.route("/health", get(health_check))
.route("/monzo-batch-export", post(monzo_batched_json))
.route("/monzo-csv-ingestion", post(monzo_batched_csv))
.route("/shortcuts-csv-import", post(shortcuts_csv))
.layer(Extension(connection.clone()))
.layer(TraceLayer::new_for_http());