From 7914e86c6242bb9865b4bc4ca3d933ae26153a81 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 8 Dec 2024 19:31:10 +0000 Subject: [PATCH] feat(nextcloud); add command 'nextcloud card add-label' --- src/nextcloud/card.rs | 47 +++++++++++++++++++++++++++++++++++++---- src/nextcloud/client.rs | 23 +++++++++++++++++--- src/nextcloud/tests.rs | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 7 deletions(-) diff --git a/src/nextcloud/card.rs b/src/nextcloud/card.rs index eb00021..888ad7a 100644 --- a/src/nextcloud/card.rs +++ b/src/nextcloud/card.rs @@ -2,6 +2,7 @@ use clap::Parser; use crate::execute::Execute; +use crate::nextcloud::model::NextcloudLabelId; use crate::{ nextcloud::model::{NextcloudCardId, NextcloudStackId}, p, FullCtx, @@ -29,6 +30,14 @@ pub(crate) enum NextcloudCardCommand { #[clap(long)] description: Option, }, + + AddLabel { + #[clap(long, action = clap::ArgAction::SetTrue)] + dump: bool, + stack_id: i64, + card_id: i64, + label_id: i64, + }, } impl Execute for NextcloudCardCommand { @@ -67,6 +76,23 @@ impl Execute for NextcloudCardCommand { ) .await } + Self::AddLabel { + dump, + stack_id, + card_id, + label_id, + } => { + add_label( + ctx, + AddLabel { + dump, + stack_id: NextcloudStackId::from(stack_id), + card_id: NextcloudCardId::from(card_id), + label_id: NextcloudLabelId::from(label_id), + }, + ) + .await + } } } } @@ -118,13 +144,26 @@ pub(crate) struct Create { pub(crate) description: Option, } pub(crate) async fn create(ctx: FullCtx, create: Create) -> color_eyre::Result<()> { - let dc = ctx.deck_client(); - let apiresult = dc.create_card(&create).await; + let api_result = ctx.deck_client().create_card(&create).await; if create.dump { - p!(ctx.prt, "{}", apiresult.text); + p!(ctx.prt, "{}", api_result.text); } else { - let card = apiresult.result?; + 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, + pub(crate) card_id: NextcloudCardId, + pub(crate) label_id: NextcloudLabelId, +} +async fn add_label(ctx: FullCtx, add_label: AddLabel) -> color_eyre::Result<()> { + let api_result = ctx.deck_client().add_label_to_card(&add_label).await; + if add_label.dump { + p!(ctx.prt, "{}", api_result.text); + } + Ok(()) +} diff --git a/src/nextcloud/client.rs b/src/nextcloud/client.rs index 2a4a361..64952ea 100644 --- a/src/nextcloud/client.rs +++ b/src/nextcloud/client.rs @@ -1,5 +1,5 @@ use crate::api_result::APIResult; -use crate::nextcloud::card::Create; +use crate::nextcloud::card::{AddLabel, Create}; use crate::nextcloud::model::{ Board, Card, NextcloudBoardId, NextcloudCardId, NextcloudHostname, NextcloudPassword, NextcloudStackId, NextcloudUsername, Stack, @@ -7,6 +7,7 @@ use crate::nextcloud::model::{ use crate::{f, FullCtx}; use bytes::Bytes; use kxio::net::{Net, ReqBuilder}; +use serde_json::json; pub(crate) struct DeckClient<'ctx> { ctx: &'ctx FullCtx, @@ -70,6 +71,22 @@ impl<'ctx> DeckClient<'ctx> { .await } + pub(crate) async fn add_label_to_card(&self, add_label: &AddLabel) -> APIResult<()> { + let board_id = self.ctx.cfg.nextcloud.board_id; + let stack_id = add_label.stack_id; + let card_id = add_label.card_id; + let label_id = add_label.label_id; + self.request_with_body( + f!("boards/{board_id}/stacks/{stack_id}/cards/{card_id}/assignLabel"), + json!({ + "labelId": label_id + }) + .to_string(), + |net, url| net.put(url), + ) + .await + } + pub(crate) async fn get_boards(&self) -> APIResult> { self.request("boards", |net, url| net.get(url)).await } @@ -82,7 +99,7 @@ impl<'ctx> DeckClient<'ctx> { // pub(crate) async fn create_board(&self, title: &str, color: &str) -> APIResult { // self.request_with_body( // "boards", - // serde_json::json!({ + // json!({ // "title": title, // "color": color // }) @@ -109,7 +126,7 @@ impl<'ctx> DeckClient<'ctx> { } pub(crate) async fn create_card(&self, create: &Create) -> APIResult { - let mut body = serde_json::json!({ + let mut body = json!({ "title": create.title, }); diff --git a/src/nextcloud/tests.rs b/src/nextcloud/tests.rs index 545f4fd..3b3c428 100644 --- a/src/nextcloud/tests.rs +++ b/src/nextcloud/tests.rs @@ -284,6 +284,7 @@ mod commands { use serde_json::json; use super::*; + use crate::nextcloud::card::AddLabel; use crate::nextcloud::{ card::Create, model::{Label, NextcloudLabelId}, @@ -432,6 +433,45 @@ mod commands { } ); } + + #[tokio::test] + async fn add_label() { + //given + let mock_net = kxio::net::mock(); + + mock_net + .on() + .put("https://host-name/index.php/apps/deck/api/v1.0/boards/2/stacks/1/cards/321/assignLabel") + .basic_auth("username", Some("password")) + .header("accept", "application/json") + .header("content-type", "application/json") + .body( + json!({ + "labelId":400, + }) + .to_string(), + ) + .respond(StatusCode::OK) + .mock() + .expect("mock request"); + + // let fs = given::a_filesystem(); + let ctx = given::a_full_context(mock_net); + + //when + let result = ctx + .deck_client() + .add_label_to_card(&AddLabel { + dump: false, + stack_id: 1.into(), + card_id: 321.into(), + label_id: 400.into(), + }) + .await; + + //then + assert!(result.result.is_ok()); + } } }