From 36800804e4334c25999790563f3ce11d0aac63fe Mon Sep 17 00:00:00 2001 From: ZeKap Date: Thu, 22 Feb 2024 11:23:12 +0100 Subject: [PATCH] fixed search --- src/Controller/SearchController.php | 21 +++++++++++++-------- templates/search/search.html.twig | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index 4c7c262..9e94696 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -13,22 +13,26 @@ use GuzzleHttp\Client; class SearchController extends AbstractController { - private static function addFruitToAlbums(array $releases,string $fruit,EntityManagerInterface $entityManager):void{ - foreach($releases as $release){ + 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) { + + if (count($album) == 0) { $album = new Album(); $album->setAlbumId($id); //https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database $entityManager->persist($album); $entityManager->flush(); + } else { + $album = $album[0]; } $album_fruit = $album->getFruits(); - if (!str_contains($album_fruit, $fruit)){ - $album->setFruits($album_fruit.$fruit); + 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(); @@ -36,8 +40,9 @@ class SearchController extends AbstractController } } - private static function getEmojiName(string $emoji) : string { - switch ($emoji){ + private static function getEmojiName(string $emoji): string + { + switch ($emoji) { case "🍎": return "apple"; case "🍐": @@ -119,7 +124,7 @@ class SearchController extends AbstractController "per_page" => "15", ]); - SearchController::addFruitToAlbums($result["results"],$fruit,$entityManager); + SearchController::addFruitToAlbums($result["results"], $fruit, $entityManager); return $this->render('search/search.html.twig', [ 'controller_name' => 'SearchController', diff --git a/templates/search/search.html.twig b/templates/search/search.html.twig index e7ac7c9..6a8a4aa 100644 --- a/templates/search/search.html.twig +++ b/templates/search/search.html.twig @@ -9,7 +9,7 @@
-

results for {{ query }} with {{fruit_emoji}} ✅

+

Results for "{{ query }}" with {{fruit_emoji}}:

{% for result in results %} {% set array = result['title']|split('-') %} {% set artist = array[0] %}