4 - Add createdAt column
This commit is contained in:
parent
6c02f9b4c1
commit
5821209e4d
6 changed files with 110 additions and 1 deletions
35
migrations/Version20260605221350.php
Normal file
35
migrations/Version20260605221350.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?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 Version20260605221350 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 rating ADD user_id INT NOT NULL');
|
||||||
|
$this->addSql('ALTER TABLE rating ADD CONSTRAINT FK_D8892622A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) NOT DEFERRABLE');
|
||||||
|
$this->addSql('CREATE INDEX IDX_D8892622A76ED395 ON rating (user_id)');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this down() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->addSql('ALTER TABLE rating DROP CONSTRAINT FK_D8892622A76ED395');
|
||||||
|
$this->addSql('DROP INDEX IDX_D8892622A76ED395');
|
||||||
|
$this->addSql('ALTER TABLE rating DROP user_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
31
migrations/Version20260605222943.php
Normal file
31
migrations/Version20260605222943.php
Normal 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 Version20260605222943 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 rating ADD created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this down() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->addSql('ALTER TABLE rating DROP created_at');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Controller\Api;
|
namespace App\Controller\Api;
|
||||||
|
|
||||||
use App\Entity\Rating;
|
use App\Entity\Rating;
|
||||||
|
use App\Entity\User;
|
||||||
use App\Repository\RatingRepository;
|
use App\Repository\RatingRepository;
|
||||||
use App\Repository\SubjectRepository;
|
use App\Repository\SubjectRepository;
|
||||||
use App\Services\OutputService;
|
use App\Services\OutputService;
|
||||||
|
|
@ -11,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
use Symfony\Component\Security\Http\Attribute\CurrentUser;
|
||||||
|
|
||||||
class RatingController extends AbstractController
|
class RatingController extends AbstractController
|
||||||
{
|
{
|
||||||
|
|
@ -40,6 +42,8 @@ class RatingController extends AbstractController
|
||||||
EntityManagerInterface $em,
|
EntityManagerInterface $em,
|
||||||
OutputService $output,
|
OutputService $output,
|
||||||
SubjectRepository $subjectRepository,
|
SubjectRepository $subjectRepository,
|
||||||
|
#[CurrentUser]
|
||||||
|
User $user
|
||||||
): JsonResponse {
|
): JsonResponse {
|
||||||
$subject = $subjectRepository->find($request->getPayload()->get('subject_id'));
|
$subject = $subjectRepository->find($request->getPayload()->get('subject_id'));
|
||||||
|
|
||||||
|
|
@ -51,6 +55,7 @@ class RatingController extends AbstractController
|
||||||
|
|
||||||
$rating = new Rating();
|
$rating = new Rating();
|
||||||
$rating->setSubject($subject);
|
$rating->setSubject($subject);
|
||||||
|
$rating->setUser($user);
|
||||||
$rating->setScore($request->getPayload()->get('score'));
|
$rating->setScore($request->getPayload()->get('score'));
|
||||||
|
|
||||||
$em->persist($rating);
|
$em->persist($rating);
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,12 @@
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
use App\Repository\RatingRepository;
|
use App\Repository\RatingRepository;
|
||||||
|
use DateTimeImmutable;
|
||||||
use Doctrine\DBAL\Types\Types;
|
use Doctrine\DBAL\Types\Types;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
#[ORM\Entity(repositoryClass: RatingRepository::class)]
|
#[ORM\Entity(repositoryClass: RatingRepository::class)]
|
||||||
|
#[ORM\HasLifecycleCallbacks]
|
||||||
class Rating
|
class Rating
|
||||||
{
|
{
|
||||||
#[ORM\Id]
|
#[ORM\Id]
|
||||||
|
|
@ -14,6 +16,10 @@ class Rating
|
||||||
#[ORM\Column]
|
#[ORM\Column]
|
||||||
private ?int $id = null;
|
private ?int $id = null;
|
||||||
|
|
||||||
|
#[ORM\ManyToOne]
|
||||||
|
#[ORM\JoinColumn(nullable: false)]
|
||||||
|
private User $user;
|
||||||
|
|
||||||
#[ORM\ManyToOne(inversedBy: 'ratings')]
|
#[ORM\ManyToOne(inversedBy: 'ratings')]
|
||||||
#[ORM\JoinColumn(nullable: false)]
|
#[ORM\JoinColumn(nullable: false)]
|
||||||
private Subject $subject;
|
private Subject $subject;
|
||||||
|
|
@ -24,6 +30,9 @@ class Rating
|
||||||
#[ORM\Column(type: Types::TEXT, options: ['default' => ''])]
|
#[ORM\Column(type: Types::TEXT, options: ['default' => ''])]
|
||||||
private string $note = '';
|
private string $note = '';
|
||||||
|
|
||||||
|
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
|
||||||
|
private DateTimeImmutable $createdAt;
|
||||||
|
|
||||||
public function getId(): ?int
|
public function getId(): ?int
|
||||||
{
|
{
|
||||||
return $this->id;
|
return $this->id;
|
||||||
|
|
@ -41,6 +50,18 @@ class Rating
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getUser(): User
|
||||||
|
{
|
||||||
|
return $this->user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUser(User $user): static
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getScore(): ?int
|
public function getScore(): ?int
|
||||||
{
|
{
|
||||||
return $this->score;
|
return $this->score;
|
||||||
|
|
@ -64,4 +85,15 @@ class Rating
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[ORM\PrePersist]
|
||||||
|
public function setCreatedAtValue(): void
|
||||||
|
{
|
||||||
|
$this->createdAt = new \DateTimeImmutable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCreatedAt(): ?\DateTimeImmutable
|
||||||
|
{
|
||||||
|
return $this->createdAt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ final class RatingFactory extends PersistentObjectFactory
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'subject' => RestaurantFactory::new(),
|
'subject' => RestaurantFactory::new(),
|
||||||
|
'user' => UserFactory::new(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,10 @@ class RatingControllerTest extends WebTestCase
|
||||||
|
|
||||||
$this->assertNotNull($ratings);
|
$this->assertNotNull($ratings);
|
||||||
$this->assertCount(1, $ratings);
|
$this->assertCount(1, $ratings);
|
||||||
$this->assertInstanceOf(Rating::class, $ratings[0]);
|
|
||||||
|
$rating = $ratings[0];
|
||||||
|
$this->assertInstanceOf(Rating::class, $rating);
|
||||||
|
$this->assertSame($user->getId(), $rating->getUser()->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUpdate(): void
|
public function testUpdate(): void
|
||||||
|
|
@ -81,6 +84,7 @@ class RatingControllerTest extends WebTestCase
|
||||||
$client = static::createClient();
|
$client = static::createClient();
|
||||||
|
|
||||||
$user = UserFactory::createOne();
|
$user = UserFactory::createOne();
|
||||||
|
|
||||||
$client->loginUser($user);
|
$client->loginUser($user);
|
||||||
|
|
||||||
$subject = RestaurantFactory::createOne([
|
$subject = RestaurantFactory::createOne([
|
||||||
|
|
@ -89,6 +93,7 @@ class RatingControllerTest extends WebTestCase
|
||||||
|
|
||||||
$rating = RatingFactory::createOne([
|
$rating = RatingFactory::createOne([
|
||||||
'subject' => $subject,
|
'subject' => $subject,
|
||||||
|
'user' => $user,
|
||||||
'score' => 0,
|
'score' => 0,
|
||||||
'note' => 'first note',
|
'note' => 'first note',
|
||||||
]);
|
]);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue