diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 381807b..905cf2d 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -45,7 +45,7 @@ security: # Note: Only the *first* access control that matches will be used access_control: # - { path: ^/admin, roles: ROLE_ADMIN } - # - { path: ^/profile, roles: ROLE_USER } + - { path: ^/favorites, roles: ROLE_USER } when@test: security: diff --git a/migrations/Version20240222094852.php b/migrations/Version20240222094852.php new file mode 100644 index 0000000..f14e1be --- /dev/null +++ b/migrations/Version20240222094852.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE album ADD album_id INT NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE album DROP album_id'); + } +} diff --git a/src/Controller/FavoriteController.php b/src/Controller/FavoriteController.php new file mode 100644 index 0000000..beaee75 --- /dev/null +++ b/src/Controller/FavoriteController.php @@ -0,0 +1,36 @@ +render('favorite/index.html.twig', [ + 'controller_name' => 'FavoriteController', + ]); + } + + #[Route('/favorites/add/{album_id}', name: 'app_favorite')] + public function addToFavorites(): Response + { + return $this->render('favorite/index.html.twig', [ + 'controller_name' => 'FavoriteController', + ]); + } + + #[Route('/favorites', name: 'app_favorite')] + public function index(Request $request): Response + { + return $this->render('favorite/index.html.twig', [ + 'controller_name' => 'FavoriteController', + ]); + } +} diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index b70350d..4c7c262 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -2,15 +2,40 @@ namespace App\Controller; +use App\Entity\Album; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\Request; +use Doctrine\ORM\EntityManagerInterface; use GuzzleHttp\Client; class SearchController extends AbstractController { + private static function addFruitToAlbums(array $releases,string $fruit,EntityManagerInterface $entityManager):void{ + foreach($releases as $release){ + $id = $release["id"]; + $album = $entityManager->getRepository(Album::class)->findBy( + ['album_id' => $id] + ); + if (!$album) { + $album = new Album(); + $album->setAlbumId($id); + //https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database + $entityManager->persist($album); + $entityManager->flush(); + } + $album_fruit = $album->getFruits(); + if (!str_contains($album_fruit, $fruit)){ + $album->setFruits($album_fruit.$fruit); + //https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database + $entityManager->persist($album); + $entityManager->flush(); + } + } + } + private static function getEmojiName(string $emoji) : string { switch ($emoji){ case "🍎": @@ -82,7 +107,7 @@ class SearchController extends AbstractController } #[Route('/search', name: 'app_search')] - public function index(Request $request): Response + public function index(Request $request, EntityManagerInterface $entityManager): Response { $page = $request->query->get('page'); $fruit = $request->query->get('fruit'); @@ -94,6 +119,8 @@ class SearchController extends AbstractController "per_page" => "15", ]); + SearchController::addFruitToAlbums($result["results"],$fruit,$entityManager); + return $this->render('search/search.html.twig', [ 'controller_name' => 'SearchController', 'query' => $request->query->get('q'), diff --git a/src/Entity/Album.php b/src/Entity/Album.php index e8548fe..63616ee 100644 --- a/src/Entity/Album.php +++ b/src/Entity/Album.php @@ -16,6 +16,9 @@ class Album #[ORM\Column(length: 255, nullable: true)] private ?string $fruits = null; + #[ORM\Column] + private ?int $album_id = null; + public function getId(): ?int { return $this->id; @@ -39,4 +42,16 @@ class Album return $this; } + + public function getAlbumId(): ?int + { + return $this->album_id; + } + + public function setAlbumId(int $album_id): static + { + $this->album_id = $album_id; + + return $this; + } } diff --git a/templates/favorite/index.html.twig b/templates/favorite/index.html.twig new file mode 100644 index 0000000..c5fb3e4 --- /dev/null +++ b/templates/favorite/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello FavoriteController!{% endblock %} + +{% block body %} + + +
/var/www/symfony/src/Controller/FavoriteController.php
/var/www/symfony/templates/favorite/index.html.twig