feat: add rest api for music tracks
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/claude-agent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::Track;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Aggregator {
|
||||
tracks: Vec<Track>,
|
||||
}
|
||||
|
||||
impl Aggregator {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn add_track(&mut self, track: Track) -> Track {
|
||||
self.tracks.push(track.clone());
|
||||
track
|
||||
}
|
||||
|
||||
pub fn get_all(&self) -> &[Track] {
|
||||
&self.tracks
|
||||
}
|
||||
|
||||
pub fn get_by_id(&self, id: Uuid) -> Option<&Track> {
|
||||
self.tracks.iter().find(|t| t.id == id)
|
||||
}
|
||||
|
||||
pub fn search_by_artist(&self, artist: &str) -> Vec<&Track> {
|
||||
let artist_lower = artist.to_lowercase();
|
||||
self.tracks
|
||||
.iter()
|
||||
.filter(|t| t.artist.to_lowercase().contains(&artist_lower))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, id: Uuid) -> bool {
|
||||
let len_before = self.tracks.len();
|
||||
self.tracks.retain(|t| t.id != id);
|
||||
self.tracks.len() != len_before
|
||||
}
|
||||
|
||||
pub fn total_duration(&self) -> u32 {
|
||||
self.tracks.iter().map(|t| t.duration_secs).sum()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user