Added merge
This commit is contained in:
@@ -690,91 +690,8 @@ pub async fn get_tracks_with_albums_for_artist(pool: &PgPool, artist_id: i64) ->
|
||||
).bind(artist_id).fetch_all(pool).await
|
||||
}
|
||||
|
||||
pub async fn rename_artist(pool: &PgPool, id: i64, new_name: &str) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("UPDATE artists SET name = $2 WHERE id = $1")
|
||||
.bind(id).bind(new_name).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_artist(pool: &PgPool, id: i64) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("DELETE FROM artists WHERE id = $1")
|
||||
.bind(id).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn rename_album(pool: &PgPool, id: i64, new_name: &str) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("UPDATE albums SET name = $2 WHERE id = $1")
|
||||
.bind(id).bind(new_name).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_album_artist(pool: &PgPool, album_id: i64, artist_id: i64) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("UPDATE albums SET artist_id = $2 WHERE id = $1")
|
||||
.bind(album_id).bind(artist_id).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub async fn move_track_artists(pool: &PgPool, from_artist_id: i64, to_artist_id: i64) -> Result<(), sqlx::Error> {
|
||||
// Update, but avoid duplicate (track_id, artist_id, role) - delete first any conflicting rows
|
||||
sqlx::query(
|
||||
r#"DELETE FROM track_artists
|
||||
WHERE artist_id = $2
|
||||
AND (track_id, role) IN (
|
||||
SELECT track_id, role FROM track_artists WHERE artist_id = $1
|
||||
)"#
|
||||
).bind(from_artist_id).bind(to_artist_id).execute(pool).await?;
|
||||
sqlx::query("UPDATE track_artists SET artist_id = $2 WHERE artist_id = $1")
|
||||
.bind(from_artist_id).bind(to_artist_id).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_duplicate_track_ids_in_albums(pool: &PgPool, source_album_id: i64, target_album_id: i64) -> Result<Vec<i64>, sqlx::Error> {
|
||||
let rows: Vec<(i64,)> = sqlx::query_as(
|
||||
r#"SELECT t1.id FROM tracks t1
|
||||
JOIN tracks t2 ON t1.file_hash = t2.file_hash AND t2.album_id = $2
|
||||
WHERE t1.album_id = $1"#
|
||||
).bind(source_album_id).bind(target_album_id).fetch_all(pool).await?;
|
||||
Ok(rows.into_iter().map(|(id,)| id).collect())
|
||||
}
|
||||
|
||||
pub async fn get_track_storage_path(pool: &PgPool, track_id: i64) -> Result<Option<String>, sqlx::Error> {
|
||||
let row: Option<(String,)> = sqlx::query_as("SELECT storage_path FROM tracks WHERE id = $1")
|
||||
.bind(track_id).fetch_optional(pool).await?;
|
||||
Ok(row.map(|(p,)| p))
|
||||
}
|
||||
|
||||
pub async fn delete_track(pool: &PgPool, track_id: i64) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("DELETE FROM track_artists WHERE track_id = $1").bind(track_id).execute(pool).await?;
|
||||
sqlx::query("DELETE FROM tracks WHERE id = $1").bind(track_id).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn move_tracks_to_album(pool: &PgPool, from_album_id: i64, to_album_id: i64) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("UPDATE tracks SET album_id = $2 WHERE album_id = $1")
|
||||
.bind(from_album_id).bind(to_album_id).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_album(pool: &PgPool, id: i64) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("DELETE FROM albums WHERE id = $1")
|
||||
.bind(id).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_track_storage_path(pool: &PgPool, track_id: i64, new_path: &str) -> Result<(), sqlx::Error> {
|
||||
sqlx::query("UPDATE tracks SET storage_path = $2 WHERE id = $1")
|
||||
.bind(track_id).bind(new_path).execute(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_albums_for_artist(pool: &PgPool, artist_id: i64) -> Result<Vec<Album>, sqlx::Error> {
|
||||
sqlx::query_as::<_, Album>("SELECT * FROM albums WHERE artist_id = $1 ORDER BY year NULLS LAST, name")
|
||||
.bind(artist_id).fetch_all(pool).await
|
||||
}
|
||||
|
||||
pub async fn find_album_by_artist_id_and_name(pool: &PgPool, artist_id: i64, name: &str) -> Result<Option<i64>, sqlx::Error> {
|
||||
let row: Option<(i64,)> = sqlx::query_as("SELECT id FROM albums WHERE artist_id = $1 AND name = $2")
|
||||
.bind(artist_id).bind(name).fetch_optional(pool).await?;
|
||||
Ok(row.map(|r| r.0))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user