Skip to content

Commit

Permalink
Content list webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanJelicSF committed Mar 20, 2024
1 parent d5fed2d commit 987f3e9
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
use SWP\Bundle\ContentListBundle\Form\Type\ContentListItemsType;
use SWP\Bundle\ContentListBundle\Services\ContentListServiceInterface;
use SWP\Bundle\CoreBundle\Controller\ContentListController;
use SWP\Bundle\CoreBundle\Form\Type\ContentListItemType;
use SWP\Bundle\CoreBundle\Model\ContentListInterface;
use SWP\Bundle\CoreBundle\Controller\ContentListController;
use SWP\Bundle\CoreBundle\Model\ContentListItemInterface;
use SWP\Bundle\CoreBundle\Repository\ArticleRepositoryInterface;
use SWP\Bundle\CoreBundle\Repository\ContentListItemRepositoryInterface;
Expand Down Expand Up @@ -56,21 +56,13 @@ class ContentListItemController extends AbstractController {
* @param ContentListServiceInterface $contentListService
* @param EventDispatcherInterface $eventDispatcher
*/

public function __construct(
EntityManagerInterface $entityManager,
public function __construct(
ContentListItemRepositoryInterface $contentListItemRepository,
ContentListServiceInterface $contentListService,
EntityManagerInterface $entityManager,
EventDispatcherInterface $eventDispatcher) {
ContentListServiceInterface $contentListService,
$this->contentListItemRepository = $contentListItemRepository;
EventDispatcherInterface $eventDispatcher,
$this->entityManager = $entityManager;
string $invalidationCacheUrl,
$this->contentListService = $contentListService;
string $invalidationToken,
$this->eventDispatcher = $eventDispatcher;
) {
$this->contentListItemRepository = $contentListItemRepository;
$this->entityManager = $entityManager;
Expand All @@ -79,7 +71,7 @@ public function __construct(
$this->invalidationCacheUrl = $invalidationCacheUrl;
$this->invalidationToken = $invalidationToken;
}
}


/**
* @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"})
Expand Down Expand Up @@ -152,7 +144,7 @@ public function updateAction(Request $request, FormFactoryInterface $formFactory
}

$this->entityManager->flush();
ContentListController::invalidateCache(
ContentListController::invalidateCache(
$this->invalidationCacheUrl,
$this->invalidationToken,
[
Expand Down Expand Up @@ -208,20 +200,40 @@ public function batchUpdateAction(

switch ($item->getAction()) {
case ContentListAction::ACTION_MOVE:
$updated = false;
$contentListItem = $this->findByContentOr404($list, $contentId);

$this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position, $isSticky);

$contentListItem->setPosition($position);
$this->contentListService->toggleStickOnItemPosition($contentListItem, $isSticky, $position);
if ($position !== $contentListItem->getPosition()) {
$this->ensureThereIsNoItemOnPositionOrThrow409(
$listId,
$position,
$isSticky,
ContentListAction::ACTION_MOVE
);
$contentListItem->setPosition($position);
$updated = true;
}

if ($isSticky !== $contentListItem->getStickyPosition()) {
$this->contentListService->toggleStickOnItemPosition($contentListItem, $isSticky, $position);
$updated = true;
}

if ($updated) {
$list->setUpdatedAt(new DateTime('now'));
$this->entityManager->flush();
}

$list->setUpdatedAt(new DateTime('now'));
$this->entityManager->flush();
$updatedArticles[$contentId] = $contentListItem->getContent();

break;
case ContentListAction::ACTION_ADD:
$this->ensureThereIsNoItemOnPositionOrThrow409($listId, $position, $isSticky);
$this->ensureThereIsNoItemOnPositionOrThrow409(
$listId,
$position,
$isSticky,
ContentListAction::ACTION_ADD
);

$object = $articleRepository->findOneById($contentId);
$contentListItem = $this->contentListService->addArticleToContentList($list, $object, $position, $isSticky);
Expand Down Expand Up @@ -249,7 +261,7 @@ public function batchUpdateAction(
ArticleEvents::POST_UPDATE
), ArticleEvents::POST_UPDATE);
}
ContentListController::invalidateCache(
ContentListController::invalidateCache(
$this->invalidationCacheUrl,
$this->invalidationToken,
[
Expand Down Expand Up @@ -294,11 +306,23 @@ private function findOr404($listId, $id): ContentListItemInterface {
return $listItem;
}

private function ensureThereIsNoItemOnPositionOrThrow409(int $listId, int $position, bool $isSticky): 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 (null !== $existingContentListItem && $isSticky && $existingContentListItem->isSticky()) {
throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.');
}
if (!$existingContentListItem && !$isSticky) {
return;
}

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.');
}
}
}

0 comments on commit 987f3e9

Please sign in to comment.