diff --git a/src/lib.rs b/src/lib.rs index 7cb3263..8cb5a3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod config; pub mod db; +pub mod repo; diff --git a/src/repo/mod.rs b/src/repo/mod.rs new file mode 100644 index 0000000..6998909 --- /dev/null +++ b/src/repo/mod.rs @@ -0,0 +1,48 @@ +pub async fn insert( + pool: &sqlx::PgPool, + song: &icarus_models::song::Song, +) -> Result<(time::OffsetDateTime, uuid::Uuid), sqlx::Error> { + let result = sqlx::query( + r#" + INSERT INTO "song" (title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, filename, directory, user_id) + VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING date_created, id; + "# + ) + .bind(&song.title) + .bind(&song.artist) + .bind(&song.album_artist) + .bind(&song.album) + .bind(&song.genre) + .bind(song.year) + .bind(song.track) + .bind(song.disc) + .bind(song.track_count) + .bind(song.disc_count) + .bind(song.duration) + .bind(&song.audio_type) + .bind(&song.filename) + .bind(&song.directory) + .bind(song.user_id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting query: {e}"); + }); + + match result { + Ok(row) => { + let id: uuid::Uuid = row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(); + let date_created_time: time::OffsetDateTime = row + .try_get("date_created") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(); + let date_created = date_created_time; + + Ok((date_created, id)) + } + Err(_) => Err(sqlx::Error::RowNotFound), + } +}