Update rating + add note field

This commit is contained in:
myrmidex 2026-06-04 22:20:33 +00:00
parent a3963d093e
commit eac85a9045
4 changed files with 116 additions and 0 deletions

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 Version20260604220935 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 note TEXT DEFAULT \'\' 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 note');
}
}

View file

@ -59,4 +59,35 @@ class RatingController extends AbstractController
return $this->json(null, 201);
}
#[Route('/api/ratings/{id}', methods: ['PATCH'])]
public function update(
int $id,
Request $request,
EntityManagerInterface $em,
OutputService $output,
RatingRepository $ratingRepository,
): JsonResponse {
$rating = $ratingRepository->findById($id);
if ($rating === null) {
return $this->json(
$output->error(["UNKNOWN_RATING"])
);
}
$payload = $request->getPayload();
if ($payload->has('score')) {
$rating->setScore($payload->getInt('score'));
}
if ($payload->has('note')) {
$rating->setNote($payload->getString('note'));
}
$em->flush();
return $this->json(null, 201);
}
}

View file

@ -21,6 +21,9 @@ class Rating
#[ORM\Column(type: Types::SMALLINT, nullable: true)]
private ?int $score = null;
#[ORM\Column(type: Types::TEXT, options: ['default' => ''])]
private string $note = '';
public function getId(): ?int
{
return $this->id;
@ -49,4 +52,16 @@ class Rating
return $this;
}
public function getNote(): string
{
return $this->note;
}
public function setNote(?string $note = ''): static
{
$this->note = $note;
return $this;
}
}

View file

@ -9,6 +9,7 @@ use App\Factory\RestaurantFactory;
use App\Factory\UserFactory;
use App\Repository\RatingRepository;
use App\Repository\SubjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class RatingControllerTest extends WebTestCase
@ -76,4 +77,42 @@ class RatingControllerTest extends WebTestCase
$this->assertCount(1, $ratings);
$this->assertInstanceOf(Rating::class, $ratings[0]);
}
public function testUpdate(): void
{
$client = static::createClient();
$user = UserFactory::createOne();
$client->loginUser($user);
$subject = RestaurantFactory::createOne([
'createdBy' => $user,
]);
$rating = RatingFactory::createOne([
'subject' => $subject,
'score' => 0,
'note' => 'first note',
]);
$client->request(
'PATCH',
'/api/ratings/' . $rating->getId(),
server: ['CONTENT_TYPE' => 'application/json'],
content: json_encode([
'score' => 8,
'note' => 'Other note',
]),
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
$em = static::getContainer()->get(EntityManagerInterface::class);
$em->clear();
$updated = $em->getRepository(Rating::class)->find($rating->getId());
$this->assertSame(8, $updated->getScore());
$this->assertSame('Other note', $updated->getNote());
}
}