4 - Add createdAt column

This commit is contained in:
myrmidex 2026-06-05 22:32:53 +00:00
parent 6c02f9b4c1
commit 5821209e4d
6 changed files with 110 additions and 1 deletions

View 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');
}
}

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 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');
}
}

View file

@ -3,6 +3,7 @@
namespace App\Controller\Api;
use App\Entity\Rating;
use App\Entity\User;
use App\Repository\RatingRepository;
use App\Repository\SubjectRepository;
use App\Services\OutputService;
@ -11,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
class RatingController extends AbstractController
{
@ -40,6 +42,8 @@ class RatingController extends AbstractController
EntityManagerInterface $em,
OutputService $output,
SubjectRepository $subjectRepository,
#[CurrentUser]
User $user
): JsonResponse {
$subject = $subjectRepository->find($request->getPayload()->get('subject_id'));
@ -51,6 +55,7 @@ class RatingController extends AbstractController
$rating = new Rating();
$rating->setSubject($subject);
$rating->setUser($user);
$rating->setScore($request->getPayload()->get('score'));
$em->persist($rating);

View file

@ -3,10 +3,12 @@
namespace App\Entity;
use App\Repository\RatingRepository;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: RatingRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Rating
{
#[ORM\Id]
@ -14,6 +16,10 @@ class Rating
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private User $user;
#[ORM\ManyToOne(inversedBy: 'ratings')]
#[ORM\JoinColumn(nullable: false)]
private Subject $subject;
@ -24,6 +30,9 @@ class Rating
#[ORM\Column(type: Types::TEXT, options: ['default' => ''])]
private string $note = '';
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private DateTimeImmutable $createdAt;
public function getId(): ?int
{
return $this->id;
@ -41,6 +50,18 @@ class Rating
return $this;
}
public function getUser(): User
{
return $this->user;
}
public function setUser(User $user): static
{
$this->user = $user;
return $this;
}
public function getScore(): ?int
{
return $this->score;
@ -64,4 +85,15 @@ class Rating
return $this;
}
#[ORM\PrePersist]
public function setCreatedAtValue(): void
{
$this->createdAt = new \DateTimeImmutable();
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
}

View file

@ -33,6 +33,7 @@ final class RatingFactory extends PersistentObjectFactory
{
return [
'subject' => RestaurantFactory::new(),
'user' => UserFactory::new(),
];
}

View file

@ -73,7 +73,10 @@ class RatingControllerTest extends WebTestCase
$this->assertNotNull($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
@ -81,6 +84,7 @@ class RatingControllerTest extends WebTestCase
$client = static::createClient();
$user = UserFactory::createOne();
$client->loginUser($user);
$subject = RestaurantFactory::createOne([
@ -89,6 +93,7 @@ class RatingControllerTest extends WebTestCase
$rating = RatingFactory::createOne([
'subject' => $subject,
'user' => $user,
'score' => 0,
'note' => 'first note',
]);