Skip to main content

jellytau_lib/commands/
playlist.rs

1//! Tauri commands for playlist management
2//! Uses handle-based system: UUID -> Arc<HybridRepository>
3//!
4//! TRACES: UR-014 | JA-019, JA-020
5
6use log::debug;
7use tauri::State;
8
9use super::repository::RepositoryManagerWrapper;
10use crate::repository::{types::*, MediaRepository};
11
12/// Create a new playlist
13#[tauri::command]
14#[specta::specta]
15pub async fn playlist_create(
16    manager: State<'_, RepositoryManagerWrapper>,
17    handle: String,
18    name: String,
19    item_ids: Option<Vec<String>>,
20) -> Result<PlaylistCreatedResult, String> {
21    debug!("[PLAYLIST] create called: name={}", name);
22    let repo = manager.0.get(&handle).ok_or("Repository not found")?;
23    let ids = item_ids.unwrap_or_default();
24    repo.as_ref()
25        .create_playlist(&name, &ids)
26        .await
27        .map_err(|e| format!("{:?}", e))
28}
29
30/// Delete a playlist
31#[tauri::command]
32#[specta::specta]
33pub async fn playlist_delete(
34    manager: State<'_, RepositoryManagerWrapper>,
35    handle: String,
36    playlist_id: String,
37) -> Result<(), String> {
38    debug!("[PLAYLIST] delete called: id={}", playlist_id);
39    let repo = manager.0.get(&handle).ok_or("Repository not found")?;
40    repo.as_ref()
41        .delete_playlist(&playlist_id)
42        .await
43        .map_err(|e| format!("{:?}", e))
44}
45
46/// Rename a playlist
47#[tauri::command]
48#[specta::specta]
49pub async fn playlist_rename(
50    manager: State<'_, RepositoryManagerWrapper>,
51    handle: String,
52    playlist_id: String,
53    name: String,
54) -> Result<(), String> {
55    debug!(
56        "[PLAYLIST] rename called: id={}, name={}",
57        playlist_id, name
58    );
59    let repo = manager.0.get(&handle).ok_or("Repository not found")?;
60    repo.as_ref()
61        .rename_playlist(&playlist_id, &name)
62        .await
63        .map_err(|e| format!("{:?}", e))
64}
65
66/// Get playlist items with PlaylistItemId
67#[tauri::command]
68#[specta::specta]
69pub async fn playlist_get_items(
70    manager: State<'_, RepositoryManagerWrapper>,
71    handle: String,
72    playlist_id: String,
73) -> Result<Vec<PlaylistEntry>, String> {
74    debug!("[PLAYLIST] get_items called: id={}", playlist_id);
75    let repo = manager.0.get(&handle).ok_or("Repository not found")?;
76    repo.as_ref()
77        .get_playlist_items(&playlist_id)
78        .await
79        .map_err(|e| format!("{:?}", e))
80}
81
82/// Add items to a playlist
83#[tauri::command]
84#[specta::specta]
85pub async fn playlist_add_items(
86    manager: State<'_, RepositoryManagerWrapper>,
87    handle: String,
88    playlist_id: String,
89    item_ids: Vec<String>,
90) -> Result<(), String> {
91    debug!(
92        "[PLAYLIST] add_items called: id={}, count={}",
93        playlist_id,
94        item_ids.len()
95    );
96    let repo = manager.0.get(&handle).ok_or("Repository not found")?;
97    repo.as_ref()
98        .add_to_playlist(&playlist_id, &item_ids)
99        .await
100        .map_err(|e| format!("{:?}", e))
101}
102
103/// Remove items from a playlist (uses PlaylistItemId entry IDs, NOT media item IDs)
104#[tauri::command]
105#[specta::specta]
106pub async fn playlist_remove_items(
107    manager: State<'_, RepositoryManagerWrapper>,
108    handle: String,
109    playlist_id: String,
110    entry_ids: Vec<String>,
111) -> Result<(), String> {
112    debug!(
113        "[PLAYLIST] remove_items called: id={}, count={}",
114        playlist_id,
115        entry_ids.len()
116    );
117    let repo = manager.0.get(&handle).ok_or("Repository not found")?;
118    repo.as_ref()
119        .remove_from_playlist(&playlist_id, &entry_ids)
120        .await
121        .map_err(|e| format!("{:?}", e))
122}
123
124/// Move a playlist item to a new position
125#[tauri::command]
126#[specta::specta]
127pub async fn playlist_move_item(
128    manager: State<'_, RepositoryManagerWrapper>,
129    handle: String,
130    playlist_id: String,
131    item_id: String,
132    new_index: u32,
133) -> Result<(), String> {
134    debug!(
135        "[PLAYLIST] move_item called: playlist={}, item={}, index={}",
136        playlist_id, item_id, new_index
137    );
138    let repo = manager.0.get(&handle).ok_or("Repository not found")?;
139    repo.as_ref()
140        .move_playlist_item(&playlist_id, &item_id, new_index)
141        .await
142        .map_err(|e| format!("{:?}", e))
143}