feat: add update_meta to SongRepositoryPort and PATCH /songs/{id}
This commit is contained in:
@@ -102,6 +102,58 @@ impl SongRepositoryPort for SqliteSongRepository {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn update_meta(
|
||||
&self,
|
||||
id: Uuid,
|
||||
title: Option<&str>,
|
||||
artist: Option<&str>,
|
||||
original_key: Option<&str>,
|
||||
) -> Result<SongSummary, 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()))?
|
||||
.ok_or(RepositoryError::NotFound)?;
|
||||
|
||||
let mut song: Song = serde_json::from_str(&row.body)
|
||||
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
|
||||
if let Some(t) = title { song.meta.title = t.to_string(); }
|
||||
if let Some(a) = artist { song.meta.artist = a.to_string(); }
|
||||
if let Some(k) = original_key { song.meta.original_key = Some(k.to_string()); }
|
||||
let new_body = serde_json::to_string(&song)
|
||||
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
|
||||
|
||||
let new_title = title.unwrap_or(&row.title);
|
||||
let new_artist = artist.unwrap_or(&row.artist);
|
||||
let new_key: Option<&str> = original_key.or(row.original_key.as_deref());
|
||||
|
||||
sqlx::query(
|
||||
"UPDATE songs SET title = ?, artist = ?, original_key = ?, body = ? WHERE id = ?"
|
||||
)
|
||||
.bind(new_title)
|
||||
.bind(new_artist)
|
||||
.bind(new_key)
|
||||
.bind(&new_body)
|
||||
.bind(&id_str)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.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: song.meta,
|
||||
preview_chords,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
||||
Reference in New Issue
Block a user