Add artists::list_in_library
Artists with at least one wanted item, the same rule the web library list uses.
This commit is contained in:
1 parent
050e2b6947
commit
8b2bea3c5b
2 files changed
+65
-1
No files matched your search
+21
-1
@@ -1,8 +1,9 @@
|
||||
use chrono::Utc;
|
||||
use sea_orm::sea_query::Expr;
|
||||
use sea_orm::sea_query::{Expr, Query};
|
||||
use sea_orm::*;
|
||||
|
||||
use crate::entities::artist::{self, ActiveModel, Entity as Artists, Model as Artist};
|
||||
use crate::entities::wanted_item;
|
||||
use crate::error::{DbError, DbResult};
|
||||
|
||||
pub async fn upsert(
|
||||
@@ -93,6 +94,25 @@ pub async fn list(db: &DatabaseConnection, limit: u64, offset: u64) -> DbResult<
|
||||
.await?)
|
||||
}
|
||||
|
||||
/// Artists that belong in the library: those with at least one wanted item.
|
||||
/// An artist without one only exists because a track wanted under another
|
||||
/// artist was credited to them (a feature, a collaboration, or a bad match).
|
||||
pub async fn list_in_library(db: &DatabaseConnection) -> DbResult<Vec<Artist>> {
|
||||
Ok(Artists::find()
|
||||
.filter(
|
||||
artist::Column::Id.in_subquery(
|
||||
Query::select()
|
||||
.column(wanted_item::Column::ArtistId)
|
||||
.from(wanted_item::Entity)
|
||||
.and_where(wanted_item::Column::ArtistId.is_not_null())
|
||||
.to_owned(),
|
||||
),
|
||||
)
|
||||
.order_by_asc(Expr::cust("LOWER(name)"))
|
||||
.all(db)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn list_all(db: &DatabaseConnection) -> DbResult<Vec<Artist>> {
|
||||
Ok(Artists::find()
|
||||
.order_by_asc(Expr::cust("LOWER(name)"))
|
||||
|
||||
@@ -458,3 +458,47 @@ async fn test_wanted_find_by_track_id_and_list_by_artist() {
|
||||
let no_artist = queries::wanted::list_by_artist(conn, None).await.unwrap();
|
||||
assert_eq!(no_artist.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_artists_list_in_library() {
|
||||
let db = test_db().await;
|
||||
let conn = db.conn();
|
||||
|
||||
let wanted = queries::artists::upsert(conn, "Kesha", None).await.unwrap();
|
||||
queries::artists::upsert(conn, "Pitbull", None)
|
||||
.await
|
||||
.unwrap();
|
||||
queries::wanted::add(
|
||||
conn,
|
||||
queries::wanted::AddWantedItem {
|
||||
item_type: ItemType::Track,
|
||||
name: "Timber",
|
||||
musicbrainz_id: None,
|
||||
artist_id: Some(wanted.id),
|
||||
album_id: None,
|
||||
track_id: None,
|
||||
user_id: None,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
// An item with no artist must not pull in anything
|
||||
queries::wanted::add(
|
||||
conn,
|
||||
queries::wanted::AddWantedItem {
|
||||
item_type: ItemType::Track,
|
||||
name: "Unknown",
|
||||
musicbrainz_id: None,
|
||||
artist_id: None,
|
||||
album_id: None,
|
||||
track_id: None,
|
||||
user_id: None,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let listed = queries::artists::list_in_library(conn).await.unwrap();
|
||||
assert_eq!(listed.len(), 1);
|
||||
assert_eq!(listed[0].name, "Kesha");
|
||||
}
|
||||
Reference in new issue
Block a user