favorites are DONE
This commit is contained in:
parent
77b34939cd
commit
c852caac35
@ -6,31 +6,75 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use App\Entity\Album;
|
||||||
|
Use App\Utils;
|
||||||
|
|
||||||
|
#[Route('/favorite')]
|
||||||
class FavoriteController extends AbstractController
|
class FavoriteController extends AbstractController
|
||||||
{
|
{
|
||||||
|
|
||||||
#[Route('/favorites/remove/{album_id}', name: 'app_favorite')]
|
#[Route('/remove/{album_id}', name: 'app_remove_favorite')]
|
||||||
public function removeFromFavorites(): Response
|
public function removeFromFavorites(int $album_id ): Response
|
||||||
{
|
{
|
||||||
return $this->render('favorite/index.html.twig', [
|
$user = $this->getUser();
|
||||||
'controller_name' => 'FavoriteController',
|
$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')]
|
#[Route('/add/{album_id}', name: 'app_add_favorite')]
|
||||||
public function addToFavorites(): Response
|
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
|
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', [
|
return $this->render('favorite/index.html.twig', [
|
||||||
'controller_name' => 'FavoriteController',
|
'controller_name' => 'FavoriteController',
|
||||||
|
'favorites' => $all_liked
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ use Symfony\Component\HttpFoundation\Response;
|
|||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use GuzzleHttp\Client;
|
Use App\Utils;
|
||||||
|
|
||||||
class SearchController extends AbstractController
|
class SearchController extends AbstractController
|
||||||
{
|
{
|
||||||
@ -20,13 +20,10 @@ class SearchController extends AbstractController
|
|||||||
$album = $entityManager->getRepository(Album::class)->findBy(
|
$album = $entityManager->getRepository(Album::class)->findBy(
|
||||||
['album_id' => $id]
|
['album_id' => $id]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (count($album) == 0) {
|
if (count($album) == 0) {
|
||||||
$album = new Album();
|
$album = new Album();
|
||||||
$album->setAlbumId($id);
|
$album->setAlbumId($id);
|
||||||
//https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database
|
//https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database
|
||||||
$entityManager->persist($album);
|
|
||||||
$entityManager->flush();
|
|
||||||
} else {
|
} else {
|
||||||
$album = $album[0];
|
$album = $album[0];
|
||||||
}
|
}
|
||||||
@ -34,9 +31,9 @@ class SearchController extends AbstractController
|
|||||||
if (!str_contains($album_fruit, $fruit)) {
|
if (!str_contains($album_fruit, $fruit)) {
|
||||||
$album->setFruits($album_fruit . $fruit);
|
$album->setFruits($album_fruit . $fruit);
|
||||||
//https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database
|
//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')]
|
#[Route('/search', name: 'app_search')]
|
||||||
public function index(Request $request, EntityManagerInterface $entityManager): Response
|
public function index(Request $request, EntityManagerInterface $entityManager): Response
|
||||||
{
|
{
|
||||||
@ -112,7 +74,7 @@ class SearchController extends AbstractController
|
|||||||
|
|
||||||
$fruit_query = $user_query . " " . $fruit_emoji;
|
$fruit_query = $user_query . " " . $fruit_emoji;
|
||||||
$page_str = strval($page);
|
$page_str = strval($page);
|
||||||
$response = SearchController::makeRequest("GET", "search", [
|
$response = Utils::makeRequest("GET", "/database/search", [
|
||||||
"q" => $fruit_query,
|
"q" => $fruit_query,
|
||||||
"type" => "release",
|
"type" => "release",
|
||||||
"page" => $page_str,
|
"page" => $page_str,
|
||||||
|
39
src/Utils.php
Normal file
39
src/Utils.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
|
class Utils
|
||||||
|
{
|
||||||
|
public 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, $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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,20 +1,30 @@
|
|||||||
{% extends 'base.html.twig' %}
|
{% extends 'base.html.twig' %}
|
||||||
|
|
||||||
{% block title %}Hello FavoriteController!{% endblock %}
|
{% block title %}Favorites!{% endblock %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<style>
|
{% for album_liked in favorites %}
|
||||||
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
|
{% set main_artist = album_liked["artists_sort"] %}
|
||||||
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
|
{% set artists = album_liked["artists"] %}
|
||||||
</style>
|
{% set album = album_liked["title"] %}
|
||||||
|
<section class="album">
|
||||||
<div class="example-wrapper">
|
<span>
|
||||||
<h1>Hello {{ controller_name }}! ✅</h1>
|
<h2>{{album}}</h2>
|
||||||
|
by
|
||||||
This friendly message is coming from:
|
{% for artist in artists %}
|
||||||
<ul>
|
<h3>{{artist["name"]}}</h3>
|
||||||
<li>Your controller at <code>/var/www/symfony/src/Controller/FavoriteController.php</code></li>
|
{% endfor %}
|
||||||
<li>Your template at <code>/var/www/symfony/templates/favorite/index.html.twig</code></li>
|
</span>
|
||||||
</ul>
|
<img
|
||||||
</div>
|
loading="lazy"
|
||||||
|
src="{{album_liked['images'][0]["uri"]}}"
|
||||||
|
alt="{{album ~ ' by ' ~ main_artist}}"
|
||||||
|
/>
|
||||||
|
<h4>TrackList</h4>
|
||||||
|
{% for track in album_liked["tracklist"] %}
|
||||||
|
<p>{{track["title"]}} - {{track["duration"]}}</p>
|
||||||
|
{% endfor %}
|
||||||
|
<br />
|
||||||
|
</section>
|
||||||
|
{% endfor %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -6,12 +6,13 @@ body %}
|
|||||||
<div class="alert alert-danger">
|
<div class="alert alert-danger">
|
||||||
{{ error.messageKey|trans(error.messageData, 'security') }}
|
{{ error.messageKey|trans(error.messageData, 'security') }}
|
||||||
</div>
|
</div>
|
||||||
{% endif %} {% if app.user %}
|
{% endif %}
|
||||||
|
{% if app.user %}
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
You are logged in as {{ app.user.userIdentifier }},
|
You are logged in as {{ app.user.userIdentifier }},
|
||||||
<a href="{{ path('app_logout') }}">Logout</a>
|
<a href="{{ path('app_logout') }}">Logout</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% else %}
|
||||||
|
|
||||||
<h2>Login</h2>
|
<h2>Login</h2>
|
||||||
<label for="username">Username</label>
|
<label for="username">Username</label>
|
||||||
@ -59,6 +60,7 @@ body %}
|
|||||||
If you don't have an account:
|
If you don't have an account:
|
||||||
<a href="{{url('app_register')}}">register</a>.
|
<a href="{{url('app_register')}}">register</a>.
|
||||||
</p>
|
</p>
|
||||||
|
{% endif %}
|
||||||
</form>
|
</form>
|
||||||
</main>
|
</main>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
Reference in New Issue
Block a user