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 new file mode 100644 index 0000000..84c1ad0 --- /dev/null +++ b/src/Controller/SupplyingController.php @@ -0,0 +1,123 @@ +getDoctrine(); + $supplyings = $em->getRepository(Supplying::class)->getOngoing(); + return $this->view($supplyings)->setContext($this->getContext()); + } + + /** + * @Rest\Post("") + * @Rest\QueryParam(name="articleCode") + * @Rest\QueryParam(name="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($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->persist($supplying); + $em->flush(); + + return $this->view( + $supplying, + Response::HTTP_CREATED + ); + } + + /** + * @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(); + $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..3a80c28 --- /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(): bool + { + return $this->outOfStock; + } + + /** + * @param Boolean $outOfStock + */ + public function setOutOfStock(bool $outOfStock): void + { + $this->outOfStock = $outOfStock; + } + +} diff --git a/src/Migrations/Version20190815150022.php b/src/Migrations/Version20190815150022.php new file mode 100644 index 0000000..d222afc --- /dev/null +++ b/src/Migrations/Version20190815150022.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 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)'); + } + + 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'); + } +} 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() - ; - } - */ -}