This commit is contained in:
JMARyA 2024-10-07 08:21:03 +02:00
parent 0554040341
commit 43abe4300f
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::prelude::FromRow;
use std::{collections::HashSet, str::FromStr};
use std::str::FromStr;
use crate::{
get_pg,
@ -9,7 +9,7 @@ use crate::{
route::{to_uuid, ToAPI},
};
use super::{event::Event, metadata::AudioMetadata, user::User};
use super::{metadata::AudioMetadata, user::User};
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct Track {
@ -198,11 +198,22 @@ impl Track {
///
/// A vector of `Track` objects representing the latest tracks for the given user.
pub async fn get_latest_of_user(u: &User) -> Vec<Self> {
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 res: Vec<(uuid::Uuid, String, String, chrono::DateTime<chrono::Utc>, Option<uuid::Uuid>, Option<uuid::Uuid>, Option<serde_json::Value>, chrono::DateTime<chrono::Utc>)> = sqlx::query_as("SELECT DISTINCT(t.*), e.time FROM track t JOIN events e ON t.id = e.track WHERE e.user = $1 ORDER BY e.time DESC")
.bind(&u.username)
.fetch_all(get_pg!())
.await
.unwrap()
.unwrap();
res.into_iter()
.map(|x| Self {
id: x.0,
path: x.1,
title: x.2,
date_added: x.3,
album: x.4,
artist: x.5,
meta: x.6,
})
.collect()
}
/// Transcodes audio to OPUS format with the specified bitrate.