Skip to content

Commit

Permalink
Merge pull request #3 from elefan-grenoble/feature/reappro
Browse files Browse the repository at this point in the history
Feature/reappro
  • Loading branch information
museOnSite authored Aug 25, 2019
2 parents 1fbd5ec + cd772c1 commit 75acc4e
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 51 deletions.
4 changes: 3 additions & 1 deletion config/packages/fos_rest.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 4 additions & 0 deletions config/packages/jms_serializer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions config/packages/sensio_framework_extra.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sensio_framework_extra:
request: { converters: true }
router:
annotations: false
123 changes: 123 additions & 0 deletions src/Controller/SupplyingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

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;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
/**
* @Rest\Route("/supplying")
*/
class SupplyingController extends AbstractFOSRestController
{
/**
* @Rest\Get("")
* @Rest\View(statusCode = 200)
*
* @SWG\Response(
* response=200,
* description="Returns the supplying list from kaso database",
* @SWG\Schema(
* type="array",
* @SWG\Items(ref=@Model(type=Supplying::class))
* )
* )
*
* @SWG\Tag(name="supplying")
*/
public function getSupplying()
{
$em = $this->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;
}
}
156 changes: 156 additions & 0 deletions src/Entity/Supplying.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use phpDocumentor\Reflection\Types\Boolean;

/**
* Supplying
*
* @ORM\Table(name="SUPPLYING")
* @ORM\Entity(repositoryClass="App\Repository\SupplyingRepository")
*/
class Supplying
{

/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* Article
* @var Article
* @ORM\ManyToOne(targetEntity="App\Entity\Article")
* @ORM\JoinColumn(name="article_code", referencedColumnName="code", nullable=false)
* @Serializer\MaxDepth(depth=2)
*/
private $article;

/**
* Quantité à réapprovisionner
* @var int|null
*
* @ORM\Column(name="quantity", type="integer", nullable=true)
*/
private $quantity;

/**
* Date de création
* @var \DateTime|null
*
* @ORM\Column(name="creation_date", type="datetime", nullable=false)
*/
private $creationDate;

/**
* Date de réapprovisionnement
* @var \DateTime|null
*
* @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=false)
*/
private $outOfStock;

/**
* @return int
*/
public function getId(): int
{
return $this->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;
}

}
36 changes: 36 additions & 0 deletions src/Migrations/Version20190815150022.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190815150022 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}

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 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');
}
}
19 changes: 19 additions & 0 deletions src/Repository/SupplyingRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Repository;

use Doctrine\ORM\EntityRepository;

class SupplyingRepository extends EntityRepository
{

public function getOngoing()
{
return $this->createQueryBuilder('s')
->where('s.supplyDate is null')
->orderBy('s.id', 'ASC')
->getQuery()
->getResult()
;
}
}
Loading

0 comments on commit 75acc4e

Please sign in to comment.