Skip to content

Commit

Permalink
Merge pull request #170 from BitBagCommerce/OPSRC-588/Saving_wishlist
Browse files Browse the repository at this point in the history
Opsrc 588/saving wishlist
  • Loading branch information
milwoz authored Aug 24, 2022
2 parents d42379a + 7323eb0 commit 36c7c84
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 15 deletions.
39 changes: 39 additions & 0 deletions src/Command/Wishlist/AddWishlistToUser.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;
use Sylius\Component\Core\Model\ShopUserInterface;

final class AddWishlistToUser
{
private WishlistInterface $wishlist;

private ShopUserInterface $shopUser;

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

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

public function getShopUser(): ShopUserInterface
{
return $this->shopUser;
}
}
51 changes: 51 additions & 0 deletions src/CommandHandler/Wishlist/AddWishlistToUserHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\AddWishlistToUser;
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Exception\WishlistHasAnotherShopUserException;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class AddWishlistToUserHandler implements MessageHandlerInterface
{
private WishlistRepositoryInterface $wishlistRepository;

private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver;

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

public function __invoke(AddWishlistToUser $addWishlistsToUser): void
{
$wishlist = $addWishlistsToUser->getWishlist();
$user = $addWishlistsToUser->getShopUser();
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();

if ($wishlistCookieToken !== $wishlist->getToken()){
throw new WishlistHasAnotherShopUserException();
}

if ($this->wishlistRepository->findOneByShopUserAndName($user, $wishlist->getName()) instanceof WishlistInterface) {
$wishlist->setName($wishlist->getName().$wishlist->getId());
}

$wishlist->setShopUser($user);
$this->wishlistRepository->add($wishlist);
}
}
81 changes: 81 additions & 0 deletions src/Controller/Action/AddWishlistToUserAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\AddWishlistToUser;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
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\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webmozart\Assert\Assert;

final class AddWishlistToUserAction
{
private MessageBusInterface $commandBus;

private FlashBagInterface $flashBag;

private TranslatorInterface $translator;

private WishlistRepositoryInterface $wishlistRepository;

private UrlGeneratorInterface $urlGenerator;

private TokenStorageInterface $tokenStorage;

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

public function __invoke(Request $request): Response
{
/** @var ShopUserInterface $shopUser */
$shopUser = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$wishlistId = $request->attributes->getInt('id');
$wishlist = $this->wishlistRepository->find($wishlistId);

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

$this->flashBag->add(
'success',
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_saved')
);
} catch (HandlerFailedException $exception) {
$this->flashBag->add(
'error',
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.you_have_no_access_to_that_wishlist')
);
}

return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_wishlists'));
}
}
13 changes: 11 additions & 2 deletions src/Controller/Action/ShowChosenWishlistAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Twig\Environment;

final class ShowChosenWishlistAction
Expand All @@ -40,14 +42,17 @@ final class ShowChosenWishlistAction

private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver;

private TokenStorageInterface $tokenStorage;

public function __construct(
WishlistRepositoryInterface $wishlistRepository,
CartContextInterface $cartContext,
FormFactoryInterface $formFactory,
Environment $twigEnvironment,
WishlistCommandProcessorInterface $wishlistCommandProcessor,
UrlGeneratorInterface $urlGenerator,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
TokenStorageInterface $tokenStorage
) {
$this->wishlistRepository = $wishlistRepository;
$this->cartContext = $cartContext;
Expand All @@ -56,15 +61,19 @@ public function __construct(
$this->wishlistCommandProcessor = $wishlistCommandProcessor;
$this->urlGenerator = $urlGenerator;
$this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver;
$this->tokenStorage = $tokenStorage;
}

public function __invoke(string $wishlistId, Request $request): Response
{
/** @var WishlistInterface $wishlist */
$wishlist = $this->wishlistRepository->find((int)$wishlistId);
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;

if ($wishlist instanceof WishlistInterface && $wishlist->getToken() === $wishlistCookieToken) {
if ($wishlist instanceof WishlistInterface && $user instanceof ShopUserInterface
|| $wishlist instanceof WishlistInterface && $wishlist->getToken() === $wishlistCookieToken
&& $wishlist->getShopUser() === null) {
$form = $this->createForm($wishlist);
return new Response(
$this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
Expand Down
17 changes: 17 additions & 0 deletions src/Exception/WishlistHasAnotherShopUserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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\Exception;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class WishlistHasAnotherShopUserException extends NotFoundHttpException
{
}
13 changes: 13 additions & 0 deletions src/Repository/WishlistRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,17 @@ public function findOneByTokenAndName(string $token, string $name): ?WishlistInt
;
}

public function findOneByShopUserAndName(ShopUserInterface $shopUser, string $name): ?WishlistInterface
{
return $this->createQueryBuilder('o')
->where('o.shopUser = :shopUser')
->andWhere('o.name =:name')
->setParameter('shopUser', $shopUser)
->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 @@ -37,4 +37,6 @@ public function findOneByShopUserAndChannel(
public function findAllByAnonymousAndChannel(?string $token, ChannelInterface $channel): ?array;

public function findOneByTokenAndName(string $token, string $name): ?WishlistInterface;

public function findOneByShopUserAndName(ShopUserInterface $shopUser, string $name): ?WishlistInterface;
}
6 changes: 6 additions & 0 deletions src/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ bitbag_sylius_wishlist_plugin_shop_wishlist_copy_selected_products_to_other_wish
methods: [ POST ]
defaults:
_controller: bitbag_sylius_wishlist_plugin.controller.action.copy_selected_products_to_other_wishlist

bitbag_sylius_wishlist_plugin_shop_wishlist_add_wishlist_to_user:
path: /wishlists/{id}/save-wishlist
methods: [GET, PUT, POST]
defaults:
_controller: bitbag_sylius_wishlist_plugin.controller.action.add_wishlists_to_user
13 changes: 13 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ services:
- "@bitbag_sylius_wishlist_plugin.processor.wishlist_command_processor"
- "@router.default"
- "@bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver"
- "@security.token_storage"
tags:
- { name: controller.service_arguments }

Expand Down Expand Up @@ -359,3 +360,15 @@ services:
- "@router"
tags:
- { name: controller.service_arguments }

bitbag_sylius_wishlist_plugin.controller.action.add_wishlists_to_user:
class: BitBag\SyliusWishlistPlugin\Controller\Action\AddWishlistToUserAction
arguments:
- "@sylius.command_bus"
- "@session.flash_bag"
- "@translator"
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
- "@router"
- "@security.token_storage"
tags:
- { name: controller.service_arguments }
9 changes: 8 additions & 1 deletion src/Resources/config/services/message_handler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ services:
arguments:
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
- "@bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver"
- "@sylius.context.channel"
tags:
- { name: messenger.message_handler }

bitbag_sylius_wishlist_plugin.command_handler.wishlist.add_wishlists_to_user_handler:
class: BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist\AddWishlistToUserHandler
arguments:
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
- "@bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver"
tags:
- { name: messenger.message_handler }
3 changes: 3 additions & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ bitbag_sylius_wishlist_plugin:
change_wishlist_name: Change wishlist name.
product_variant_exists_in_another_wishlist: exists in another wishlist.
wishlist_name_changed: Wishlist name has been changed.
wishlist_save: Save wishlist
wishlist_saved: Wishlist has been saved.
you_have_no_access_to_that_wishlist: You have no access to that wishlist.
33 changes: 21 additions & 12 deletions src/Resources/views/WishlistGroup/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,36 @@
<div class="bb-wishlist-list-wraper-content">
<div class="bb-wishlist-list-wraper-content-name">
<a
href="{{ path('bitbag_sylius_wishlist_plugin_shop_wishlist_show_chosen_wishlist', {'wishlistId': wishlist.id}) }}"
{{ sylius_test_html_attribute('wishlist-wishlist') }}
data-wishlist-name="{{ wishlist.name }}"
href="{{ path('bitbag_sylius_wishlist_plugin_shop_wishlist_show_chosen_wishlist', {'wishlistId': wishlist.id}) }}"
{{ sylius_test_html_attribute('wishlist-wishlist') }}
data-wishlist-name="{{ wishlist.name }}"
>
{{ wishlist.name == null ? 'bitbag_sylius_wishlist_plugin.ui.wishlist'|trans : wishlist.name|trans }}
{{ wishlist.name == null ? 'bitbag_sylius_wishlist_plugin.ui.wishlist'|trans : wishlist.name|trans }}
</a>
</div>
<div class="middle aligned column">
{% if wishlist.shopuser is null and app.user is not null %}
<a
href="{{ path('bitbag_sylius_wishlist_plugin_shop_wishlist_add_wishlist_to_user', {'id': wishlist.id}) }}"
id="wishlist-add-to-user-button-{{ wishlist.name }}" class="ui icon button default"
data-wishlist-name="{{ wishlist.name }}"
>
{{ 'bitbag_sylius_wishlist_plugin.ui.wishlist_save'|trans }}
</a>
{% endif %}
<button
id="wishlist-delete-button-{{ wishlist.name }}" class="ui icon button default"
{{ sylius_test_html_attribute('wishlist-wishlist-remove') }}
data-wishlist-name="{{ wishlist.name }}"
data-wishlist-remove-id="{{ wishlist.id }}"
id="wishlist-delete-button-{{ wishlist.name }}" class="ui icon button default"
{{ sylius_test_html_attribute('wishlist-wishlist-remove') }}
data-wishlist-name="{{ wishlist.name }}"
data-wishlist-remove-id="{{ wishlist.id }}"
>
<i class="icon remove"></i>
</button>
<button
id="wishlist-edit-button-{{ wishlist.name }}" class="ui icon button default"
{{ sylius_test_html_attribute('wishlist-wishlist-edit') }}
data-wishlist-name="{{ wishlist.name }}"
data-wishlist-edit-id="{{ wishlist.id }}"
id="wishlist-edit-button-{{ wishlist.name }}" class="ui icon button default"
{{ sylius_test_html_attribute('wishlist-wishlist-edit') }}
data-wishlist-name="{{ wishlist.name }}"
data-wishlist-edit-id="{{ wishlist.id }}"
>
<i class="icon edit"></i>
</button>
Expand Down

0 comments on commit 36c7c84

Please sign in to comment.