From 8598de6976d37e9c8a369501484f1985cc45992c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 17 Dec 2024 07:09:49 +0000 Subject: [PATCH] refactor: command 'nextcloud card create' --- src/nextcloud/card.rs | 54 ++++++++++++++---------------- src/nextcloud/client.rs | 40 +++++++++++++--------- src/nextcloud/tests/card/create.rs | 4 ++- src/nextcloud/tests/mod.rs | 2 +- 4 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/nextcloud/card.rs b/src/nextcloud/card.rs index 56ff012..1998243 100644 --- a/src/nextcloud/card.rs +++ b/src/nextcloud/card.rs @@ -2,7 +2,7 @@ use clap::Parser; use crate::execute::Execute; -use crate::nextcloud::model::{NextcloudCardDescription, NextcloudCardTitle, NextcloudLabelId}; +use crate::nextcloud::model::NextcloudLabelId; use crate::{ nextcloud::model::{NextcloudCardId, NextcloudStackId}, p, FullCtx, @@ -20,6 +20,7 @@ pub enum NextcloudCardCommand { Create { #[clap(long, action = clap::ArgAction::SetTrue)] dump: bool, + board_id: i64, stack_id: i64, #[clap(long)] title: String, @@ -66,20 +67,34 @@ impl Execute for NextcloudCardCommand { } Self::Create { dump, + board_id, stack_id, title, description, } => { - create( - ctx, - Create { - dump: *dump, - stack_id: (*stack_id).into(), - title: title.clone().into(), - description: description.clone().map(|d| d.into()), - }, - ) - .await + let api_result = ctx + .deck_client() + .create_card( + (*board_id).into(), + (*stack_id).into(), + title, + description.as_deref(), + ) + .await; + if *dump { + p!(ctx.prt, "{}", api_result.text); + } else { + let card = api_result.result?; + p!( + ctx.prt, + "{}:{}:{}:{}", + board_id, + stack_id, + card.id, + card.title + ); + } + Ok(()) } Self::AddLabel { dump, @@ -102,23 +117,6 @@ impl Execute for NextcloudCardCommand { } } -pub(crate) struct Create { - pub(crate) dump: bool, - pub(crate) stack_id: NextcloudStackId, - pub(crate) title: NextcloudCardTitle, - pub(crate) description: Option, -} -pub(crate) async fn create(ctx: &FullCtx, create: Create) -> color_eyre::Result<()> { - let api_result = ctx.deck_client().create_card(&create).await; - if create.dump { - p!(ctx.prt, "{}", api_result.text); - } else { - let card = api_result.result?; - p!(ctx.prt, "{}:{}", card.id, card.title); - } - Ok(()) -} - pub(crate) struct AddLabel { pub(crate) dump: bool, pub(crate) stack_id: NextcloudStackId, diff --git a/src/nextcloud/client.rs b/src/nextcloud/client.rs index 4a050d6..4da76e8 100644 --- a/src/nextcloud/client.rs +++ b/src/nextcloud/client.rs @@ -1,14 +1,21 @@ -use crate::api_result::APIResult; -use crate::nextcloud::card::{AddLabel, Create}; -use crate::nextcloud::model::{ - Board, Card, NextcloudBoardId, NextcloudCardId, NextcloudHostname, NextcloudPassword, - NextcloudStackId, NextcloudUsername, Stack, -}; -use crate::{f, FullCtx}; +// use bytes::Bytes; use kxio::net::{Net, ReqBuilder}; use serde_json::json; +use crate::{ + api_result::APIResult, + f, + nextcloud::{ + card::AddLabel, + model::{ + Board, Card, NextcloudBoardId, NextcloudCardId, NextcloudHostname, NextcloudPassword, + NextcloudStackId, NextcloudUsername, Stack, + }, + }, + FullCtx, +}; + pub(crate) struct DeckClient<'ctx> { ctx: &'ctx FullCtx, hostname: &'ctx NextcloudHostname, @@ -16,8 +23,6 @@ pub(crate) struct DeckClient<'ctx> { password: &'ctx NextcloudPassword, } -impl DeckClient<'_> {} - // Uses the API described here: https://deck.readthedocs.io/en/stable/API/#cards impl<'ctx> DeckClient<'ctx> { pub fn new(ctx: &'ctx FullCtx) -> Self { @@ -109,20 +114,23 @@ impl<'ctx> DeckClient<'ctx> { .await } - pub(crate) async fn create_card(&self, create: &Create) -> APIResult { + pub(crate) async fn create_card( + &self, + board_id: NextcloudBoardId, + stack_id: NextcloudStackId, + title: &str, + description: Option<&str>, + ) -> APIResult { let mut body = json!({ - "title": create.title, + "title": title, }); - if let Some(desc) = &create.description { + if let Some(desc) = &description { body["description"] = serde_json::Value::String(desc.to_string()); } self.request_with_body( - format!( - "boards/{}/stacks/{}/cards", - self.ctx.cfg.nextcloud.board_id, create.stack_id - ), + f!("boards/{board_id}/stacks/{stack_id}/cards"), body.to_string(), |net, url| net.post(url), ) diff --git a/src/nextcloud/tests/card/create.rs b/src/nextcloud/tests/card/create.rs index ba24ccf..1f7d299 100644 --- a/src/nextcloud/tests/card/create.rs +++ b/src/nextcloud/tests/card/create.rs @@ -48,6 +48,7 @@ async fn dump(ctx: FullCtx, stack_id: NextcloudStackId, #[case] description: Opt //when Command::Nextcloud(NextcloudCommand::Card(NextcloudCardCommand::Create { dump: true, + board_id: ctx.cfg.nextcloud.board_id.into(), stack_id: stack_id.into(), title: "my new card".to_string(), description, @@ -76,6 +77,7 @@ async fn no_dump(ctx: FullCtx, stack_id: NextcloudStackId, #[case] description: //when Command::Nextcloud(NextcloudCommand::Card(NextcloudCardCommand::Create { dump: false, + board_id: ctx.cfg.nextcloud.board_id.into(), stack_id: stack_id.into(), title: "my new card".to_string(), description, @@ -86,5 +88,5 @@ async fn no_dump(ctx: FullCtx, stack_id: NextcloudStackId, #[case] description: //then let output = prt.output(); - assert_peq!(output.trim(), "331:my new card"); + assert_peq!(output.trim(), "2:1:331:my new card"); } diff --git a/src/nextcloud/tests/mod.rs b/src/nextcloud/tests/mod.rs index 888c314..9eb7c13 100644 --- a/src/nextcloud/tests/mod.rs +++ b/src/nextcloud/tests/mod.rs @@ -15,7 +15,7 @@ use crate::nextcloud::NextcloudCommand; use crate::Command; use crate::{ nextcloud::{ - card::{AddLabel, Create}, + card::AddLabel, model::{ Card, Label, NextcloudBoardId, NextcloudCardId, NextcloudCardTitle, NextcloudETag, NextcloudHostname, NextcloudLabelId, NextcloudOrder, NextcloudPassword,