This commit is contained in:
JMARyA 2024-10-06 17:38:27 +02:00
parent 334790da70
commit 0554040341
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -198,17 +198,8 @@ impl Track {
/// ///
/// A vector of `Track` objects representing the latest tracks for the given user. /// A vector of `Track` objects representing the latest tracks for the given user.
pub async fn get_latest_of_user(u: &User) -> Vec<Self> { pub async fn get_latest_of_user(u: &User) -> Vec<Self> {
let latest_events = Event::get_latest_events_of(u).await; sqlx::query_as("SELECT DISTINCT t.* FROM track t JOIN events e ON t.id = e.track WHERE e.user = $1 ORDER BY e.time DESC")
let mut ids = HashSet::new(); .bind(&u.username)
for event in latest_events {
if !ids.contains(&event.track) {
ids.insert(event.track.clone());
}
}
sqlx::query_as("SELECT * FROM track WHERE id = ANY($1)")
.bind(ids.into_iter().collect::<Vec<_>>())
.fetch_all(get_pg!()) .fetch_all(get_pg!())
.await .await
.unwrap() .unwrap()