<?php
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Nelmio\ApiDocBundle\Annotation\Security;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use App\Entity\Bike;
use App\Entity\Agency;
use App\Form\BikeType;
use App\Repository\BikeRepository;
use App\Repository\OrderRepository;
use App\Service\BikeService;
use App\Service\UploadFileService;
class BikeController extends AbstractController
{
/**
* @var BikeService
*/
private $BikeService;
public function __construct(BikeService $BikeService)
{
$this->BikeService = $BikeService;
}
/**
* @OA\Tag(name="Bike")
* @Security(name="Bearer")
* @Route("/api/liste/bike", methods={"GET"})
*/
public function liste()
{
$liste = $this->BikeService->getBike();
$jsonBike = [];
foreach ($liste as $key => $bike){
$jsonBike[$key] = $this->BikeService->toJson($bike);
}
return new JsonResponse($jsonBike);
}
/**
* @OA\Tag(name="Bike")
* @Security(name="Bearer")
* @Route("/api/liste/bike_by_agency/{id}", methods={"GET"})
*/
public function listeBikeByAgency(Agency $agency)
{
$liste = $this->BikeService->getBike(array('agency'=> $agency));
$jsonBike = [];
foreach ($liste as $key => $bike){
$jsonBike[$key] = $this->BikeService->toJson($bike);
}
return new JsonResponse($jsonBike);
}
/**
* @OA\Tag(name="Bike")
* @Security(name="Bearer")
* @Route("/api/bike/{id}", methods={"GET"})
*/
public function getBike(Bike $bike)
{
$jsonBike = $this->BikeService->toJson($bike);
return new JsonResponse($jsonBike);
}
/**
*@OA\RequestBody( required=true, @OA\JsonContent (
* @OA\Property(
* property="start",
* type="string",
* example="2023-03-14"
* ),
* @OA\Property(
* property="end",
* type="string",
* example="2023-03-18"
* ),
* @OA\Property(
* property="agency",
* type="string",
* example="2"
* ),
*),)
* @OA\Response (
* response=201,
* description="Success",
* )
* @OA\Response(
* response="300",
* description="FORMTYPE IS INVALID",
* )
* @OA\Response(
* response="400",
* description="BAD REQUEST",
* )
* @OA\Response(
* response="401",
* description="Unauthorized",
* )
* @OA\Tag(name="Bike")
* @Route("/api/get_free_bikes", methods={"POST"})
*/
public function getFreeBikes(Request $request, BikeRepository $BikeRepository, OrderRepository $OrderRepository)
{
$data = json_decode($request->getContent(), true);
$start = new \DateTime($data['start']);
$end = new \DateTime($data['end']);
$ids = [];
$freeOrderBikes = $OrderRepository->getFreeBikes($data['agency'], $start, $end);
foreach($freeOrderBikes as $bike){
array_push($ids, $bike['id']);
}
$jsonBike = [];
$liste = $this->BikeService->getBike(array('agency'=> $data['agency']));
foreach ($liste as $bike) {
if (!in_array($bike->getId(), $ids)) {
if($bike->isStatut()){
array_push($jsonBike, $this->BikeService->toJson($bike));
}
}
}
return new JsonResponse($jsonBike);
}
/**
*@OA\RequestBody( required=true, @OA\JsonContent (
* @OA\Property(
* property="bike",
* type="string",
* example="1"
* ),
* @OA\Property(
* property="statut",
* type="boolean",
* example="true"
* ),
*),)
* @OA\Response (
* response=201,
* description="Success",
* )
* @OA\Response(
* response="300",
* description="FORMTYPE IS INVALID",
* )
* @OA\Response(
* response="400",
* description="BAD REQUEST",
* )
* @OA\Response(
* response="401",
* description="Unauthorized",
* )
* @OA\Tag(name="Bike")
* @Route("/api/update_statut/bike", methods={"PUT"})
*/
public function updateStatut(Request $request)
{
$data = json_decode($request->getContent(), true);
$bike = $this->BikeService->get($data['bike']);
if($bike){
$bike->setStatut($data['statut']);
$bike = $this->BikeService->persist($bike);
}
return new JsonResponse($bike->getId());
}
/**
*@OA\RequestBody( required=true, @OA\JsonContent (
* @OA\Property(
* property="agency",
* type="string",
* example="1"
* ),
* @OA\Property(
* property="category",
* type="string",
* example="1"
* ),
* @OA\Property(
* property="type",
* type="string",
* example="1"
* ),
* @OA\Property(
* property="size",
* type="string",
* example="1"
* ),@OA\Property(
* property="brand",
* type="string",
* example="1"
* ),@OA\Property(
* property="tariff",
* type="string",
* example="1"
* ),@OA\Property(
* property="participant",
* type="string",
* example="1"
* ),@OA\Property(
* property="refernce",
* type="string",
* example="11111"
* ),@OA\Property(
* property="description",
* type="string",
* example="description"
* ),@OA\Property(
* property="statut",
* type="string",
* example="true"
* ),
*
*),)
* @OA\Response (
* response=201,
* description="Success",
* )
* @OA\Response(
* response="300",
* description="FORMTYPE IS INVALID",
* )
* @OA\Response(
* response="400",
* description="BAD REQUEST",
* )
* @OA\Response(
* response="401",
* description="Unauthorized",
* )
* @Security(name="Bearer")
* @OA\Tag(name="Bike")
* @Route("/api/create/bike", methods={"POST"})
*/
public function createBike(Request $request, UploadFileService $UploadFileService)
{
$data = $request->request->all();
$bike = new Bike();
$form = $this->createForm(BikeType::class, $bike, array('csrf_protection' => false));
$form->submit($data);
$principal_image = $request->files->get('principal_image');
if ($principal_image) {
$principalImageName = $UploadFileService->upload($principal_image);
$bike->setPrincipalImage($principalImageName);
}
$multiple_image = $request->files->get('multiple_image');
if ($multiple_image) {
foreach($multiple_image as $image){
$multipleImageName[] = $UploadFileService->upload($image);
}
$bike->setMultipleImage($multipleImageName);
}
$bike = $this->BikeService->persist($bike);
return new JsonResponse($bike->getId());
}
/**
*@OA\RequestBody( required=true, @OA\JsonContent (
* @OA\Property(
* property="agency",
* type="string",
* example="1"
* ),
* @OA\Property(
* property="category",
* type="string",
* example="1"
* ),
* @OA\Property(
* property="type",
* type="string",
* example="1"
* ),
* @OA\Property(
* property="size",
* type="string",
* example="1"
* ),@OA\Property(
* property="brand",
* type="string",
* example="1"
* ),@OA\Property(
* property="tariff",
* type="string",
* example="1"
* ),@OA\Property(
* property="participant",
* type="string",
* example="1"
* ),@OA\Property(
* property="refernce",
* type="string",
* example="11111"
* ),@OA\Property(
* property="description",
* type="string",
* example="description"
* ),@OA\Property(
* property="statut",
* type="string",
* example="true"
* ),
*),)
* @OA\Response (
* response=201,
* description="Success",
* )
* @OA\Response(
* response="300",
* description="FORMTYPE IS INVALID",
* )
* @OA\Response(
* response="400",
* description="BAD REQUEST",
* )
* @OA\Response(
* response="401",
* description="Unauthorized",
* )
* @Security(name="Bearer")
* @OA\Tag(name="Bike")
* @Route("/api/update/bike/{id}", methods={"POST"})
*/
public function updateBike(Request $request, Bike $bike, UploadFileService $UploadFileService)
{
$data = $request->request->all();
$form = $this->createForm(BikeType::class, $bike, array('csrf_protection' => false));
$form->submit($data, false);
$principal_image = $request->files->get('principal_image');
if ($principal_image) {
$principalImageName = $UploadFileService->upload($principal_image);
$bike->setPrincipalImage($principalImageName);
}
$multiple_image = $request->files->get('multiple_image');
if ($multiple_image) {
foreach($multiple_image as $image){
$multipleImageName[] = $UploadFileService->upload($image);
}
$bike->setMultipleImage($multipleImageName);
}
$bike = $this->BikeService->persist($bike);
return new JsonResponse($bike->getId());
}
/**
* @OA\Tag(name="Bike")
* @Security(name="Bearer")
* @Route("/api/delete/bike/{id}", methods={"DELETE"})
*/
public function deleteBike($id, Request $request)
{
$Bike = $this->BikeService->get($id);
try {
$this->BikeService->remove($Bike);
$request->getSession()->getFlashBag()->add('success', 'bike supprimé avec succès !');
} catch (\Exception $exception) {
$request->getSession()->getFlashBag()->add('danger', 'un ou plusieurs produit liés à cette entité !');
}
return new JsonResponse($request);
}
}