pages works
This commit is contained in:
parent
48bcf42e7d
commit
ad85898786
@ -8,13 +8,13 @@ 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 App\Utils;
|
use App\Utils;
|
||||||
|
|
||||||
class SearchController extends AbstractController
|
class SearchController extends AbstractController
|
||||||
{
|
{
|
||||||
|
|
||||||
private static function addFruitToAlbums(array $releases, string $fruit, EntityManagerInterface $entityManager): void
|
private static function addFruitToAlbums(array $releases, string $fruit, EntityManagerInterface $entityManager): void
|
||||||
{
|
{
|
||||||
foreach ($releases as $release) {
|
foreach ($releases as $release) {
|
||||||
$id = $release["id"];
|
$id = $release["id"];
|
||||||
$album = $entityManager->getRepository(Album::class)->findBy(
|
$album = $entityManager->getRepository(Album::class)->findBy(
|
||||||
@ -37,7 +37,8 @@ class SearchController extends AbstractController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getEmojiName(string | null $emoji) : string | null {
|
private static function getEmojiName(string | null $emoji): string | null
|
||||||
|
{
|
||||||
return match ($emoji) {
|
return match ($emoji) {
|
||||||
"🍎" => "apple",
|
"🍎" => "apple",
|
||||||
"🍐" => "pear",
|
"🍐" => "pear",
|
||||||
@ -62,18 +63,18 @@ class SearchController extends AbstractController
|
|||||||
public function index(Request $request, EntityManagerInterface $entityManager): Response
|
public function index(Request $request, EntityManagerInterface $entityManager): Response
|
||||||
{
|
{
|
||||||
$page = $request->query->get('page');
|
$page = $request->query->get('page');
|
||||||
if ($page== null) $page = 1;
|
if ($page == null) $page = 1;
|
||||||
$fruit = $request->query->get('fruit');
|
$fruit = $request->query->get('fruit');
|
||||||
$user_query = $request->query->get('q');
|
$user_query = $request->query->get('q');
|
||||||
|
|
||||||
$fruit_emoji = SearchController::getEmojiName($fruit);
|
$fruit_name = SearchController::getEmojiName($fruit);
|
||||||
if ($fruit_emoji == null) {
|
if ($fruit_name == null) {
|
||||||
// We do not support requests without a fruit or with an invalid one
|
// We do not support requests without a fruit or with an invalid one
|
||||||
// Redirect to the homepage in this case
|
// Redirect to the homepage in this case
|
||||||
return $this->redirect('/');
|
return $this->redirect('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
$fruit_query = $user_query . " " . $fruit_emoji;
|
$fruit_query = $user_query . " " . $fruit_name;
|
||||||
$page_str = strval($page);
|
$page_str = strval($page);
|
||||||
$response = Utils::makeRequest("GET", "/database/search", [
|
$response = Utils::makeRequest("GET", "/database/search", [
|
||||||
"q" => $fruit_query,
|
"q" => $fruit_query,
|
||||||
@ -86,30 +87,30 @@ class SearchController extends AbstractController
|
|||||||
SearchController::addFruitToAlbums($results, $fruit, $entityManager);
|
SearchController::addFruitToAlbums($results, $fruit, $entityManager);
|
||||||
$user = $this->getUser();
|
$user = $this->getUser();
|
||||||
$array_id = array();
|
$array_id = array();
|
||||||
foreach($user->getLiked() as $like){
|
foreach ($user->getLiked() as $like) {
|
||||||
array_push(
|
array_push(
|
||||||
$array_id,
|
$array_id,
|
||||||
$like->getAlbumId()
|
$like->getAlbumId()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$true_results = array();
|
$true_results = array();
|
||||||
foreach($results as $result){
|
foreach ($results as $result) {
|
||||||
|
|
||||||
|
$isLiked = in_array($result["id"], $array_id);
|
||||||
|
|
||||||
$isLiked = in_array($result["id"],$array_id);
|
|
||||||
|
|
||||||
$result = array_merge(
|
$result = array_merge(
|
||||||
$result,
|
$result,
|
||||||
["isLiked" => $isLiked? "true": "false"]
|
["isLiked" => $isLiked ? "true" : "false"]
|
||||||
);
|
);
|
||||||
array_push($true_results,$result);
|
array_push($true_results, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = $true_results;
|
$results = $true_results;
|
||||||
|
|
||||||
return $this->render('search/search.html.twig', [
|
return $this->render('search/search.html.twig', [
|
||||||
'controller_name' => 'SearchController',
|
'controller_name' => 'SearchController',
|
||||||
'query' => $user_query,
|
'query' => $user_query,
|
||||||
'fruit_emoji' => $fruit_emoji,
|
'fruit_emoji' => $fruit,
|
||||||
'fruit_name' => SearchController::getEmojiName($fruit),
|
'fruit_name' => SearchController::getEmojiName($fruit),
|
||||||
'page' => $page,
|
'page' => $page,
|
||||||
'all_page' => $response["pagination"]["pages"],
|
'all_page' => $response["pagination"]["pages"],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user