Return ArtistData from top_artists

This commit is contained in:
Ethan Girouard 2024-11-04 00:29:53 -05:00
parent 35abbe19ee
commit 414489d1be
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146
2 changed files with 13 additions and 2 deletions

View File

@ -4,6 +4,7 @@ use server_fn::codec::{MultipartData, MultipartFormData};
use cfg_if::cfg_if; use cfg_if::cfg_if;
use crate::songdata::SongData; use crate::songdata::SongData;
use crate::artistdata::ArtistData;
use crate::models::Artist; use crate::models::Artist;
use std::time::SystemTime; use std::time::SystemTime;
@ -260,7 +261,7 @@ pub async fn top_songs(for_user_id: i32, start_date: SystemTime, end_date: Syste
/// Returns a list of tuples with the play count and the artist data, sorted by play count (most played first). /// Returns a list of tuples with the play count and the artist data, sorted by play count (most played first).
#[server(endpoint = "/profile/top_artists")] #[server(endpoint = "/profile/top_artists")]
pub async fn top_artists(for_user_id: i32, start_date: SystemTime, end_date: SystemTime, limit: Option<i64>) pub async fn top_artists(for_user_id: i32, start_date: SystemTime, end_date: SystemTime, limit: Option<i64>)
-> Result<Vec<(i64, Artist)>, ServerFnError> -> Result<Vec<(i64, ArtistData)>, ServerFnError>
{ {
let mut db_con = get_db_conn(); let mut db_con = get_db_conn();
@ -288,5 +289,13 @@ pub async fn top_artists(for_user_id: i32, start_date: SystemTime, end_date: Sys
.load(&mut db_con)? .load(&mut db_con)?
}; };
Ok(artist_counts) let artist_data: Vec<(i64, ArtistData)> = artist_counts.into_iter().map(|(plays, artist)| {
(plays, ArtistData {
id: artist.id.unwrap(),
name: artist.name,
image_path: format!("/assets/images/artists/{}.webp", artist.id.unwrap()),
})
}).collect();
Ok(artist_data)
} }

View File

@ -1,8 +1,10 @@
use crate::components::dashboard_tile::DashboardTile; use crate::components::dashboard_tile::DashboardTile;
use serde::{Serialize, Deserialize};
/// Holds information about an artist /// Holds information about an artist
/// ///
/// Intended to be used in the front-end /// Intended to be used in the front-end
#[derive(Serialize, Deserialize)]
pub struct ArtistData { pub struct ArtistData {
/// Artist id /// Artist id
pub id: i32, pub id: i32,