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, });