add Temperature/Hygrometry API
This commit is contained in:
parent
2fa34dd760
commit
38b433a63c
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -19,5 +19,8 @@
|
|||
/.web-server-pid
|
||||
###< symfony/web-server-bundle ###
|
||||
|
||||
/Makefile
|
||||
/public/.htaccess
|
||||
/.mage.yml
|
||||
/node_modules
|
||||
/public/motion/snapshots/*
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
"ext-ctype": "*",
|
||||
"ext-iconv": "*",
|
||||
"sensio/framework-extra-bundle": "^5.1",
|
||||
"symfony/apache-pack": "^1.0",
|
||||
"symfony/asset": "4.4.*",
|
||||
"symfony/console": "4.4.*",
|
||||
"symfony/dotenv": "4.4.*",
|
||||
|
|
42
src/Controller/HygrometryApiController.php
Normal file
42
src/Controller/HygrometryApiController.php
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ use Symfony\Component\HttpFoundation\Request;
|
|||
class MonitoringApiController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Route("/api/monitoring/temperature", name="api_monitoring_temperature")
|
||||
* @Route("/api/monitoring/temperature", name="api_monitoring_temperature", methods={"GET"})
|
||||
*/
|
||||
public function temperature(Request $request)
|
||||
{
|
||||
|
@ -52,7 +52,7 @@ class MonitoringApiController extends AbstractController
|
|||
}
|
||||
|
||||
/**
|
||||
* @Route("/api/monitoring/hygrometry", name="api_monitoring_hygrometry")
|
||||
* @Route("/api/monitoring/hygrometry", name="api_monitoring_hygrometry", methods={"GET"})
|
||||
*/
|
||||
public function hygrometry(Request $request)
|
||||
{
|
||||
|
|
42
src/Controller/TemperatureApiController.php
Normal file
42
src/Controller/TemperatureApiController.php
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
57
src/Entity/Hygrometry.php
Normal file
57
src/Entity/Hygrometry.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
57
src/Entity/Temperature.php
Normal file
57
src/Entity/Temperature.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
37
src/Migrations/Version20200316130907.php
Normal file
37
src/Migrations/Version20200316130907.php
Normal file
|
@ -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');
|
||||
}
|
||||
}
|
50
src/Repository/HygrometryRepository.php
Normal file
50
src/Repository/HygrometryRepository.php
Normal file
|
@ -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()
|
||||
;
|
||||
}
|
||||
*/
|
||||
}
|
50
src/Repository/TemperatureRepository.php
Normal file
50
src/Repository/TemperatureRepository.php
Normal file
|
@ -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()
|
||||
;
|
||||
}
|
||||
*/
|
||||
}
|
47
src/Validator/ApiValidator.php
Normal file
47
src/Validator/ApiValidator.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
12
symfony.lock
12
symfony.lock
|
@ -141,6 +141,18 @@
|
|||
"config/packages/sensio_framework_extra.yaml"
|
||||
]
|
||||
},
|
||||
"symfony/apache-pack": {
|
||||
"version": "1.0",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes-contrib",
|
||||
"branch": "master",
|
||||
"version": "1.0",
|
||||
"ref": "410b9325a37ef86f1e47262c61738f6202202bca"
|
||||
},
|
||||
"files": [
|
||||
"public/.htaccess"
|
||||
]
|
||||
},
|
||||
"symfony/asset": {
|
||||
"version": "v4.4.5"
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue