Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opsrc 645/added editing controller #164

Merged
merged 22 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions features/editing_name_of_wishlist.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Feature: Editing wishlists name
Given the store operates on a single channel in "United States"
Given I am on "/"

@ui
@ui @javascript
Scenario: Editing wishlists name
And the store has a wishlist named "Wishlist1"
When I go to "/wishlists"
Then I should see "Wishlist1"
And I follow edit for "Wishlist1"
And I fill in "edit_wishlist_name_name" with "Wishlist2"
When I press "edit_wishlist_name_save"
Then I should be on "/wishlists"
And I should see "Wishlist2"
When I press "wishlist-edit-button-Wishlist1"
And I fill in "edit_wishlist_name" with "Wishlist2"
When I press "edit_wishlist_save"
Then I go to "/wishlists"
And I should see "Wishlist2"
17 changes: 12 additions & 5 deletions features/removing_wishlist.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ Feature: Removing a wishlist
Given the store operates on a single channel in "United States"
Given I am on "/"

@ui
@ui @javascript
Scenario: Removing a wishlist
And the store has a wishlist named "Wishlist1"
And the store has a wishlist named "Wishlist2"
When I go to "/wishlists"
Then I should have 3 wishlists
When I follow remove for "Wishlist1"
When I press "wishlist-delete-button-Wishlist1"
When I press "remove_wishlist_save"
Then I should be on "/wishlists"
Then I should wait for one second
Then I should have 2 wishlists

@ui

@ui @javascript
Scenario: Removing a wishlist with one existing
When I go to "/wishlists"
Then I should have 1 wishlists
When I follow remove for "Wishlist1"
Then I should have 1 wishlists
When I press "wishlist-delete-button-Wishlist"
When I press "remove_wishlist_save"
Then I should be on "/wishlists"
Then I should wait for one second
Then I should have 1 wishlists
39 changes: 39 additions & 0 deletions src/Command/Wishlist/UpdateWishlistName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Command\Wishlist;

use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;

final class UpdateWishlistName implements UpdateWishlistNameInterface
{
public string $name;

public ?string $channelCode = null;

private WishlistInterface $wishlist;

public function __construct(string $name, WishlistInterface $wishlist)
{
$this->name = $name;
$this->wishlist = $wishlist;
}

public function getName(): string
{
return $this->name;
}

public function getWishlist(): WishlistInterface
{
return $this->wishlist;
}
}

20 changes: 20 additions & 0 deletions src/Command/Wishlist/UpdateWishlistNameInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Command\Wishlist;

use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;

interface UpdateWishlistNameInterface
{
public function getName(): string;

public function getWishlist(): WishlistInterface;
}
43 changes: 43 additions & 0 deletions src/CommandHandler/Wishlist/UpdateWishlistNameHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/
declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;

use BitBag\SyliusWishlistPlugin\Command\Wishlist\UpdateWishlistName;
use BitBag\SyliusWishlistPlugin\Exception\WishlistNameIsTakenException;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;

final class UpdateWishlistNameHandler
{
private WishlistRepositoryInterface $wishlistRepository;

private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver;

public function __construct(
WishlistRepositoryInterface $wishlistRepository,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver
) {
$this->wishlistRepository = $wishlistRepository;
$this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver;
}

public function __invoke(UpdateWishlistName $updateWishlistName): void
{
$wishlist = $updateWishlistName->getWishlist();

$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();

if ($this->wishlistRepository->findOneByTokenAndName($wishlistCookieToken, $updateWishlistName->getName())) {
throw new WishlistNameIsTakenException();
}

$wishlist->setName($updateWishlistName->getName());
$this->wishlistRepository->add($wishlist);
}
}
73 changes: 73 additions & 0 deletions src/Controller/Action/UpdateWishlistNameAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Controller\Action;

use BitBag\SyliusWishlistPlugin\Command\Wishlist\UpdateWishlistName;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webmozart\Assert\Assert;

final class UpdateWishlistNameAction
{
private MessageBusInterface $commandBus;

private FlashBagInterface $flashBag;

private TranslatorInterface $translator;

private WishlistRepositoryInterface $wishlistRepository;

private UrlGeneratorInterface $urlGenerator;

public function __construct(
MessageBusInterface $commandBus,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
WishlistRepositoryInterface $wishlistRepository,
UrlGeneratorInterface $urlGenerator
) {
$this->commandBus = $commandBus;
$this->flashBag = $flashBag;
$this->translator = $translator;
$this->wishlistRepository = $wishlistRepository;
$this->urlGenerator = $urlGenerator;
}

public function __invoke(Request $request): Response
{
$wishlistName = $request->request->get('edit_wishlist_name')['name'];
milwoz marked this conversation as resolved.
Show resolved Hide resolved
Assert::string($wishlistName);
$wishlistId = $request->attributes->getInt('id');
$wishlist = $this->wishlistRepository->find($wishlistId);

try {
$updateWishlistName = new UpdateWishlistName($wishlistName, $wishlist);
$this->commandBus->dispatch($updateWishlistName);

$this->flashBag->add(
'success',
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_name_changed')
);
} catch (HandlerFailedException $exception) {
$this->flashBag->add(
'error',
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_name_already_exists')
);
}
return new Response($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
}
}
16 changes: 15 additions & 1 deletion src/Repository/WishlistRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function findAllByToken(string $token): ?array
->getResult()
;
}

public function findAllByShopUser(int $shopUser): ?array
{
return $this->createQueryBuilder('o')
Expand Down Expand Up @@ -114,4 +114,18 @@ public function findAllByAnonymousAndChannel(?string $token, ChannelInterface $c

return $qb->getQuery()->getResult();
}

public function findOneByTokenAndName(string $token, string $name): ?WishlistInterface
{
return $this->createQueryBuilder('o')
->where('o.token = :token')
->andWhere('o.name =:name')
->setParameter('token', $token)
->setParameter('name', $name)
->getQuery()
->setMaxResults(1)
->getOneOrNullResult()
;
}

}
2 changes: 2 additions & 0 deletions src/Repository/WishlistRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ public function findOneByShopUserAndChannel(
): ?WishlistInterface;

public function findAllByAnonymousAndChannel(?string $token, ChannelInterface $channel): ?array;

public function findOneByTokenAndName(string $token, string $name): ?WishlistInterface;
}
115 changes: 0 additions & 115 deletions src/Resources/assets/shop/js/addAnotherWishlistModal.js

This file was deleted.

Loading