diff --git a/src/args.rs b/src/args.rs index d6a2f35..d070114 100644 --- a/src/args.rs +++ b/src/args.rs @@ -36,6 +36,14 @@ pub(crate) struct Opts { #[derive(Serialize, Subcommand)] pub(crate) enum Commands { + /// Albums related commands + #[serde(rename = "albums")] + Albums { + #[command(subcommand)] + #[serde(flatten)] + albums_command: AlbumsCommands, + }, + /// Assets related commands #[serde(rename = "assets")] Assets { @@ -68,6 +76,13 @@ pub(crate) enum Commands { }, } +#[derive(Serialize, Subcommand)] +pub(crate) enum AlbumsCommands { + /// List all albums + #[serde(rename = "list")] + List {}, +} + #[derive(Serialize, Subcommand)] pub(crate) enum AssetsCommands { /// Delete all assets diff --git a/src/commands/list_albums.rs b/src/commands/list_albums.rs new file mode 100644 index 0000000..faa785d --- /dev/null +++ b/src/commands/list_albums.rs @@ -0,0 +1,35 @@ +use crate::{ + actions::{action::Action, fetch_all_albums::FetchAllAlbums}, + context::Context, +}; +use chrono::{DateTime, Utc}; +use color_eyre::eyre::Result; +use tabled::{settings::Style, Table, Tabled}; +#[derive(Tabled)] +struct Album { + #[tabled(rename = "Name")] + name: String, + #[tabled(rename = "Assets count")] + assets_count: u32, + #[tabled(rename = "Updated at")] + updated_at: DateTime, +} + +pub async fn list_albums(ctx: Context) -> Result<()> { + let mut albums: Vec<_> = FetchAllAlbums::new(()) + .execute(&ctx) + .await? + .into_iter() + .map(|album| Album { + name: album.name, + updated_at: album.updated_at, + assets_count: album.assets_count, + }) + .collect(); + + albums.sort_by_key(|album| album.name.to_lowercase()); + + println!("{}", Table::new(albums).with(Style::rounded()),); + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 5bc09d8..039ac33 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,4 +1,5 @@ pub mod delete_assets; +pub mod list_albums; pub mod list_assets; pub mod missing_date_of_birth; pub mod scan_libraries; diff --git a/src/main.rs b/src/main.rs index 2823308..19e4997 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,12 @@ mod tests { } use crate::args::Commands; -use args::{AssetsCommands, LibrariesCommands, PeopleCommands, ServerCommands}; +use args::{AlbumsCommands, AssetsCommands, LibrariesCommands, PeopleCommands, ServerCommands}; use clap::Parser; use color_eyre::eyre::{Result, WrapErr}; use color_eyre::Section; use commands::delete_assets::delete_assets; +use commands::list_albums::list_albums; use commands::list_assets::list_assets; use commands::missing_date_of_birth::missing_date_of_birth; use commands::scan_libraries::scan_libraries; @@ -76,6 +77,9 @@ async fn main() -> Result<()> { validate_client_connection(&ctx.client).await?; match &args.command { + Commands::Albums { albums_command } => match albums_command { + AlbumsCommands::List {} => list_albums(ctx).await, + }, Commands::Assets { assets_command } => match assets_command { AssetsCommands::Delete { offline } => delete_assets(ctx, *offline).await, AssetsCommands::List { offline } => list_assets(ctx, *offline).await, diff --git a/src/models/album.rs b/src/models/album.rs index f151b5d..fe64982 100644 --- a/src/models/album.rs +++ b/src/models/album.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, NaiveDate, Utc}; use uuid::Uuid; use crate::types::AlbumResponseDto; @@ -6,6 +7,8 @@ use crate::types::AlbumResponseDto; pub struct Album { pub id: Uuid, pub name: String, + pub updated_at: DateTime, + pub assets_count: u32, } impl From for Album { @@ -13,6 +16,8 @@ impl From for Album { Self { id: Uuid::parse_str(&value.id).expect("Unable to parse an album's UUID"), name: value.album_name, + updated_at: value.updated_at, + assets_count: value.asset_count as u32, } } }