refactor: command 'nextcloud card create'
Some checks failed
Release Please / Release-plz (push) Failing after 18s
Test / build (map[name:nightly]) (push) Successful in 2m25s
Test / build (map[name:stable]) (push) Successful in 2m12s

This commit is contained in:
Paul Campbell 2024-12-17 07:09:49 +00:00
parent 7cf6f9bd94
commit 8598de6976
4 changed files with 54 additions and 46 deletions

View file

@ -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()),
},
let api_result = ctx
.deck_client()
.create_card(
(*board_id).into(),
(*stack_id).into(),
title,
description.as_deref(),
)
.await
.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<NextcloudCardDescription>,
}
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,

View file

@ -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<Card> {
pub(crate) async fn create_card(
&self,
board_id: NextcloudBoardId,
stack_id: NextcloudStackId,
title: &str,
description: Option<&str>,
) -> APIResult<Card> {
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),
)

View file

@ -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");
}

View file

@ -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,