@ -0,0 +1,42 @@ | |||
<?php | |||
namespace App\Controller; | |||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |||
use Symfony\Component\Routing\Annotation\Route; | |||
use Symfony\Component\HttpFoundation\Response; | |||
use Symfony\Component\HttpFoundation\Request; | |||
use App\Validator\ApiValidator; | |||
use App\Entity\Hygrometry; | |||
class HygrometryApiController extends AbstractController | |||
{ | |||
/** | |||
* @Route("/api/hygrometry/create", name="api_hygrometry_create", methods={"POST"}) | |||
*/ | |||
public function create(Request $request, ApiValidator $validator): Response | |||
{ | |||
if ('application/json' === $request->getContentType()) { | |||
return $this->json([], 400); | |||
} | |||
$content = $request->getContent(); | |||
if (!$validator->isValidContent($content)) { | |||
return $this->json([], 400); | |||
} | |||
$data = json_decode($content, true); | |||
$entity = new Hygrometry(); | |||
$entity | |||
->setDate(new \DateTime($data['date'])) | |||
->setValue((float) $data['value']); | |||
$manager = $this->getDoctrine()->getManager(); | |||
$manager->persist($entity); | |||
$manager->flush(); | |||
return $this->json([], 201); | |||
} | |||
} |
@ -0,0 +1,42 @@ | |||
<?php | |||
namespace App\Controller; | |||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |||
use Symfony\Component\Routing\Annotation\Route; | |||
use Symfony\Component\HttpFoundation\Response; | |||
use Symfony\Component\HttpFoundation\Request; | |||
use App\Validator\ApiValidator; | |||
use App\Entity\Temperature; | |||
class TemperatureApiController extends AbstractController | |||
{ | |||
/** | |||
* @Route("/api/temperature/create", name="api_temperature_create", methods={"POST"}) | |||
*/ | |||
public function create(Request $request, ApiValidator $validator): Response | |||
{ | |||
if ('application/json' === $request->getContentType()) { | |||
return $this->json([], 400); | |||
} | |||
$content = $request->getContent(); | |||
if (!$validator->isValidContent($content)) { | |||
return $this->json([], 400); | |||
} | |||
$data = json_decode($content, true); | |||
$entity = new Temperature(); | |||
$entity | |||
->setDate(new \DateTime($data['date'])) | |||
->setValue((float) $data['value']); | |||
$manager = $this->getDoctrine()->getManager(); | |||
$manager->persist($entity); | |||
$manager->flush(); | |||
return $this->json([], 201); | |||
} | |||
} |
@ -0,0 +1,57 @@ | |||
<?php | |||
namespace App\Entity; | |||
use Doctrine\ORM\Mapping as ORM; | |||
/** | |||
* @ORM\Entity(repositoryClass="App\Repository\HygrometryRepository") | |||
*/ | |||
class Hygrometry | |||
{ | |||
/** | |||
* @ORM\Id() | |||
* @ORM\GeneratedValue() | |||
* @ORM\Column(type="integer") | |||
*/ | |||
private $id; | |||
/** | |||
* @ORM\Column(type="datetime") | |||
*/ | |||
private $date; | |||
/** | |||
* @ORM\Column(type="float") | |||
*/ | |||
private $value; | |||
public function getId(): ?int | |||
{ | |||
return $this->id; | |||
} | |||
public function getDate(): ?\DateTimeInterface | |||
{ | |||
return $this->date; | |||
} | |||
public function setDate(\DateTimeInterface $date): self | |||
{ | |||
$this->date = $date; | |||
return $this; | |||
} | |||
public function getValue(): ?float | |||
{ | |||
return $this->value; | |||
} | |||
public function setValue(float $value): self | |||
{ | |||
$this->value = $value; | |||
return $this; | |||
} | |||
} |
@ -0,0 +1,57 @@ | |||
<?php | |||
namespace App\Entity; | |||
use Doctrine\ORM\Mapping as ORM; | |||
/** | |||
* @ORM\Entity(repositoryClass="App\Repository\TemperatureRepository") | |||
*/ | |||
class Temperature | |||
{ | |||
/** | |||
* @ORM\Id() | |||
* @ORM\GeneratedValue() | |||
* @ORM\Column(type="integer") | |||
*/ | |||
private $id; | |||
/** | |||
* @ORM\Column(type="datetime") | |||
*/ | |||
private $date; | |||
/** | |||
* @ORM\Column(type="float") | |||
*/ | |||
private $value; | |||
public function getId(): ?int | |||
{ | |||
return $this->id; | |||
} | |||
public function getDate(): ?\DateTimeInterface | |||
{ | |||
return $this->date; | |||
} | |||
public function setDate(\DateTimeInterface $date): self | |||
{ | |||
$this->date = $date; | |||
return $this; | |||
} | |||
public function getValue(): ?float | |||
{ | |||
return $this->value; | |||
} | |||
public function setValue(float $value): self | |||
{ | |||
$this->value = $value; | |||
return $this; | |||
} | |||
} |
@ -0,0 +1,37 @@ | |||
<?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 Version20200316130907 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 hygrometry (id INT AUTO_INCREMENT NOT NULL, date DATETIME NOT NULL, value DOUBLE PRECISION NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'); | |||
$this->addSql('CREATE TABLE temperature (id INT AUTO_INCREMENT NOT NULL, date DATETIME NOT NULL, value DOUBLE PRECISION NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'); | |||
} | |||
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 hygrometry'); | |||
$this->addSql('DROP TABLE temperature'); | |||
} | |||
} |
@ -0,0 +1,50 @@ | |||
<?php | |||
namespace App\Repository; | |||
use App\Entity\Hygrometry; | |||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | |||
use Doctrine\Common\Persistence\ManagerRegistry; | |||
/** | |||
* @method Hygrometry|null find($id, $lockMode = null, $lockVersion = null) | |||
* @method Hygrometry|null findOneBy(array $criteria, array $orderBy = null) | |||
* @method Hygrometry[] findAll() | |||
* @method Hygrometry[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | |||
*/ | |||
class HygrometryRepository extends ServiceEntityRepository | |||
{ | |||
public function __construct(ManagerRegistry $registry) | |||
{ | |||
parent::__construct($registry, Hygrometry::class); | |||
} | |||
// /** | |||
// * @return Hygrometry[] Returns an array of Hygrometry objects | |||
// */ | |||
/* | |||
public function findByExampleField($value) | |||
{ | |||
return $this->createQueryBuilder('h') | |||
->andWhere('h.exampleField = :val') | |||
->setParameter('val', $value) | |||
->orderBy('h.id', 'ASC') | |||
->setMaxResults(10) | |||
->getQuery() | |||
->getResult() | |||
; | |||
} | |||
*/ | |||
/* | |||
public function findOneBySomeField($value): ?Hygrometry | |||
{ | |||
return $this->createQueryBuilder('h') | |||
->andWhere('h.exampleField = :val') | |||
->setParameter('val', $value) | |||
->getQuery() | |||
->getOneOrNullResult() | |||
; | |||
} | |||
*/ | |||
} |
@ -0,0 +1,50 @@ | |||
<?php | |||
namespace App\Repository; | |||
use App\Entity\Temperature; | |||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | |||
use Doctrine\Common\Persistence\ManagerRegistry; | |||
/** | |||
* @method Temperature|null find($id, $lockMode = null, $lockVersion = null) | |||
* @method Temperature|null findOneBy(array $criteria, array $orderBy = null) | |||
* @method Temperature[] findAll() | |||
* @method Temperature[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | |||
*/ | |||
class TemperatureRepository extends ServiceEntityRepository | |||
{ | |||
public function __construct(ManagerRegistry $registry) | |||
{ | |||
parent::__construct($registry, Temperature::class); | |||
} | |||
// /** | |||
// * @return Temperature[] Returns an array of Temperature objects | |||
// */ | |||
/* | |||
public function findByExampleField($value) | |||
{ | |||
return $this->createQueryBuilder('t') | |||
->andWhere('t.exampleField = :val') | |||
->setParameter('val', $value) | |||
->orderBy('t.id', 'ASC') | |||
->setMaxResults(10) | |||
->getQuery() | |||
->getResult() | |||
; | |||
} | |||
*/ | |||
/* | |||
public function findOneBySomeField($value): ?Temperature | |||
{ | |||
return $this->createQueryBuilder('t') | |||
->andWhere('t.exampleField = :val') | |||
->setParameter('val', $value) | |||
->getQuery() | |||
->getOneOrNullResult() | |||
; | |||
} | |||
*/ | |||
} |
@ -0,0 +1,47 @@ | |||
<?php | |||
namespace App\Validator; | |||
/** | |||
* class ApiValidator. | |||
* | |||
* @author Simon Vieille <simon@deblan.fr> | |||
*/ | |||
class ApiValidator | |||
{ | |||
/** | |||
* isValidContent | |||
* | |||
* @param string|null $content | |||
* | |||
* @return bool | |||
*/ | |||
public function isValidContent(? string $content): bool | |||
{ | |||
$json = json_decode($content, true); | |||
if (!is_array($json)) { | |||
return false; | |||
} | |||
if (count($json) !== 2) { | |||
return false; | |||
} | |||
if (!isset($json['date'], $json['value'])) { | |||
return false; | |||
} | |||
try { | |||
new \DateTime($json['date']); | |||
} catch (\Exception $e) { | |||
return false; | |||
} | |||
if (filter_var($json['value'], FILTER_VALIDATE_FLOAT) === null) { | |||
return false; | |||
} | |||
return true; | |||
} | |||
} |