- Favoris qui s'affiche dans les recherches
This commit is contained in:
@ -29,41 +29,47 @@ class APISearchController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/toggleLike/{idGoogle}', name: 'like', methods: "POST")]
|
||||
public function addFavoris (FavorisRepository $favorisRepository, String $idGoogle)
|
||||
public function addFavoris(FavorisRepository $favorisRepository, String $idGoogle)
|
||||
{
|
||||
$favoris = new Favoris();
|
||||
$user = $this->getUser();
|
||||
$favoris->setUser($user);
|
||||
$favoris->setIdGoogle($idGoogle);
|
||||
|
||||
return $this->json($favorisRepository->addFavoris($favoris));
|
||||
|
||||
return $this->json(['success' => true, 'message' => 'Favoris ajouté']);
|
||||
}
|
||||
|
||||
#[Route('/untoggleLike/{idGoogle}', name: 'unlike', methods: "POST")]
|
||||
public function removeFavoris (FavorisRepository $favorisRepository, String $idGoogle)
|
||||
public function removeFavoris(FavorisRepository $favorisRepository, String $idGoogle)
|
||||
{
|
||||
$favoris = new Favoris();
|
||||
$user = $this->getUser();
|
||||
$favoris->setUser($user);
|
||||
$favoris->setIdGoogle($idGoogle);
|
||||
$favorisRepository->removeFavoris($user, $idGoogle);
|
||||
|
||||
return $this->json($favorisRepository->removeFavoris($user, $idGoogle));
|
||||
|
||||
return $this->json(['success' => true, 'message' => 'Favoris supprimé']);
|
||||
}
|
||||
|
||||
#[Route('/api/search', name: 'api_search')]
|
||||
public function index(Request $request): Response
|
||||
public function index(Request $request, FavorisRepository $favorisRepository): Response
|
||||
{
|
||||
ini_set('memory_limit', '512M');
|
||||
|
||||
// Appeler la méthode search et récupérer les résultats
|
||||
$datas = $this->search($request);
|
||||
$query = $request->query->get('q');
|
||||
|
||||
$user = $this->getUser();
|
||||
|
||||
$favoris = $favorisRepository->getFavorisByUser($user);
|
||||
|
||||
// Afficher les résultats dans le template
|
||||
return $this->render('apiSearch/index.html.twig', [
|
||||
'controller_name' => 'APISearchController',
|
||||
'datas' => $datas,
|
||||
'query' => $query,
|
||||
'favoris' => $favoris,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -48,28 +48,37 @@ class FavorisRepository extends ServiceEntityRepository
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Favoris[] Returns an array of Favoris objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('f')
|
||||
// ->andWhere('f.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('f.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
public function getFavorisByUser($userId): array
|
||||
{
|
||||
return $this->createQueryBuilder('f')
|
||||
->andWhere('f.user = :userId')
|
||||
->setParameter('userId', $userId)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
// public function findOneBySomeField($value): ?Favoris
|
||||
// {
|
||||
// return $this->createQueryBuilder('f')
|
||||
// ->andWhere('f.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
// /**
|
||||
// * @return Favoris[] Returns an array of Favoris objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('f')
|
||||
// ->andWhere('f.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('f.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?Favoris
|
||||
// {
|
||||
// return $this->createQueryBuilder('f')
|
||||
// ->andWhere('f.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
|
@ -3,10 +3,10 @@
|
||||
{% block title %}{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
|
||||
<div>
|
||||
<div>
|
||||
<div class="w-1/2 py-2 flex flex-col justify-center mt-5 mx-auto">
|
||||
<h1 class="text-2xl font-bold">Résultats pour : {{ query }}</h1>
|
||||
<h1 class="text-2xl font-bold">Résultats pour :
|
||||
{{ query }}</h1>
|
||||
</div>
|
||||
{% for book in datas.items %}
|
||||
<div class="relative flex rounded-xl border bg-white shadow-lg hover:shadow-xl transition-shadow duration-300 w-1/2 mx-auto py-2 mt-5">
|
||||
@ -25,15 +25,27 @@
|
||||
</div>
|
||||
<div class="w-9/12">
|
||||
{% if book.volumeInfo.publisher is defined %}
|
||||
<p class="p-2 italic font-bold">Aux éditions : <span class="font-normal">{{ book.volumeInfo.publisher }}</span></p>
|
||||
<p class="p-2 italic font-bold">Date de publication : <span class="font-normal">{{ book.volumeInfo.publishedDate }}</span></p>
|
||||
<p class="p-2 italic font-bold">Aux éditions :
|
||||
<span class="font-normal">{{ book.volumeInfo.publisher }}</span>
|
||||
</p>
|
||||
<p class="p-2 italic font-bold">Date de publication :
|
||||
<span class="font-normal">{{ book.volumeInfo.publishedDate }}</span>
|
||||
</p>
|
||||
{% if book.volumeInfo.categories is defined %}
|
||||
{% for categorie in book.volumeInfo.categories %}
|
||||
<p class="p-2 italic font-bold">Catégorie : <span class="font-normal">{{ categorie }}</span></p>
|
||||
<p class="p-2 italic font-bold">Catégorie :
|
||||
<span class="font-normal">{{ categorie }}</span>
|
||||
</p>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
<p class="p-2 italic font-bold">Nombres de pages : <span class="font-normal">{{ book.volumeInfo.pageCount }}</span></p>
|
||||
<p class="p-2 italic font-bold">Description : <span class="font-normal">{{ book.searchInfo.textSnippet | raw }}</span></p>
|
||||
<p class="p-2 italic font-bold">Nombres de pages :
|
||||
<span class="font-normal">{{ book.volumeInfo.pageCount }}</span>
|
||||
</p>
|
||||
<p class="p-2 italic font-bold">Description :
|
||||
{% if book.searchInfo is defined %}
|
||||
<span class="font-normal">{{ book.searchInfo.textSnippet | raw }}</span>
|
||||
{% endif %}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
@ -46,69 +58,64 @@
|
||||
|
||||
|
||||
{% if app.user %}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
|
||||
class="absolute top-2 right-2 w-6 h-6 cursor-pointer transition-colors duration-300"
|
||||
x-data="{ liked: false }"
|
||||
:fill="liked ? 'red' : 'none'"
|
||||
:stroke="liked ? 'red' : 'currentColor'"
|
||||
@click="toggleLike('{{ book.id }} ', liked); liked = !liked">
|
||||
<path stroke-linecap="round" stroke-linejoin="round"
|
||||
d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z"/>
|
||||
{% for favori in favoris %}
|
||||
{% if favori.getIdGoogle() == book.id %}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewbox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="absolute top-2 right-2 w-6 h-6 cursor-pointer transition-colors duration-300" x-data="{ liked: true }" :fill="liked ? 'red' : 'none'" :stroke="liked ? 'red' : 'currentColor'" @click="toggleLike('{{ book.id }} ', liked); liked = !liked">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z"/>
|
||||
</svg>
|
||||
{% endif %}
|
||||
{% if favori.getIdGoogle() != book.id %}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewbox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="absolute top-2 right-2 w-6 h-6 cursor-pointer transition-colors duration-300" x-data="{ liked: false }" :fill="liked ? 'red' : 'none'" :stroke="liked ? 'red' : 'currentColor'" @click="toggleLike('{{ book.id }} ', liked); liked = !liked">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z"/>
|
||||
</svg>
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% endfor %}
|
||||
|
||||
{% endif %}
|
||||
|
||||
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function toggleLike(idGoogle, liked) {
|
||||
// Si l'élément est déjà aimé (liked = true), on le retire des favoris
|
||||
if (liked) {
|
||||
fetch("{{ path('unlike', {'idGoogle': 'PLACEHOLDER'}) }}".replace('PLACEHOLDER', idGoogle), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
// D'autres données peuvent être envoyées si nécessaire
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.status === 'removed') {
|
||||
// Mettre à jour le SVG ou l'état du bouton pour retirer le like
|
||||
console.log('Livre retiré des favoris');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Erreur AJAX', error);
|
||||
});
|
||||
} else { // Si l'élément n'est pas encore aimé (liked = false), on l'ajoute aux favoris
|
||||
fetch("{{ path('like', {'idGoogle': 'PLACEHOLDER'}) }}".replace('PLACEHOLDER', idGoogle), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
// D'autres données peuvent être envoyées si nécessaire
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.status === 'added') {
|
||||
// Mettre à jour le SVG ou l'état du bouton pour ajouter le like
|
||||
console.log('Livre ajouté aux favoris');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Erreur AJAX', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script>
|
||||
function toggleLike(idGoogle, liked) { // Si l'élément est déjà aimé (liked = true), on le retire des favoris
|
||||
if (liked) {
|
||||
fetch("{{ path('unlike', {'idGoogle': 'PLACEHOLDER'}) }}".replace('PLACEHOLDER', idGoogle), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: JSON.stringify({ // D'autres données peuvent être envoyées si nécessaire
|
||||
})
|
||||
}).then(response => response.json()).then(data => {
|
||||
if (data.status === 'removed') { // Mettre à jour le SVG ou l'état du bouton pour retirer le like
|
||||
console.log('Livre retiré des favoris');
|
||||
}
|
||||
}).catch(error => {
|
||||
console.error('Erreur AJAX', error);
|
||||
});
|
||||
} else { // Si l'élément n'est pas encore aimé (liked = false), on l'ajoute aux favoris
|
||||
fetch("{{ path('like', {'idGoogle': 'PLACEHOLDER'}) }}".replace('PLACEHOLDER', idGoogle), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: JSON.stringify({ // D'autres données peuvent être envoyées si nécessaire
|
||||
})
|
||||
}).then(response => response.json()).then(data => {
|
||||
if (data.status === 'added') { // Mettre à jour le SVG ou l'état du bouton pour ajouter le like
|
||||
console.log('Livre ajouté aux favoris');
|
||||
}
|
||||
}).catch(error => {
|
||||
console.error('Erreur AJAX', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
|
Reference in New Issue
Block a user