2026-06-03 23:49:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controller\Api;
|
|
|
|
|
|
|
|
|
|
use App\Entity\Restaurant;
|
2026-06-05 13:15:30 +02:00
|
|
|
use App\Entity\User;
|
2026-06-03 23:49:30 +02:00
|
|
|
use App\Repository\SubjectRepository;
|
|
|
|
|
use App\Services\OutputService;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
use Symfony\Component\Routing\Attribute\Route;
|
2026-06-05 13:15:30 +02:00
|
|
|
use Symfony\Component\Security\Http\Attribute\CurrentUser;
|
2026-06-03 23:49:30 +02:00
|
|
|
|
|
|
|
|
class SubjectController extends AbstractController
|
|
|
|
|
{
|
|
|
|
|
#[Route('/api/subjects', methods: ['GET'])]
|
|
|
|
|
public function index(
|
|
|
|
|
SubjectRepository $repo,
|
|
|
|
|
OutputService $output,
|
|
|
|
|
): JsonResponse {
|
|
|
|
|
return $this->json($output->success(['subjects' => $repo->findAll()]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/api/subjects', methods: ['POST'])]
|
|
|
|
|
public function create(
|
|
|
|
|
Request $request,
|
|
|
|
|
EntityManagerInterface $em,
|
2026-06-05 13:15:30 +02:00
|
|
|
#[CurrentUser]
|
|
|
|
|
User $user,
|
2026-06-03 23:49:30 +02:00
|
|
|
): JsonResponse {
|
|
|
|
|
$subject = new Restaurant();
|
|
|
|
|
$subject->setName($request->getPayload()->get('name'));
|
2026-06-05 13:15:30 +02:00
|
|
|
$subject->setCreatedBy($user);
|
2026-06-03 23:49:30 +02:00
|
|
|
|
|
|
|
|
$em->persist($subject);
|
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json(null, 201);
|
|
|
|
|
}
|
2026-06-05 13:57:21 +02:00
|
|
|
|
|
|
|
|
#[Route('/api/subjects/{id}', methods: ['PATCH'])]
|
|
|
|
|
public function update(
|
|
|
|
|
int $id,
|
|
|
|
|
Request $request,
|
|
|
|
|
EntityManagerInterface $em,
|
|
|
|
|
OutputService $output,
|
|
|
|
|
SubjectRepository $repository,
|
|
|
|
|
): JsonResponse {
|
|
|
|
|
$subject = $repository->findById($id);
|
|
|
|
|
|
|
|
|
|
if ($subject === null) {
|
|
|
|
|
return $this->json(
|
|
|
|
|
$output->error(["UNKNOWN_RATING"])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$payload = $request->getPayload();
|
|
|
|
|
|
|
|
|
|
if ($payload->has('name')) {
|
|
|
|
|
$subject->setName($payload->getString('name'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json(null, 202);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/api/subjects/{id}', methods: ['DELETE'])]
|
|
|
|
|
public function delete(
|
|
|
|
|
int $id,
|
|
|
|
|
EntityManagerInterface $em,
|
|
|
|
|
OutputService $output,
|
|
|
|
|
SubjectRepository $repository,
|
|
|
|
|
): JsonResponse {
|
|
|
|
|
$subject = $repository->findById($id);
|
|
|
|
|
|
|
|
|
|
if ($subject === null) {
|
|
|
|
|
return $this->json($output->error(["UNKNOWN_RATING"]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$em->remove($subject);
|
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json(null, 204);
|
|
|
|
|
}
|
2026-06-03 23:49:30 +02:00
|
|
|
}
|