feat(cups): add CLI commands for cup management
- Add cup add/list/get/update/delete subcommands - Wire Cup variant into Commands enum and main dispatch
This commit is contained in:
parent
a6fad343fc
commit
4e644d5528
3 changed files with 101 additions and 1 deletions
|
|
@ -4,7 +4,7 @@ use brewlog::infrastructure::backup::{BackupData, BackupService};
|
|||
use brewlog::infrastructure::client::BrewlogClient;
|
||||
use brewlog::infrastructure::database::Database;
|
||||
use brewlog::presentation::cli::{
|
||||
Cli, Commands, ServeCommand, bags, brews, cafes, gear, roasters, roasts, tokens,
|
||||
Cli, Commands, ServeCommand, bags, brews, cafes, cups, gear, roasters, roasts, tokens,
|
||||
};
|
||||
use clap::Parser;
|
||||
|
||||
|
|
@ -47,6 +47,10 @@ async fn main() -> Result<()> {
|
|||
let client = BrewlogClient::from_base_url(&cli.api_url)?;
|
||||
cafes::run(&client, command).await
|
||||
}
|
||||
Commands::Cup { command } => {
|
||||
let client = BrewlogClient::from_base_url(&cli.api_url)?;
|
||||
cups::run(&client, command).await
|
||||
}
|
||||
Commands::Token { command } => {
|
||||
let client = BrewlogClient::from_base_url(&cli.api_url)?;
|
||||
tokens::run(&client, command).await
|
||||
|
|
|
|||
88
src/presentation/cli/cups.rs
Normal file
88
src/presentation/cli/cups.rs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
use anyhow::Result;
|
||||
use clap::{Args, Subcommand};
|
||||
|
||||
use super::macros::{define_delete_command, define_get_command};
|
||||
use super::print_json;
|
||||
use crate::domain::cups::{NewCup, UpdateCup};
|
||||
use crate::domain::ids::{CafeId, CupId, RoastId};
|
||||
use crate::infrastructure::client::BrewlogClient;
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum CupCommands {
|
||||
/// Add a new cup
|
||||
Add(AddCupCommand),
|
||||
/// List all cups
|
||||
List,
|
||||
/// Get a cup by ID
|
||||
Get(GetCupCommand),
|
||||
/// Update a cup
|
||||
Update(UpdateCupCommand),
|
||||
/// Delete a cup
|
||||
Delete(DeleteCupCommand),
|
||||
}
|
||||
|
||||
pub async fn run(client: &BrewlogClient, cmd: CupCommands) -> Result<()> {
|
||||
match cmd {
|
||||
CupCommands::Add(c) => add_cup(client, c).await,
|
||||
CupCommands::List => list_cups(client).await,
|
||||
CupCommands::Get(c) => get_cup(client, c).await,
|
||||
CupCommands::Update(c) => update_cup(client, c).await,
|
||||
CupCommands::Delete(c) => delete_cup(client, c).await,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct AddCupCommand {
|
||||
#[arg(long)]
|
||||
pub roast_id: i64,
|
||||
#[arg(long)]
|
||||
pub cafe_id: i64,
|
||||
#[arg(long)]
|
||||
pub notes: Option<String>,
|
||||
#[arg(long)]
|
||||
pub rating: Option<i32>,
|
||||
}
|
||||
|
||||
pub async fn add_cup(client: &BrewlogClient, command: AddCupCommand) -> Result<()> {
|
||||
let payload = NewCup {
|
||||
roast_id: RoastId::new(command.roast_id),
|
||||
cafe_id: CafeId::new(command.cafe_id),
|
||||
notes: command.notes,
|
||||
rating: command.rating,
|
||||
};
|
||||
|
||||
let cup = client.cups().create(&payload).await?;
|
||||
print_json(&cup)
|
||||
}
|
||||
|
||||
pub async fn list_cups(client: &BrewlogClient) -> Result<()> {
|
||||
let cups = client.cups().list().await?;
|
||||
print_json(&cups)
|
||||
}
|
||||
|
||||
define_get_command!(GetCupCommand, get_cup, CupId, cups);
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct UpdateCupCommand {
|
||||
#[arg(long)]
|
||||
pub id: i64,
|
||||
#[arg(long)]
|
||||
pub notes: Option<String>,
|
||||
#[arg(long)]
|
||||
pub rating: Option<i32>,
|
||||
}
|
||||
|
||||
pub async fn update_cup(client: &BrewlogClient, command: UpdateCupCommand) -> Result<()> {
|
||||
let payload = UpdateCup {
|
||||
notes: command.notes,
|
||||
rating: command.rating,
|
||||
};
|
||||
|
||||
let cup = client
|
||||
.cups()
|
||||
.update(CupId::new(command.id), &payload)
|
||||
.await?;
|
||||
print_json(&cup)
|
||||
}
|
||||
|
||||
define_delete_command!(DeleteCupCommand, delete_cup, CupId, cups, "cup");
|
||||
|
|
@ -2,6 +2,7 @@ pub mod backup;
|
|||
pub mod bags;
|
||||
pub mod brews;
|
||||
pub mod cafes;
|
||||
pub mod cups;
|
||||
pub mod gear;
|
||||
mod macros;
|
||||
pub mod roasters;
|
||||
|
|
@ -15,6 +16,7 @@ use bags::BagCommands;
|
|||
use brews::BrewCommands;
|
||||
use cafes::CafeCommands;
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use cups::CupCommands;
|
||||
use gear::GearCommands;
|
||||
use roasters::RoasterCommands;
|
||||
use roasts::RoastCommands;
|
||||
|
|
@ -76,6 +78,12 @@ pub enum Commands {
|
|||
command: CafeCommands,
|
||||
},
|
||||
|
||||
/// Manage cups
|
||||
Cup {
|
||||
#[command(subcommand)]
|
||||
command: CupCommands,
|
||||
},
|
||||
|
||||
/// Manage API tokens
|
||||
Token {
|
||||
#[command(subcommand)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue