From d0cca8e941aff73cd281555f573084ba8918bf9a Mon Sep 17 00:00:00 2001 From: Bastien Deshayes Date: Sun, 11 Aug 2019 23:36:40 +0200 Subject: [PATCH 1/4] =?UTF-8?q?WIP=20Gestion=20du=20r=C3=A9approvisionneme?= =?UTF-8?q?nt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/SupplyingController.php | 48 +++++++ src/Entity/Supplying.php | 156 +++++++++++++++++++++++ src/Migrations/Version20190811195955.php | 36 ++++++ 3 files changed, 240 insertions(+) create mode 100644 src/Controller/SupplyingController.php create mode 100644 src/Entity/Supplying.php create mode 100644 src/Migrations/Version20190811195955.php diff --git a/src/Controller/SupplyingController.php b/src/Controller/SupplyingController.php new file mode 100644 index 0000000..03b6331 --- /dev/null +++ b/src/Controller/SupplyingController.php @@ -0,0 +1,48 @@ +getDoctrine(); + $reappros = $em->getRepository(Supplying::class)->findAll(); + return $this->view($reappros)->setContext($this->getContext()); + } + + private function getContext() + { + $context = new Context(); + $context->setGroups(['Default']); + if ($this->isGranted(['ROLE_USER'])) { + $context->addGroup('privilegied'); + } + return $context; + } +} \ No newline at end of file diff --git a/src/Entity/Supplying.php b/src/Entity/Supplying.php new file mode 100644 index 0000000..4c25a45 --- /dev/null +++ b/src/Entity/Supplying.php @@ -0,0 +1,156 @@ +id; + } + + /** + * @return Article + */ + public function getArticle(): Article + { + return $this->article; + } + + /** + * @param Article $article + */ + public function setArticle(Article $article): void + { + $this->article = $article; + } + + /** + * @return int|null + */ + public function getQuantity(): ?int + { + return $this->quantity; + } + + /** + * @param int|null $quantity + */ + public function setQuantity(?int $quantity): void + { + $this->quantity = $quantity; + } + + /** + * @return \DateTime|null + */ + public function getCreationDate(): ?\DateTime + { + return $this->creationDate; + } + + /** + * @param \DateTime|null $creationDate + */ + public function setCreationDate(?\DateTime $creationDate): void + { + $this->creationDate = $creationDate; + } + + /** + * @return \DateTime|null + */ + public function getSupplyDate(): ?\DateTime + { + return $this->supplyDate; + } + + /** + * @param \DateTime|null $supplyDate + */ + public function setSupplyDate(?\DateTime $supplyDate): void + { + $this->supplyDate = $supplyDate; + } + + /** + * @return Boolean + */ + public function getOutOfStock(): Boolean + { + return $this->outOfStock; + } + + /** + * @param Boolean $outOfStock + */ + public function setOutOfStock(Boolean $outOfStock): void + { + $this->outOfStock = $outOfStock; + } + +} diff --git a/src/Migrations/Version20190811195955.php b/src/Migrations/Version20190811195955.php new file mode 100644 index 0000000..e74410a --- /dev/null +++ b/src/Migrations/Version20190811195955.php @@ -0,0 +1,36 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('CREATE TABLE SUPPLYING (id INT AUTO_INCREMENT NOT NULL, article_code BIGINT DEFAULT NULL, quantity INT DEFAULT NULL, creation_date DATE NOT NULL, supply_date DATE DEFAULT NULL, out_of_stock TINYINT(1) DEFAULT NULL, INDEX IDX_4D448068C757B799 (article_code), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'); + $this->addSql('ALTER TABLE SUPPLYING ADD CONSTRAINT FK_4D448068C757B799 FOREIGN KEY (article_code) REFERENCES ARTICLE (code)'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('DROP TABLE SUPPLYING'); + } +} From 3c7af551fc42815536014e2870413654059b2dbb Mon Sep 17 00:00:00 2001 From: Bastien Deshayes Date: Mon, 12 Aug 2019 00:41:10 +0200 Subject: [PATCH 2/4] Create supplying --- src/Controller/SupplyingController.php | 39 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Controller/SupplyingController.php b/src/Controller/SupplyingController.php index 03b6331..7360045 100644 --- a/src/Controller/SupplyingController.php +++ b/src/Controller/SupplyingController.php @@ -2,12 +2,15 @@ namespace App\Controller; +use App\Entity\Article; use App\Entity\Supplying; use FOS\RestBundle\Context\Context; use FOS\RestBundle\Controller\AbstractFOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use Nelmio\ApiDocBundle\Annotation\Model; +use phpDocumentor\Reflection\Types\Integer; use Swagger\Annotations as SWG; +use Symfony\Component\HttpFoundation\Response; /** * @Rest\Route("/supplying") @@ -32,8 +35,40 @@ class SupplyingController extends AbstractFOSRestController public function getSupplying() { $em = $this->getDoctrine(); - $reappros = $em->getRepository(Supplying::class)->findAll(); - return $this->view($reappros)->setContext($this->getContext()); + $supplyings = $em->getRepository(Supplying::class)->findAll(); + return $this->view($supplyings)->setContext($this->getContext()); + } + + /** + * @Rest\Post("/{id}/quantity/{quantity}") + * @Rest\View(statusCode = 201) + * + * @SWG\Response( + * response=201, + * description="Returns the supplying created", + * @SWG\Schema( + * type="array", + * @SWG\Items(ref=@Model(type=Supplying::class)) + * ) + * ) + * + * @SWG\Tag(name="supplying") + */ + public function createSupplying(Article $article, int $quantity) + { + $supplying = new Supplying(); + $supplying->setArticle($article); + $supplying->setCreationDate(new \DateTime()); + $supplying->setQuantity($quantity); + + $em = $this->getDoctrine()->getEntityManager(); + $em->persist($supplying); + $em->flush(); + + return $this->view( + $supplying, + Response::HTTP_CREATED + ); } private function getContext() From c20b45dadf79f8d06a1137b6012b49fc6b88ac9f Mon Sep 17 00:00:00 2001 From: Bastien Deshayes Date: Thu, 15 Aug 2019 23:15:26 +0200 Subject: [PATCH 3/4] =?UTF-8?q?WIP=20Gestion=20du=20r=C3=A9approvisionneme?= =?UTF-8?q?nt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 +- config/packages/fos_rest.yaml | 4 +- config/packages/jms_serializer.yaml | 4 ++ config/packages/sensio_framework_extra.yaml | 1 + src/Controller/SupplyingController.php | 50 +++++++++++++++++-- src/Entity/Supplying.php | 16 +++--- ...11195955.php => Version20190815150022.php} | 4 +- src/Repository/SupplyingRepository.php | 19 +++++++ src/Repository/TestRepository.php | 50 ------------------- 9 files changed, 83 insertions(+), 67 deletions(-) rename src/Migrations/{Version20190811195955.php => Version20190815150022.php} (82%) create mode 100644 src/Repository/SupplyingRepository.php delete mode 100644 src/Repository/TestRepository.php diff --git a/.env b/.env index 7542eb6..377f765 100644 --- a/.env +++ b/.env @@ -24,7 +24,7 @@ APP_SECRET=c6c9613ae69be21633161a3c12c60cd2 # Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" # Configure your db driver and server_version in config/packages/doctrine.yaml -DATABASE_URL=mysql://root:root@127.0.0.1:3306/kaso +DATABASE_URL=mysql://lelefan:lelefan@127.0.0.1:3306/kaso ###< doctrine/doctrine-bundle ### ###> hwi/oauth-bundle ### diff --git a/config/packages/fos_rest.yaml b/config/packages/fos_rest.yaml index d82692e..079c90e 100644 --- a/config/packages/fos_rest.yaml +++ b/config/packages/fos_rest.yaml @@ -1,8 +1,10 @@ # Read the documentation: https://symfony.com/doc/master/bundles/FOSRestBundle/index.html fos_rest: -# param_fetcher_listener: true + param_fetcher_listener: force # allowed_methods_listener: true # routing_loader: true + body_converter: + enabled: true view: view_response_listener: true formats: diff --git a/config/packages/jms_serializer.yaml b/config/packages/jms_serializer.yaml index 2b2a97a..ca1b2a2 100644 --- a/config/packages/jms_serializer.yaml +++ b/config/packages/jms_serializer.yaml @@ -5,6 +5,10 @@ jms_serializer: default_context: serialization: enable_max_depth_checks: true + handlers: + datetime: + default_format: "Y-m-d H:i:s" + default_timezone: "UTC" # metadata: # auto_detection: false # directories: diff --git a/config/packages/sensio_framework_extra.yaml b/config/packages/sensio_framework_extra.yaml index 1821ccc..5d3ab7a 100644 --- a/config/packages/sensio_framework_extra.yaml +++ b/config/packages/sensio_framework_extra.yaml @@ -1,3 +1,4 @@ sensio_framework_extra: + request: { converters: true } router: annotations: false diff --git a/src/Controller/SupplyingController.php b/src/Controller/SupplyingController.php index 7360045..84c1ad0 100644 --- a/src/Controller/SupplyingController.php +++ b/src/Controller/SupplyingController.php @@ -11,7 +11,8 @@ use phpDocumentor\Reflection\Types\Integer; use Swagger\Annotations as SWG; use Symfony\Component\HttpFoundation\Response; - +use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; +use Symfony\Component\HttpFoundation\Request; /** * @Rest\Route("/supplying") */ @@ -35,12 +36,14 @@ class SupplyingController extends AbstractFOSRestController public function getSupplying() { $em = $this->getDoctrine(); - $supplyings = $em->getRepository(Supplying::class)->findAll(); + $supplyings = $em->getRepository(Supplying::class)->getOngoing(); return $this->view($supplyings)->setContext($this->getContext()); } /** - * @Rest\Post("/{id}/quantity/{quantity}") + * @Rest\Post("") + * @Rest\QueryParam(name="articleCode") + * @Rest\QueryParam(name="quantity") * @Rest\View(statusCode = 201) * * @SWG\Response( @@ -54,14 +57,17 @@ public function getSupplying() * * @SWG\Tag(name="supplying") */ - public function createSupplying(Article $article, int $quantity) + public function createSupplying($articleCode, $quantity) { + $em = $this->getDoctrine()->getEntityManager(); + $article = $em->getRepository(Article::class)->findOneByCode($articleCode); + $supplying = new Supplying(); $supplying->setArticle($article); $supplying->setCreationDate(new \DateTime()); $supplying->setQuantity($quantity); + $supplying->setOutOfStock(false); - $em = $this->getDoctrine()->getEntityManager(); $em->persist($supplying); $em->flush(); @@ -71,6 +77,40 @@ public function createSupplying(Article $article, int $quantity) ); } + /** + * @Rest\Put("/{id}") + * @Rest\View(statusCode = 204) + * + * @ParamConverter("updates", converter="fos_rest.request_body") + * + * @SWG\Response( + * response=204, + * description="Supplying updated", + * @SWG\Schema( + * type="array", + * @SWG\Items(ref=@Model(type=Supplying::class)) + * ) + * ) + * + * @SWG\Tag(name="supplying") + */ + public function updateSupplying(int $id, Supplying $updates) + { + $em = $this->getDoctrine()->getEntityManager(); + $supplying = $em->getRepository(Supplying::class)->findOneById($id); + + $supplying->setQuantity($updates->getQuantity()); + $supplying->setOutOfStock($updates->getOutOfStock()); + $supplying->setSupplyDate($updates->getSupplyDate()); + + $em->flush(); + + return $this->view( + null, + Response::HTTP_NO_CONTENT + ); + } + private function getContext() { $context = new Context(); diff --git a/src/Entity/Supplying.php b/src/Entity/Supplying.php index 4c25a45..3a80c28 100644 --- a/src/Entity/Supplying.php +++ b/src/Entity/Supplying.php @@ -10,7 +10,7 @@ * Supplying * * @ORM\Table(name="SUPPLYING") - * @ORM\Entity(readOnly=false) + * @ORM\Entity(repositoryClass="App\Repository\SupplyingRepository") */ class Supplying { @@ -28,7 +28,7 @@ class Supplying * Article * @var Article * @ORM\ManyToOne(targetEntity="App\Entity\Article") - * @ORM\JoinColumn(name="article_code", referencedColumnName="code") + * @ORM\JoinColumn(name="article_code", referencedColumnName="code", nullable=false) * @Serializer\MaxDepth(depth=2) */ private $article; @@ -45,7 +45,7 @@ class Supplying * Date de création * @var \DateTime|null * - * @ORM\Column(name="creation_date", type="date", nullable=false) + * @ORM\Column(name="creation_date", type="datetime", nullable=false) */ private $creationDate; @@ -53,15 +53,15 @@ class Supplying * Date de réapprovisionnement * @var \DateTime|null * - * @ORM\Column(name="supply_date", type="date", nullable=true) + * @ORM\Column(name="supply_date", type="datetime", nullable=true) */ private $supplyDate; /** * En rupture de stock (non trouvé) * - * @var Boolean - * @ORM\Column(name="out_of_stock", type="boolean", nullable=true) + * @var boolean + * @ORM\Column(name="out_of_stock", type="boolean", nullable=false) */ private $outOfStock; @@ -140,7 +140,7 @@ public function setSupplyDate(?\DateTime $supplyDate): void /** * @return Boolean */ - public function getOutOfStock(): Boolean + public function getOutOfStock(): bool { return $this->outOfStock; } @@ -148,7 +148,7 @@ public function getOutOfStock(): Boolean /** * @param Boolean $outOfStock */ - public function setOutOfStock(Boolean $outOfStock): void + public function setOutOfStock(bool $outOfStock): void { $this->outOfStock = $outOfStock; } diff --git a/src/Migrations/Version20190811195955.php b/src/Migrations/Version20190815150022.php similarity index 82% rename from src/Migrations/Version20190811195955.php rename to src/Migrations/Version20190815150022.php index e74410a..d222afc 100644 --- a/src/Migrations/Version20190811195955.php +++ b/src/Migrations/Version20190815150022.php @@ -10,7 +10,7 @@ /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20190811195955 extends AbstractMigration +final class Version20190815150022 extends AbstractMigration { public function getDescription() : string { @@ -22,7 +22,7 @@ public function up(Schema $schema) : void // this up() migration is auto-generated, please modify it to your needs $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); - $this->addSql('CREATE TABLE SUPPLYING (id INT AUTO_INCREMENT NOT NULL, article_code BIGINT DEFAULT NULL, quantity INT DEFAULT NULL, creation_date DATE NOT NULL, supply_date DATE DEFAULT NULL, out_of_stock TINYINT(1) DEFAULT NULL, INDEX IDX_4D448068C757B799 (article_code), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'); + $this->addSql('CREATE TABLE SUPPLYING (id INT AUTO_INCREMENT NOT NULL, article_code BIGINT NOT NULL, quantity INT DEFAULT NULL, creation_date DATETIME NOT NULL, supply_date DATETIME DEFAULT NULL, out_of_stock TINYINT(1) NOT NULL, INDEX IDX_4D448068C757B799 (article_code), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'); $this->addSql('ALTER TABLE SUPPLYING ADD CONSTRAINT FK_4D448068C757B799 FOREIGN KEY (article_code) REFERENCES ARTICLE (code)'); } diff --git a/src/Repository/SupplyingRepository.php b/src/Repository/SupplyingRepository.php new file mode 100644 index 0000000..ea44e0d --- /dev/null +++ b/src/Repository/SupplyingRepository.php @@ -0,0 +1,19 @@ +createQueryBuilder('s') + ->where('s.supplyDate is null') + ->orderBy('s.id', 'ASC') + ->getQuery() + ->getResult() + ; + } +} diff --git a/src/Repository/TestRepository.php b/src/Repository/TestRepository.php deleted file mode 100644 index c626a79..0000000 --- a/src/Repository/TestRepository.php +++ /dev/null @@ -1,50 +0,0 @@ -createQueryBuilder('t') - ->andWhere('t.exampleField = :val') - ->setParameter('val', $value) - ->orderBy('t.id', 'ASC') - ->setMaxResults(10) - ->getQuery() - ->getResult() - ; - } - */ - - /* - public function findOneBySomeField($value): ?Test - { - return $this->createQueryBuilder('t') - ->andWhere('t.exampleField = :val') - ->setParameter('val', $value) - ->getQuery() - ->getOneOrNullResult() - ; - } - */ -} From cd772c18c4011bcf607828a540327d35c89abc1f Mon Sep 17 00:00:00 2001 From: Bastien Deshayes Date: Sun, 25 Aug 2019 16:41:43 +0200 Subject: [PATCH 4/4] Fix .env --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index 377f765..7542eb6 100644 --- a/.env +++ b/.env @@ -24,7 +24,7 @@ APP_SECRET=c6c9613ae69be21633161a3c12c60cd2 # Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" # Configure your db driver and server_version in config/packages/doctrine.yaml -DATABASE_URL=mysql://lelefan:lelefan@127.0.0.1:3306/kaso +DATABASE_URL=mysql://root:root@127.0.0.1:3306/kaso ###< doctrine/doctrine-bundle ### ###> hwi/oauth-bundle ###