diff --git a/migrations/Version20210503161858.php b/migrations/Version20210503161858.php new file mode 100644 index 0000000..69375cd --- /dev/null +++ b/migrations/Version20210503161858.php @@ -0,0 +1,36 @@ +addSql('CREATE SEQUENCE wish_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE wish (id INT NOT NULL, by_user_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_D7D174C9DC9C2434 ON wish (by_user_id)'); + $this->addSql('ALTER TABLE wish ADD CONSTRAINT FK_D7D174C9DC9C2434 FOREIGN KEY (by_user_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('DROP SEQUENCE wish_id_seq CASCADE'); + $this->addSql('DROP TABLE wish'); + } +} diff --git a/src/Controller/Admin/DashboardController.php b/src/Controller/Admin/DashboardController.php index 23ef616..b6c661a 100644 --- a/src/Controller/Admin/DashboardController.php +++ b/src/Controller/Admin/DashboardController.php @@ -4,6 +4,7 @@ namespace App\Controller\Admin; use App\Entity\User; use App\Entity\Offering; +use App\Entity\Wish; use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard; use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController; @@ -32,5 +33,6 @@ class DashboardController extends AbstractDashboardController yield MenuItem::linktoDashboard('Dashboard', 'fa fa-home'); yield MenuItem::linkToCrud('User', 'fas fa-user', User::class); yield MenuItem::linkToCrud('Offering', 'fas fa-seedling', Offering::class); + yield MenuItem::linkToCrud('Wish', 'fas fa-star', Wish::class); } } diff --git a/src/Controller/Admin/WishCrudController.php b/src/Controller/Admin/WishCrudController.php new file mode 100644 index 0000000..aa01194 --- /dev/null +++ b/src/Controller/Admin/WishCrudController.php @@ -0,0 +1,24 @@ +entityManager = $entityManager; + } + #[Route('/user', name: 'user_page')] public function user(): Response { @@ -15,4 +30,30 @@ class UserController extends AbstractController 'user' => $this->getUser(), ]); } + + #[Route('/wishlist', name: 'wishlist')] + public function wishlist(Request $request, WishRepository $wishRepository): Response + { + $wish = new Wish(); + $form = $this->createForm(WishFormType::class, $wish); + $user = $this->getUser(); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $wish->setByUser($user); + + $this->entityManager->persist($wish); + $this->entityManager->flush(); + + $this->addFlash("success", "Successfully added the new offering!"); + return $this->redirectToRoute('wishlist'); + } + + return $this->render('user/wish.html.twig', [ + 'user' => $this->getUser(), + 'wishes' => $wishRepository->findByUser($user), + 'wish_form' => $form->createView(), + ]); + } } diff --git a/src/Entity/User.php b/src/Entity/User.php index 293a749..9a661e3 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -54,9 +54,15 @@ class User implements UserInterface */ private $offerings; + /** + * @ORM\OneToMany(targetEntity=Wish::class, mappedBy="byUser") + */ + private $wishes; + public function __construct() { $this->offerings = new ArrayCollection(); + $this->wishes = new ArrayCollection(); } public function getId(): ?int @@ -193,4 +199,34 @@ class User implements UserInterface { return (string) $this->getUsername(); } + + /** + * @return Collection|Wish[] + */ + public function getWishes(): Collection + { + return $this->wishes; + } + + public function addWish(Wish $wish): self + { + if (!$this->wishes->contains($wish)) { + $this->wishes[] = $wish; + $wish->setByUser($this); + } + + return $this; + } + + public function removeWish(Wish $wish): self + { + if ($this->wishes->removeElement($wish)) { + // set the owning side to null (unless already changed) + if ($wish->getByUser() === $this) { + $wish->setByUser(null); + } + } + + return $this; + } } diff --git a/src/Entity/Wish.php b/src/Entity/Wish.php new file mode 100644 index 0000000..fac0288 --- /dev/null +++ b/src/Entity/Wish.php @@ -0,0 +1,63 @@ +id; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } + + public function getByUser(): ?User + { + return $this->byUser; + } + + public function setByUser(?User $byUser): self + { + $this->byUser = $byUser; + + return $this; + } + + public function __toString(): string + { + return (string) $this->getTitle(); + } +} diff --git a/src/Form/WishFormType.php b/src/Form/WishFormType.php new file mode 100644 index 0000000..e4bfef5 --- /dev/null +++ b/src/Form/WishFormType.php @@ -0,0 +1,27 @@ +add('title') + ->add('submit', SubmitType::class) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Wish::class, + ]); + } +} diff --git a/src/Repository/WishRepository.php b/src/Repository/WishRepository.php new file mode 100644 index 0000000..e3fa822 --- /dev/null +++ b/src/Repository/WishRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('w') + ->andWhere('w.byUser = :val') + ->setParameter('val', $value) + ->orderBy('w.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + + + /* + public function findOneBySomeField($value): ?Wish + { + return $this->createQueryBuilder('w') + ->andWhere('w.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/templates/user/wish.html.twig b/templates/user/wish.html.twig new file mode 100644 index 0000000..73f0a8c --- /dev/null +++ b/templates/user/wish.html.twig @@ -0,0 +1,36 @@ +{% extends 'base.html.twig' %} + +{% block title %}Whishlist{% endblock %} + +{% block body %} + {% for message in app.flashes('success') %} + + {% endfor %} + +
+

Your Whishlist

+
+ +
+ {% if wishes == [] %} + + {% else %} + + {% endif %} +
+ +
+ +
+

New wish

+ {{ form(wish_form) }} +
+{% endblock %} \ No newline at end of file