Untitled

 avatar
unknown
plain_text
a year ago
2.7 kB
5
Indexable
<?php

namespace App\Controller;

use App\Entity\Exam;
use App\Repository\ExamRepository;
use App\Repository\TestRepository;
use App\Repository\ExamCategoryRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use OpenApi\Attributes as OA;
use Nelmio\ApiDocBundle\Annotation\Security;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class ExamCategoryController extends AbstractController
{
    private $examCategoryRepository;
    private $manager;

    public function __construct(ExamCategoryRepository $examCategoryRepository,EntityManagerInterface $manager)
    {
        $this->examCategoryRepository = $examCategoryRepository;
        $this->manager = $manager;
    }



    #[Route('/examcategories/{id}', name: 'examCategories_delete', methods: 'DELETE')]
    #[OA\Tag(name: 'ExamCategories')]
    #[Security(name: 'Bearer')]
    #[OA\Delete(
        description: 'Remove  ExamCategory by ID',
        summary: 'Remove  ExamCategory by ID',
        tags: ['ExamCategories'],
        parameters: [
            new OA\Parameter(
                name: 'id',
                description: 'ID of the ExamCategory',
                in: 'path',
                required: true,
                schema: new OA\Schema(type: 'integer', format: 'int64')
            )
        ],
        responses: [
            new OA\Response(
                response: '200',
                description: 'Successful operation',
            ),
            new OA\Response(
                response: '404',
                description: 'ExamCategory not found',
                content: new OA\MediaType(mediaType: 'application/json')
            )
        ]
    )]
    public function delete($id, TestRepository $testRepository): JsonResponse
    {
        $cc = $this->examCategoryRepository->find($id);
        if (!$cc) {
            return new JsonResponse(["message"=>"ExamCategory not found"], Response::HTTP_NOT_FOUND);
        }
        $tests = $testRepository->findBy(['exam'=>$cc->getExam()->getId()]);
        if(count($tests)) {
            return new JsonResponse(["message"=>"ExamCategory can't be removed because exam tests are already generated."], Response::HTTP_NOT_FOUND);
        }
        $this->manager->remove($cc);
        $this->manager->flush();
        return new JsonResponse(["message"=>"ExamCategory successfully removed"], Response::HTTP_OK);
    }

}
Editor is loading...
Leave a Comment