From 29580088e6f5c87c365e8671ea8c52ce3de48895 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 7 Dec 2024 09:56:40 +0000 Subject: [PATCH] feat(nextcloud): add command 'nextcloud card get' --- src/lib.rs | 11 +++++ src/nextcloud/card.rs | 25 +++++++++- src/nextcloud/mod.rs | 13 ++++++ src/nextcloud/tests.rs | 46 ++++++++++++++++++- src/tests/responses/nextcloud-card-get.json | 51 +++++++++++++++++++++ 5 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 src/tests/responses/nextcloud-card-get.json diff --git a/src/lib.rs b/src/lib.rs index a51023c..2b0b301 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,6 +74,12 @@ enum NextcloudCardCommand { dump: bool, stack_id: i64, }, + Get { + #[clap(long, action = clap::ArgAction::SetTrue)] + dump: bool, + stack_id: i64, + card_id: i64, + }, } #[derive(Parser, Debug)] @@ -157,6 +163,11 @@ pub async fn run(ctx: Ctx) -> color_eyre::Result<()> { dump, stack_id, })) => nextcloud::card::list(ctx, dump, stack_id.into()).await, + Command::Nextcloud(NextcloudCommand::Card(NextcloudCardCommand::Get { + dump, + stack_id, + card_id, + })) => nextcloud::card::get(ctx, dump, stack_id.into(), card_id.into()).await, } } } diff --git a/src/nextcloud/card.rs b/src/nextcloud/card.rs index e6ed8ac..1d89fce 100644 --- a/src/nextcloud/card.rs +++ b/src/nextcloud/card.rs @@ -1,6 +1,8 @@ // -use crate::nextcloud::model::NextcloudStackId; -use crate::{p, FullCtx}; +use crate::{ + nextcloud::model::{NextcloudCardId, NextcloudStackId}, + p, FullCtx, +}; pub(crate) async fn list( ctx: FullCtx, @@ -22,3 +24,22 @@ pub(crate) async fn list( } Ok(()) } + +pub(crate) async fn get( + ctx: FullCtx, + dump: bool, + stack_id: NextcloudStackId, + card_id: NextcloudCardId, +) -> color_eyre::Result<()> { + let api_result = ctx + .deck_client() + .get_card(ctx.cfg.nextcloud.board_id, stack_id, card_id) + .await; + if dump { + p!(ctx.prt, "{}", api_result.text); + } else { + let card = api_result.result?; + p!(ctx.prt, "{}:{}", card.id, card.title); + } + Ok(()) +} diff --git a/src/nextcloud/mod.rs b/src/nextcloud/mod.rs index cc96d8e..c8923db 100644 --- a/src/nextcloud/mod.rs +++ b/src/nextcloud/mod.rs @@ -140,4 +140,17 @@ impl<'ctx> DeckClient<'ctx> { ) .await } + + async fn get_card( + &self, + board_id: NextcloudBoardId, + stack_id: NextcloudStackId, + card_id: model::NextcloudCardId, + ) -> APIResult { + self.request( + f!("boards/{board_id}/stacks/{stack_id}/cards/{card_id}"), + |net, url| net.get(url), + ) + .await + } } diff --git a/src/nextcloud/tests.rs b/src/nextcloud/tests.rs index 7daa5aa..6b43c20 100644 --- a/src/nextcloud/tests.rs +++ b/src/nextcloud/tests.rs @@ -100,11 +100,9 @@ mod config { } mod commands { - use super::*; mod board { - use super::*; #[tokio::test] @@ -287,6 +285,7 @@ mod commands { mod card { use super::*; + use crate::nextcloud::model::{Label, NextcloudLabelId}; #[tokio::test] async fn list() { @@ -333,6 +332,49 @@ mod commands { } ); } + + #[tokio::test] + async fn get() { + //given + let mock_net = kxio::net::mock(); + + mock_net + .on() + .get("https://host-name/index.php/apps/deck/api/v1.0/boards/2/stacks/1/cards/321") + .basic_auth("username", Some("password")) + .respond(StatusCode::OK) + .body(include_str!("../tests/responses/nextcloud-card-get.json")) + .expect("mock request"); + + let fs = given::a_filesystem(); + let ctx = given::a_full_context(mock_net, fs); + let deck_client = DeckClient::new(&ctx); + + //when + let result = deck_client + .get_card(ctx.cfg.nextcloud.board_id, 1.into(), 321.into()) + .await + .result + .expect("get stacks"); + + assert_eq!( + result, + Card { + id: NextcloudCardId::new(321), + title: NextcloudCardTitle::new("Breakfast: Cereal"), + description: Some(s!("")), + stack_id: NextcloudStackId::new(1), + order: NextcloudOrder::new(1), + archived: false, + due_date: None, + labels: Some(vec![Label { + id: NextcloudLabelId::new(1), + title: s!("Finished"), + color: s!("31CC7C") + }]) + } + ); + } } } diff --git a/src/tests/responses/nextcloud-card-get.json b/src/tests/responses/nextcloud-card-get.json new file mode 100644 index 0000000..7ed1ca5 --- /dev/null +++ b/src/tests/responses/nextcloud-card-get.json @@ -0,0 +1,51 @@ +{ + "id": 321, + "title": "Breakfast: Cereal", + "description": "", + "stackId": 1, + "type": "plain", + "lastModified": 1733515897, + "lastEditor": null, + "createdAt": 1733043461, + "labels": [ + { + "id": 1, + "title": "Finished", + "color": "31CC7C", + "boardId": 1, + "cardId": 321, + "lastModified": 1670965629, + "ETag": "983f87848dc9c18d0aee63e7ee0fc83f" + } + ], + "assignedUsers": [ + { + "id": 24, + "participant": { + "primaryKey": "pcampbell", + "uid": "pcampbell", + "displayname": "Paul Campbell", + "type": 0 + }, + "cardId": 321, + "type": 0 + } + ], + "attachments": [], + "attachmentCount": 0, + "owner": { + "primaryKey": "pcampbell", + "uid": "pcampbell", + "displayname": "Paul Campbell", + "type": 0 + }, + "order": 1, + "archived": false, + "done": null, + "duedate": null, + "deletedAt": 0, + "commentsUnread": 0, + "commentsCount": 0, + "ETag": "bf59162abcbc193b94349985122d7009", + "overdue": 0 +}