db stuff trust me

This commit is contained in:
Djalim Simaila 2024-02-22 10:12:02 +00:00
parent 9d7daa65da
commit 09205ce225
6 changed files with 131 additions and 2 deletions

View File

@ -45,7 +45,7 @@ security:
# Note: Only the *first* access control that matches will be used # Note: Only the *first* access control that matches will be used
access_control: access_control:
# - { path: ^/admin, roles: ROLE_ADMIN } # - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER } - { path: ^/favorites, roles: ROLE_USER }
when@test: when@test:
security: security:

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240222094852 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE album ADD album_id INT NOT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE album DROP album_id');
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\HttpFoundation\Request;
class FavoriteController extends AbstractController
{
#[Route('/favorites/remove/{album_id}', name: 'app_favorite')]
public function removeFromFavorites(): Response
{
return $this->render('favorite/index.html.twig', [
'controller_name' => 'FavoriteController',
]);
}
#[Route('/favorites/add/{album_id}', name: 'app_favorite')]
public function addToFavorites(): Response
{
return $this->render('favorite/index.html.twig', [
'controller_name' => 'FavoriteController',
]);
}
#[Route('/favorites', name: 'app_favorite')]
public function index(Request $request): Response
{
return $this->render('favorite/index.html.twig', [
'controller_name' => 'FavoriteController',
]);
}
}

View File

@ -2,15 +2,40 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Album;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 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 GuzzleHttp\Client; use GuzzleHttp\Client;
class SearchController extends AbstractController 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 (!$album) {
$album = new Album();
$album->setAlbumId($id);
//https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database
$entityManager->persist($album);
$entityManager->flush();
}
$album_fruit = $album->getFruits();
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();
}
}
}
private static function getEmojiName(string $emoji) : string { private static function getEmojiName(string $emoji) : string {
switch ($emoji){ switch ($emoji){
case "🍎": case "🍎":
@ -82,7 +107,7 @@ class SearchController extends AbstractController
} }
#[Route('/search', name: 'app_search')] #[Route('/search', name: 'app_search')]
public function index(Request $request): Response public function index(Request $request, EntityManagerInterface $entityManager): Response
{ {
$page = $request->query->get('page'); $page = $request->query->get('page');
$fruit = $request->query->get('fruit'); $fruit = $request->query->get('fruit');
@ -94,6 +119,8 @@ class SearchController extends AbstractController
"per_page" => "15", "per_page" => "15",
]); ]);
SearchController::addFruitToAlbums($result["results"],$fruit,$entityManager);
return $this->render('search/search.html.twig', [ return $this->render('search/search.html.twig', [
'controller_name' => 'SearchController', 'controller_name' => 'SearchController',
'query' => $request->query->get('q'), 'query' => $request->query->get('q'),

View File

@ -16,6 +16,9 @@ class Album
#[ORM\Column(length: 255, nullable: true)] #[ORM\Column(length: 255, nullable: true)]
private ?string $fruits = null; private ?string $fruits = null;
#[ORM\Column]
private ?int $album_id = null;
public function getId(): ?int public function getId(): ?int
{ {
return $this->id; return $this->id;
@ -39,4 +42,16 @@ class Album
return $this; return $this;
} }
public function getAlbumId(): ?int
{
return $this->album_id;
}
public function setAlbumId(int $album_id): static
{
$this->album_id = $album_id;
return $this;
}
} }

View File

@ -0,0 +1,20 @@
{% extends 'base.html.twig' %}
{% block title %}Hello FavoriteController!{% 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>
{% endblock %}