feat(persistence): add SqliteSongRepository and SqliteRepositoryFactory

This commit is contained in:
2026-04-08 03:05:16 +02:00
parent 8f9886c9a9
commit 37a1e386e4
5 changed files with 3805 additions and 1 deletions

View File

@@ -0,0 +1,131 @@
use async_trait::async_trait;
use domain::{
RepositoryError, Song, SongMeta, SongRepositoryPort, SongSummary, StoredSong,
song_preview_chords,
};
use sqlx::SqlitePool;
use uuid::Uuid;
pub struct SqliteSongRepository {
pool: SqlitePool,
}
impl SqliteSongRepository {
pub async fn new(database_url: &str) -> Result<Self, sqlx::Error> {
let pool = SqlitePool::connect(database_url).await?;
sqlx::migrate!("./migrations").run(&pool).await?;
Ok(Self { pool })
}
}
#[derive(sqlx::FromRow)]
struct SongRow {
id: String,
title: String,
artist: String,
original_key: Option<String>,
preview_chords: String,
body: String,
}
#[async_trait]
impl SongRepositoryPort for SqliteSongRepository {
async fn save(&self, song: &Song) -> Result<StoredSong, RepositoryError> {
let id = Uuid::new_v4();
let id_str = id.to_string();
let body = serde_json::to_string(song)
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
let preview = song_preview_chords(song);
let preview_json = serde_json::to_string(&preview)
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
let original_key = song.meta.original_key.as_deref();
sqlx::query(
"INSERT INTO songs (id, title, artist, original_key, preview_chords, body) VALUES (?, ?, ?, ?, ?, ?)"
)
.bind(&id_str)
.bind(&song.meta.title)
.bind(&song.meta.artist)
.bind(original_key)
.bind(&preview_json)
.bind(&body)
.execute(&self.pool)
.await
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
Ok(StoredSong { id, song: song.clone() })
}
async fn list(&self) -> Result<Vec<SongSummary>, RepositoryError> {
let rows = sqlx::query_as::<_, SongRow>(
"SELECT id, title, artist, original_key, preview_chords, body FROM songs ORDER BY created_at DESC"
)
.fetch_all(&self.pool)
.await
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
rows.into_iter()
.map(|row| {
let id = Uuid::parse_str(&row.id)
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
let preview_chords: Vec<String> = serde_json::from_str(&row.preview_chords)
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
Ok(SongSummary {
id,
meta: SongMeta {
title: row.title,
artist: row.artist,
original_key: row.original_key,
capo: None,
tuning: None,
tempo: None,
},
preview_chords,
})
})
.collect()
}
async fn get(&self, id: Uuid) -> Result<Option<Song>, RepositoryError> {
let id_str = id.to_string();
let row = sqlx::query_as::<_, SongRow>(
"SELECT id, title, artist, original_key, preview_chords, body FROM songs WHERE id = ?"
)
.bind(&id_str)
.fetch_optional(&self.pool)
.await
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
match row {
None => Ok(None),
Some(r) => {
let song: Song = serde_json::from_str(&r.body)
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
Ok(Some(song))
}
}
}
async fn delete(&self, id: Uuid) -> Result<(), RepositoryError> {
let id_str = id.to_string();
let result = sqlx::query("DELETE FROM songs WHERE id = ?")
.bind(&id_str)
.execute(&self.pool)
.await
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
if result.rows_affected() == 0 {
Err(RepositoryError::NotFound)
} else {
Ok(())
}
}
}
pub struct SqliteRepositoryFactory;
impl SqliteRepositoryFactory {
pub async fn create(database_url: &str) -> Result<SqliteSongRepository, sqlx::Error> {
SqliteSongRepository::new(database_url).await
}
}