favorites are DONE

This commit is contained in:
Djalim Simaila 2024-02-22 14:14:56 +00:00
parent 77b34939cd
commit c852caac35
5 changed files with 130 additions and 73 deletions

View File

@ -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
]);
}
}
}

View File

@ -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,

39
src/Utils.php Normal file
View 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);
}
}

View File

@ -1,20 +1,30 @@
{% extends 'base.html.twig' %}
{% block title %}Hello FavoriteController!{% endblock %}
{% block title %}Favorites!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code>/var/www/symfony/src/Controller/FavoriteController.php</code></li>
<li>Your template at <code>/var/www/symfony/templates/favorite/index.html.twig</code></li>
</ul>
</div>
{% for album_liked in favorites %}
{% set main_artist = album_liked["artists_sort"] %}
{% set artists = album_liked["artists"] %}
{% set album = album_liked["title"] %}
<section class="album">
<span>
<h2>{{album}}</h2>
by
{% for artist in artists %}
<h3>{{artist["name"]}}</h3>
{% endfor %}
</span>
<img
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 %}

View File

@ -6,12 +6,13 @@ body %}
<div class="alert alert-danger">
{{ error.messageKey|trans(error.messageData, 'security') }}
</div>
{% endif %} {% if app.user %}
{% endif %}
{% if app.user %}
<div class="mb-3">
You are logged in as {{ app.user.userIdentifier }},
<a href="{{ path('app_logout') }}">Logout</a>
</div>
{% endif %}
{% else %}
<h2>Login</h2>
<label for="username">Username</label>
@ -59,6 +60,7 @@ body %}
If you don't have an account:
<a href="{{url('app_register')}}">register</a>.
</p>
{% endif %}
</form>
</main>
{% endblock %}