refactor: command 'nextcloud card create'
This commit is contained in:
parent
7cf6f9bd94
commit
8598de6976
4 changed files with 54 additions and 46 deletions
|
@ -2,7 +2,7 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
use crate::execute::Execute;
|
use crate::execute::Execute;
|
||||||
use crate::nextcloud::model::{NextcloudCardDescription, NextcloudCardTitle, NextcloudLabelId};
|
use crate::nextcloud::model::NextcloudLabelId;
|
||||||
use crate::{
|
use crate::{
|
||||||
nextcloud::model::{NextcloudCardId, NextcloudStackId},
|
nextcloud::model::{NextcloudCardId, NextcloudStackId},
|
||||||
p, FullCtx,
|
p, FullCtx,
|
||||||
|
@ -20,6 +20,7 @@ pub enum NextcloudCardCommand {
|
||||||
Create {
|
Create {
|
||||||
#[clap(long, action = clap::ArgAction::SetTrue)]
|
#[clap(long, action = clap::ArgAction::SetTrue)]
|
||||||
dump: bool,
|
dump: bool,
|
||||||
|
board_id: i64,
|
||||||
stack_id: i64,
|
stack_id: i64,
|
||||||
#[clap(long)]
|
#[clap(long)]
|
||||||
title: String,
|
title: String,
|
||||||
|
@ -66,20 +67,34 @@ impl Execute for NextcloudCardCommand {
|
||||||
}
|
}
|
||||||
Self::Create {
|
Self::Create {
|
||||||
dump,
|
dump,
|
||||||
|
board_id,
|
||||||
stack_id,
|
stack_id,
|
||||||
title,
|
title,
|
||||||
description,
|
description,
|
||||||
} => {
|
} => {
|
||||||
create(
|
let api_result = ctx
|
||||||
ctx,
|
.deck_client()
|
||||||
Create {
|
.create_card(
|
||||||
dump: *dump,
|
(*board_id).into(),
|
||||||
stack_id: (*stack_id).into(),
|
(*stack_id).into(),
|
||||||
title: title.clone().into(),
|
title,
|
||||||
description: description.clone().map(|d| d.into()),
|
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 {
|
Self::AddLabel {
|
||||||
dump,
|
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) struct AddLabel {
|
||||||
pub(crate) dump: bool,
|
pub(crate) dump: bool,
|
||||||
pub(crate) stack_id: NextcloudStackId,
|
pub(crate) stack_id: NextcloudStackId,
|
||||||
|
|
|
@ -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 bytes::Bytes;
|
||||||
use kxio::net::{Net, ReqBuilder};
|
use kxio::net::{Net, ReqBuilder};
|
||||||
use serde_json::json;
|
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> {
|
pub(crate) struct DeckClient<'ctx> {
|
||||||
ctx: &'ctx FullCtx,
|
ctx: &'ctx FullCtx,
|
||||||
hostname: &'ctx NextcloudHostname,
|
hostname: &'ctx NextcloudHostname,
|
||||||
|
@ -16,8 +23,6 @@ pub(crate) struct DeckClient<'ctx> {
|
||||||
password: &'ctx NextcloudPassword,
|
password: &'ctx NextcloudPassword,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeckClient<'_> {}
|
|
||||||
|
|
||||||
// Uses the API described here: https://deck.readthedocs.io/en/stable/API/#cards
|
// Uses the API described here: https://deck.readthedocs.io/en/stable/API/#cards
|
||||||
impl<'ctx> DeckClient<'ctx> {
|
impl<'ctx> DeckClient<'ctx> {
|
||||||
pub fn new(ctx: &'ctx FullCtx) -> Self {
|
pub fn new(ctx: &'ctx FullCtx) -> Self {
|
||||||
|
@ -109,20 +114,23 @@ impl<'ctx> DeckClient<'ctx> {
|
||||||
.await
|
.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!({
|
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());
|
body["description"] = serde_json::Value::String(desc.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.request_with_body(
|
self.request_with_body(
|
||||||
format!(
|
f!("boards/{board_id}/stacks/{stack_id}/cards"),
|
||||||
"boards/{}/stacks/{}/cards",
|
|
||||||
self.ctx.cfg.nextcloud.board_id, create.stack_id
|
|
||||||
),
|
|
||||||
body.to_string(),
|
body.to_string(),
|
||||||
|net, url| net.post(url),
|
|net, url| net.post(url),
|
||||||
)
|
)
|
||||||
|
|
|
@ -48,6 +48,7 @@ async fn dump(ctx: FullCtx, stack_id: NextcloudStackId, #[case] description: Opt
|
||||||
//when
|
//when
|
||||||
Command::Nextcloud(NextcloudCommand::Card(NextcloudCardCommand::Create {
|
Command::Nextcloud(NextcloudCommand::Card(NextcloudCardCommand::Create {
|
||||||
dump: true,
|
dump: true,
|
||||||
|
board_id: ctx.cfg.nextcloud.board_id.into(),
|
||||||
stack_id: stack_id.into(),
|
stack_id: stack_id.into(),
|
||||||
title: "my new card".to_string(),
|
title: "my new card".to_string(),
|
||||||
description,
|
description,
|
||||||
|
@ -76,6 +77,7 @@ async fn no_dump(ctx: FullCtx, stack_id: NextcloudStackId, #[case] description:
|
||||||
//when
|
//when
|
||||||
Command::Nextcloud(NextcloudCommand::Card(NextcloudCardCommand::Create {
|
Command::Nextcloud(NextcloudCommand::Card(NextcloudCardCommand::Create {
|
||||||
dump: false,
|
dump: false,
|
||||||
|
board_id: ctx.cfg.nextcloud.board_id.into(),
|
||||||
stack_id: stack_id.into(),
|
stack_id: stack_id.into(),
|
||||||
title: "my new card".to_string(),
|
title: "my new card".to_string(),
|
||||||
description,
|
description,
|
||||||
|
@ -86,5 +88,5 @@ async fn no_dump(ctx: FullCtx, stack_id: NextcloudStackId, #[case] description:
|
||||||
|
|
||||||
//then
|
//then
|
||||||
let output = prt.output();
|
let output = prt.output();
|
||||||
assert_peq!(output.trim(), "331:my new card");
|
assert_peq!(output.trim(), "2:1:331:my new card");
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ use crate::nextcloud::NextcloudCommand;
|
||||||
use crate::Command;
|
use crate::Command;
|
||||||
use crate::{
|
use crate::{
|
||||||
nextcloud::{
|
nextcloud::{
|
||||||
card::{AddLabel, Create},
|
card::AddLabel,
|
||||||
model::{
|
model::{
|
||||||
Card, Label, NextcloudBoardId, NextcloudCardId, NextcloudCardTitle, NextcloudETag,
|
Card, Label, NextcloudBoardId, NextcloudCardId, NextcloudCardTitle, NextcloudETag,
|
||||||
NextcloudHostname, NextcloudLabelId, NextcloudOrder, NextcloudPassword,
|
NextcloudHostname, NextcloudLabelId, NextcloudOrder, NextcloudPassword,
|
||||||
|
|
Loading…
Reference in a new issue