Commit a6b48bf7 authored by Mat's avatar Mat

OffreAPIController, create et read + fonction privée pour créer le code référence

parent 90037931
......@@ -57,6 +57,8 @@ doctrine:
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: '%database_path%'
mapping_types:
enum: string
orm:
auto_generate_proxy_classes: '%kernel.debug%'
......@@ -84,6 +86,7 @@ fos_user:
fos_js_routing:
routes_to_expose:
- api_legume_*
- api_offre_*
# VichUploaderBundle Configuration
vich_uploader:
......
<?php
namespace APIBundle\Controller;
use APIBundle\Form\OffreAPIType;
use PotageBundle\Entity\Offre;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class OffreAPIController extends MasterAPIController
{
/**
* @return string
* @throws \Doctrine\ORM\NonUniqueResultException
*/
private function newReference()
{
$now = new \DateTime();
$em = $this->getDoctrine()->getManager();
$num = $em->getRepository('PotageBundle:Offre')->findOneLastReference();
$last = intval(substr($num['reference'], -2));
$last++;
$last = sprintf("%02d", $last);
return 'OF' . $now->format('ymd') . $last;
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
function createAction(Request $request)
{
$reference = $this->newReference();
$offre = new Offre($reference);
$form = $this->createForm(OffreAPIType::class, $offre);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
dump($form);
$em->persist($offre);
$em->flush();
return $this->api($offre);
}
return $this->api($form , Response::HTTP_BAD_REQUEST);
}
/**
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function readAction()
{
$em = $this->getDoctrine()->getManager();
$offres = $em->getRepository('PotageBundle:Offre')->findAllForRead();
return $this->api($offres);
}
}
<?php
namespace APIBundle\Form;
use PotageBundle\Form\Offre\OffreType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OffreAPIType extends OffreType
{
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('csrf_protection', false);
// TODO temporaire, le temps de régler les validations au niveau des Asserts et des FormType
$resolver->setDefault('attr', array('novalidate' => true));
}
}
......@@ -50,3 +50,16 @@ api_legume_search:
_controller: APIBundle:LegumeAPI:search
methods: [ GET ]
## CRUD OffreAPI
api_offre_create:
path: /offre/ajouter
defaults:
_controller: APIBundle:OffreAPI:create
methods: [ POST ]
api_offre_read:
path: /offres
defaults:
_controller: APIBundle:OffreAPI:read
methods: [ GET ]
\ No newline at end of file
......@@ -74,6 +74,7 @@ class Offre
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime('0000-00-00 00:00:00');
}
/**
*
* @ORM\PreUpdate()
......@@ -83,6 +84,17 @@ class Offre
$this->updatedAt = new \DateTime();
}
/**
* Offre constructor.
*
* @param string $reference
*/
public function __construct($reference)
{
$this->offreLegumes = new \Doctrine\Common\Collections\ArrayCollection();
$this->status = 'draft';
$this->reference = $reference;
}
/**
......@@ -214,13 +226,6 @@ class Offre
{
return $this->endedAt;
}
/**
* Constructor
*/
public function __construct()
{
$this->offreLegumes = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add offreLegume
......
<?php
namespace PotageBundle\Form\Offre;
use PotageBundle\Entity\Offre;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OffreType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('endedAt', DateTimeType::class, array(
'label' => "Date de clôture de l'offre",
'label_attr' => array('class' => 'col-form-label'),
'attr' => array(
'class' => 'form-control form-control-sm',
),
));
$builder->add('sauver', SubmitType::class, array(
'label' => 'Enregistrer',
'attr' => array('class' => 'btn btn-dark mb-2')
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('data_class', Offre::class);
$resolver->setDefault('attr', array(
'class' => 'formulaire formulaire_offre',
));
}
/**
* @return null|string
*/
public function getBlockPrefix()
{
return "offre";
}
}
\ No newline at end of file
......@@ -10,4 +10,29 @@ namespace PotageBundle\Repository;
*/
class OffreRepository extends \Doctrine\ORM\EntityRepository
{
/**
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneLastReference()
{
$qb = $this->createQueryBuilder('o')
->select('o.reference')
->orderBy('o.reference', 'DESC')
->setMaxResults(1);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @return array
*/
public function findAllForRead()
{
$qb = $this->createQueryBuilder('o')
->orderBy('o.status')
->addOrderBy('o.reference', 'DESC');
return $qb->getQuery()->getResult();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment