From a7115fc869a1b12cb5120e8d35bd48a5a391cd10 Mon Sep 17 00:00:00 2001 From: zkabic Date: Wed, 2 Aug 2023 10:06:57 +0200 Subject: [PATCH] Split logic in ensureThereIsNoItemOnPositionOrThrow409 function for add and move action --- .../Controller/ContentListItemController.php | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php b/src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php index 7a9ee04d0..c119c99a7 100644 --- a/src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php +++ b/src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php @@ -187,7 +187,12 @@ public function batchUpdateAction( $contentListItem = $this->findByContentOr404($list, $contentId); if ($position !== $contentListItem->getPosition()) { - $this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position, $isSticky); + $this->ensureThereIsNoItemOnPositionOrThrow409( + $listId, + $position, + $isSticky, + ContentListAction::ACTION_MOVE + ); $contentListItem->setPosition($position); $updated = true; } @@ -206,7 +211,12 @@ public function batchUpdateAction( break; case ContentListAction::ACTION_ADD: - $this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position); + $this->ensureThereIsNoItemOnPositionOrThrow409( + $listId, + $position, + $isSticky, + ContentListAction::ACTION_ADD + ); $object = $articleRepository->findOneById($contentId); $contentListItem = $this->contentListService->addArticleToContentList($list, $object, $position, $isSticky); @@ -269,15 +279,23 @@ private function findOr404($listId, $id): ContentListItemInterface { return $listItem; } - private function ensureThereIsNoItemOnPositionOrThrow409(int $listId, int $position, bool $isSticky = false): void { - $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); + private function ensureThereIsNoItemOnPositionOrThrow409( + int $listId, + int $position, + bool $isSticky, + string $action): void { + $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); if (!$existingContentListItem && !$isSticky) { return; } - if ($isSticky || ($existingContentListItem && $existingContentListItem->isSticky())) { - throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); - } + if ($existingContentListItem && $existingContentListItem->isSticky()) { + throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); + } + + if ($action === ContentListAction::ACTION_MOVE && $isSticky) { + throw new ConflictHttpException('Cannot move pinned item. Unpin it first.'); + } } }