diff --git a/src/Controller/FavoriteController.php b/src/Controller/FavoriteController.php index beaee75..fad2908 100644 --- a/src/Controller/FavoriteController.php +++ b/src/Controller/FavoriteController.php @@ -6,31 +6,75 @@ 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 App\Entity\Album; +Use App\Utils; +#[Route('/favorite')] class FavoriteController extends AbstractController { - #[Route('/favorites/remove/{album_id}', name: 'app_favorite')] - public function removeFromFavorites(): Response + #[Route('/remove/{album_id}', name: 'app_remove_favorite')] + public function removeFromFavorites(int $album_id ): Response { - return $this->render('favorite/index.html.twig', [ - 'controller_name' => 'FavoriteController', - ]); + $user = $this->getUser(); + $album = $entityManager->getRepository(Album::class)->findBy( + ['album_id' => $album_id] + //['album_id' => $request->query->get('album_id')] + ); + + if (count($album) == 0) { + return new Response("ca pas a marche"); + } + + $user->removeLiked($album[0]); + $entityManager->persist($user); + $entityManager->flush(); + + return new Response("ca a marche"); } - #[Route('/favorites/add/{album_id}', name: 'app_favorite')] - public function addToFavorites(): Response + #[Route('/add/{album_id}', name: 'app_add_favorite')] + public function addToFavorites(int $album_id ,Request $request, EntityManagerInterface $entityManager): Response { - return $this->render('favorite/index.html.twig', [ - 'controller_name' => 'FavoriteController', - ]); + + $user = $this->getUser(); + $album = $entityManager->getRepository(Album::class)->findBy( + ['album_id' => $album_id] + //['album_id' => $request->query->get('album_id')] + ); + + if (count($album) == 0) { + return new Response("ca pas a marche"); + } + $user->addLiked($album[0]); + $entityManager->persist($user); + $entityManager->flush(); + + return new Response("ca a marche"); } - - #[Route('/favorites', name: 'app_favorite')] + + #[Route('/', name: 'app_favorites')] public function index(Request $request): Response { + $user = $this->getUser(); + + $all_liked = array(); + foreach($user->getLiked() as $liked){ + $release = Utils::makeRequest("GET", "/releases/".strval($liked->getAlbumId()), []); + $release = array_merge( + $release, + ["fruit" => $liked->getFruits()] + ); + array_push( + $all_liked, + $release, + ); + } + return $this->render('favorite/index.html.twig', [ 'controller_name' => 'FavoriteController', + 'favorites' => $all_liked ]); - } + } } diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index f81499b..6274315 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -8,25 +8,22 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\Request; use Doctrine\ORM\EntityManagerInterface; -use GuzzleHttp\Client; +Use App\Utils; 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 (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]; } @@ -34,9 +31,9 @@ class SearchController extends AbstractController 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(); } + $entityManager->persist($album); + $entityManager->flush(); } } @@ -61,41 +58,6 @@ class SearchController extends AbstractController }; } - - - private static function makeRequest(string $method, string $endpoint, array $querryParams) - { - - // Create a new Guzzle HTTP client - $baseUri = 'https://api.discogs.com/'; - $client = new Client([ - 'base_uri' => $baseUri, - 'timeout' => 10, // You can set request timeout (in seconds) - ]); - - // Set the base URI for the request and any default request options - $auth = array( - "key" => "kLzxeHuJCXRyQuBURaEN", - "secret" => "biDCkuoMbGLeDtQGqCPCtbVqmzPaVmQG" - ); - - // Create the query parameters array - $querryParams = array_merge($querryParams, $auth); - - // Send a GET request to the specified URI with the query parameters - $response = $client->request($method, "/database/" . $endpoint, [ - 'query' => $querryParams - ]); - - // Get the status code of the response - $statusCode = $response->getStatusCode(); - - // Get the body of the response - $body = $response->getBody()->getContents(); - - return json_decode($body, true); - } - #[Route('/search', name: 'app_search')] public function index(Request $request, EntityManagerInterface $entityManager): Response { @@ -112,7 +74,7 @@ class SearchController extends AbstractController $fruit_query = $user_query . " " . $fruit_emoji; $page_str = strval($page); - $response = SearchController::makeRequest("GET", "search", [ + $response = Utils::makeRequest("GET", "/database/search", [ "q" => $fruit_query, "type" => "release", "page" => $page_str, diff --git a/src/Utils.php b/src/Utils.php new file mode 100644 index 0000000..40d8a15 --- /dev/null +++ b/src/Utils.php @@ -0,0 +1,39 @@ + $baseUri, + 'timeout' => 10, // You can set request timeout (in seconds) + ]); + + // Set the base URI for the request and any default request options + $auth = array( + "key" => "kLzxeHuJCXRyQuBURaEN", + "secret" => "biDCkuoMbGLeDtQGqCPCtbVqmzPaVmQG" + ); + + // Create the query parameters array + $querryParams = array_merge($querryParams, $auth); + + // Send a GET request to the specified URI with the query parameters + $response = $client->request($method, $endpoint, [ + 'query' => $querryParams + ]); + + // Get the status code of the response + $statusCode = $response->getStatusCode(); + + // Get the body of the response + $body = $response->getBody()->getContents(); + + return json_decode($body, true); + } +} diff --git a/templates/favorite/index.html.twig b/templates/favorite/index.html.twig index c5fb3e4..9ec1e41 100644 --- a/templates/favorite/index.html.twig +++ b/templates/favorite/index.html.twig @@ -1,20 +1,30 @@ {% extends 'base.html.twig' %} -{% block title %}Hello FavoriteController!{% endblock %} +{% block title %}Favorites!{% endblock %} {% block body %} - - -
/var/www/symfony/src/Controller/FavoriteController.php
/var/www/symfony/templates/favorite/index.html.twig
{{track["title"]}} - {{track["duration"]}}
+ {% endfor %} +