Introduces playlist header

This commit is contained in:
Antoine Gersant 2024-10-04 18:02:32 -07:00
parent 76535b2f87
commit b175e319b7
4 changed files with 31 additions and 12 deletions
src

View file

@ -1,6 +1,9 @@
use core::clone::Clone;
use sqlx::{Acquire, QueryBuilder, Sqlite};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;
use sqlx::{Acquire, QueryBuilder, Sqlite};
use crate::app::Error;
use crate::db::DB;
@ -10,12 +13,19 @@ pub struct Manager {
db: DB,
}
#[derive(Debug)]
pub struct PlaylistHeader {
pub name: String,
pub duration: Duration,
pub num_songs_by_genre: HashMap<String, u32>,
}
impl Manager {
pub fn new(db: DB) -> Self {
Self { db }
}
pub async fn list_playlists(&self, owner: &str) -> Result<Vec<String>, Error> {
pub async fn list_playlists(&self, owner: &str) -> Result<Vec<PlaylistHeader>, Error> {
let mut connection = self.db.connect().await?;
let user_id = sqlx::query_scalar!("SELECT id FROM users WHERE name = $1", owner)
@ -198,7 +208,7 @@ mod test {
.await
.unwrap();
assert_eq!(found_playlists.len(), 1);
assert_eq!(found_playlists[0], TEST_PLAYLIST_NAME);
assert_eq!(found_playlists[0].name, TEST_PLAYLIST_NAME);
}
#[tokio::test]

View file

@ -618,12 +618,9 @@ async fn get_search(
async fn get_playlists(
auth: Auth,
State(playlist_manager): State<playlist::Manager>,
) -> Result<Json<Vec<dto::ListPlaylistsEntry>>, APIError> {
let playlist_names = playlist_manager.list_playlists(auth.get_username()).await?;
let playlists: Vec<dto::ListPlaylistsEntry> = playlist_names
.into_iter()
.map(|p| dto::ListPlaylistsEntry { name: p })
.collect();
) -> Result<Json<Vec<dto::PlaylistHeader>>, APIError> {
let playlists = playlist_manager.list_playlists(auth.get_username()).await?;
let playlists = playlists.into_iter().map(|p| p.into()).collect();
Ok(Json(playlists))
}

View file

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use crate::app::{config, ddns, index, peaks, settings, thumbnail, user, vfs};
use crate::app::{config, ddns, index, peaks, playlist, settings, thumbnail, user, vfs};
use std::{collections::HashMap, convert::From, path::PathBuf};
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
@ -77,8 +77,20 @@ impl From<peaks::Peaks> for Peaks {
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ListPlaylistsEntry {
pub struct PlaylistHeader {
pub name: String,
pub num_songs_by_genre: HashMap<String, u32>,
pub duration: u64,
}
impl From<playlist::PlaylistHeader> for PlaylistHeader {
fn from(header: playlist::PlaylistHeader) -> Self {
Self {
name: header.name,
num_songs_by_genre: header.num_songs_by_genre,
duration: header.duration.as_secs(),
}
}
}
#[derive(Clone, Serialize, Deserialize)]

View file

@ -22,7 +22,7 @@ async fn list_playlists_golden_path() {
service.login().await;
let request = protocol::playlists();
let response = service
.fetch_json::<_, Vec<dto::ListPlaylistsEntry>>(&request)
.fetch_json::<_, Vec<dto::PlaylistHeader>>(&request)
.await;
assert_eq!(response.status(), StatusCode::OK);
}