Skip to content

Commit

Permalink
introduce cloneCard backendFunctionality
Browse files Browse the repository at this point in the history
Signed-off-by: grnd-alt <[email protected]>
  • Loading branch information
grnd-alt committed Nov 4, 2024
1 parent 59606ec commit 4212c10
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
['name' => 'card#read', 'url' => '/cards/{cardId}', 'verb' => 'GET'],
['name' => 'card#create', 'url' => '/cards', 'verb' => 'POST'],
['name' => 'card#update', 'url' => '/cards/{cardId}', 'verb' => 'PUT'],
['name' => 'card#clone', 'url' => '/cards/{cardId}/clone', 'verb' => 'POST'],
['name' => 'card#delete', 'url' => '/cards/{cardId}', 'verb' => 'DELETE'],
['name' => 'card#deleted', 'url' => '/{boardId}/cards/deleted', 'verb' => 'GET'],
['name' => 'card#rename', 'url' => '/cards/{cardId}/rename', 'verb' => 'PUT'],
Expand Down
9 changes: 9 additions & 0 deletions lib/Controller/CardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public function create($title, $stackId, $type = 'plain', $order = 999, string $
public function update($id, $title, $stackId, $type, $order, $description, $duedate, $deletedAt) {
return $this->cardService->update($id, $title, $stackId, $type, $this->userId, $description, $order, $duedate, $deletedAt);
}
/**
* @NoAdminRequired
* @param $cardId
* @param $targetStackId
* @return \OCP\AppFramework\Db\Entity
*/
public function clone(int $cardId, ?int $targetStackId = null) {
return $this->cardService->cloneCard($cardId, $targetStackId);
}

/**
* @NoAdminRequired
Expand Down
24 changes: 24 additions & 0 deletions lib/Service/CardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function __construct(
LoggerInterface $logger,
IRequest $request,
CardServiceValidator $cardServiceValidator,
private AssignmentService $assignmentService,
?string $userId,
) {
$this->cardMapper = $cardMapper;
Expand Down Expand Up @@ -390,6 +391,29 @@ public function update($id, $title, $stackId, $type, $owner, $description = '',
return $card;
}

public function cloneCard(int $id, ?int $targetStackId = null):Card {
$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_READ);
$originCard = $this->cardMapper->find($id);
if ($targetStackId == null) {
$targetStackId = $originCard->getStackId();
}
$this->permissionService->checkPermission($this->stackMapper, $targetStackId, Acl::PERMISSION_EDIT);
$newCard = $this->create($originCard->getTitle(), $targetStackId, $originCard->getType(), $originCard->getOrder(), $originCard->getOwner());
$boardId = $this->stackMapper->findBoardId($targetStackId);
foreach ($this->labelMapper->findAssignedLabelsForCard($id) as $label) {
if ($boardId != $this->stackMapper->findBoardId($originCard->getStackId())) {
$label = $this->labelService->cloneLabelIfNotExists($label->getId(), $boardId);
}
$this->assignLabel($newCard->getId(), $label->getId());
}
foreach ($this->assignedUsersMapper->findAll($id) as $assignement) {
$this->assignmentService->assignUser($newCard->getId(), $assignement->getParticipant());
}
$newCard->setDescription($originCard->getDescription());
$card = $this->enrichCards([$this->cardMapper->update($newCard)]);
return $card[0];
}

/**
* @param $id
* @param $title
Expand Down
12 changes: 12 additions & 0 deletions lib/Service/LabelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ public function create($title, $color, $boardId) {
return $this->labelMapper->insert($label);
}

public function cloneLabelIfNotExists(int $labelId, int $targetBoardId): Label {
$this->permissionService->checkPermission(null, $targetBoardId, Acl::PERMISSION_MANAGE);
$boardLabels = $this->boardService->find($targetBoardId)->getLabels();
$originLabel = $this->find($labelId);
$filteredValues = array_values(array_filter($boardLabels, fn ($item) => $item->getTitle() === $originLabel->getTitle()));
if (empty($filteredValues)) {
$label = $this->create($originLabel->getTitle(), $originLabel->getColor(), $targetBoardId);
return $label;
}
return $originLabel;
}

/**
* @param $id
* @return \OCP\AppFramework\Db\Entity
Expand Down
7 changes: 3 additions & 4 deletions src/CardCloneDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
:max-height="100"
label="title" />

<button :disabled="!isBoardAndStackChoosen" class="primary" @click="moveCard">
<button :disabled="!isBoardAndStackChoosen" class="primary" @click="cloneCard">
{{ t('deck', 'Clone card') }}
</button>
<button @click="modalShow = false">
Expand Down Expand Up @@ -89,9 +89,8 @@ export default {
return err
}
},
async moveCard() {
this.copiedCard = Object.assign({}, this.card)
this.copiedCard.stackId = this.selectedStack.id
async cloneCard() {
this.$store.dispatch('cloneCard', { cardId: this.card.id, targetStackId: this.selectedStack.id })
this.$store.dispatch('addCard', this.copiedCard)
this.modalShow = false
},
Expand Down
17 changes: 17 additions & 0 deletions src/services/CardApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ export class CardApi {
})
}

cloneCard(cardId, targetStackId) {
return axios.post(this.url(`/cards/${cardId}/clone`), {
targetStackId,
})
.then(
(response) => {
return Promise.resolve(response.data)
},
(err) => {
return Promise.reject(err)
},
)
.catch((err) => {
return Promise.reject(err)
})
}

deleteCard(cardId) {
return axios.delete(this.url(`/cards/${cardId}`))
.then(
Expand Down
5 changes: 5 additions & 0 deletions src/store/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ export default {
},
},
actions: {
async cloneCard({ commit }, { cardId, targetStackId }) {
const createdCard = await apiClient.cloneCard(cardId, targetStackId)
commit('addCard', createdCard)
return createdCard
},
async addCard({ commit }, card) {
const createdCard = await apiClient.addCard(card)
commit('addCard', createdCard)
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/Service/CardServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class CardServiceTest extends TestCase {
/** @var CardServiceValidator|MockObject */
private $cardServiceValidator;

/** @var AssignmentService|MockObject */
private $assignmentService;

public function setUp(): void {
parent::setUp();
$this->cardMapper = $this->createMock(CardMapper::class);
Expand All @@ -113,6 +116,7 @@ public function setUp(): void {
$this->logger = $this->createMock(LoggerInterface::class);
$this->request = $this->createMock(IRequest::class);
$this->cardServiceValidator = $this->createMock(CardServiceValidator::class);
$this->assignmentService = $this->createMock(AssignmentService::class);

$this->logger->expects($this->any())->method('error');

Expand All @@ -136,6 +140,7 @@ public function setUp(): void {
$this->logger,
$this->request,
$this->cardServiceValidator,
$this->assignmentService,
'user1'
);
}
Expand Down

0 comments on commit 4212c10

Please sign in to comment.