From c592cb4bc206aec579c74fe03c9856f61fe5d58c Mon Sep 17 00:00:00 2001 From: firdaous elhalafi Date: Thu, 21 Dec 2023 22:26:21 +0100 Subject: [PATCH 1/3] =?UTF-8?q?R=C3=A9cuperation=20du=20prix=20de=20vente?= =?UTF-8?q?=20moyen=20du=20m=C3=A8tre=20carr=C3=A9=20en=20evolution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + api/.gitignore | 3 + api/composer.lock | 14 ++-- api/src/Controller/SaleController.php | 22 ++++++ api/src/Service/SaleService.php | 105 ++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 api/src/Controller/SaleController.php create mode 100644 api/src/Service/SaleService.php diff --git a/.gitignore b/.gitignore index 2a5536b..b2f649c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /.env /helm/api-platform/charts/* !/helm/api-platform/charts/.gitignore + +.idea/* diff --git a/api/.gitignore b/api/.gitignore index d270e41..c930ddc 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -19,3 +19,6 @@ .phpunit.result.cache /phpunit.xml ###< symfony/phpunit-bridge ### + +### Migration ### +/migrations/* diff --git a/api/composer.lock b/api/composer.lock index 4fc11b5..8845eb2 100644 --- a/api/composer.lock +++ b/api/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "api-platform/core", - "version": "v3.2.7", + "version": "v3.2.9", "source": { "type": "git", "url": "https://github.com/api-platform/core.git", - "reference": "f297d2192652a3acd2a644707740de8cb5069221" + "reference": "fe07a7f8196bfbefd2f8d02d0f7cacb6b1fbf527" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/api-platform/core/zipball/f297d2192652a3acd2a644707740de8cb5069221", - "reference": "f297d2192652a3acd2a644707740de8cb5069221", + "url": "https://api.github.com/repos/api-platform/core/zipball/fe07a7f8196bfbefd2f8d02d0f7cacb6b1fbf527", + "reference": "fe07a7f8196bfbefd2f8d02d0f7cacb6b1fbf527", "shasum": "" }, "require": { @@ -69,9 +69,11 @@ "phpstan/phpstan-doctrine": "^1.0", "phpstan/phpstan-phpunit": "^1.0", "phpstan/phpstan-symfony": "^1.0", + "phpunit/phpunit": "^9.5", "psr/log": "^1.0 || ^2.0 || ^3.0", "ramsey/uuid": "^3.9.7 || ^4.0", "ramsey/uuid-doctrine": "^1.4 || ^2.0", + "sebastian/comparator": "<5.0", "soyuka/contexts": "v3.3.9", "soyuka/stubs-mongodb": "^1.0", "symfony/asset": "^6.1 || ^7.0", @@ -165,9 +167,9 @@ ], "support": { "issues": "https://github.com/api-platform/core/issues", - "source": "https://github.com/api-platform/core/tree/v3.2.7" + "source": "https://github.com/api-platform/core/tree/v3.2.9" }, - "time": "2023-11-30T13:51:25+00:00" + "time": "2023-12-20T17:01:35+00:00" }, { "name": "doctrine/cache", diff --git a/api/src/Controller/SaleController.php b/api/src/Controller/SaleController.php new file mode 100644 index 0000000..e311ba9 --- /dev/null +++ b/api/src/Controller/SaleController.php @@ -0,0 +1,22 @@ +saleService->getMonthlyAveragePriceEvolution(); + + return $this->json($result); + } +} diff --git a/api/src/Service/SaleService.php b/api/src/Service/SaleService.php new file mode 100644 index 0000000..f7c50c6 --- /dev/null +++ b/api/src/Service/SaleService.php @@ -0,0 +1,105 @@ +date = $date; + $this->amount = $amount; + $this->surface = $surface; + $this->region = $region; + } + + // getters + public function getId(): ?int + { + return $this->id; + } + + public function getDate(): ?\DateTimeInterface + { + return $this->date; + } + + public function getAmount(): ?float + { + return $this->amount; + } + + public function getSurface(): ?float + { + return $this->surface; + } + + public function getRegion(): ?string + { + return $this->region; + } + + // setters + public function setDate(\DateTimeImmutable $date): void + { + $this->date = $date; + } + + public function setAmount(float $amount): void + { + $this->amount = $amount; + } + + public function setSurface(float $surface): void + { + $this->surface = $surface; + } + + public function setRegion(string $region): void + { + $this->region = $region; + } +} -- GitLab From 49ba4f4a7bb2e6326b4dd34ee223fcc33e832dae Mon Sep 17 00:00:00 2001 From: firdaous elhalafi Date: Fri, 22 Dec 2023 23:25:57 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Tentative=20de=20r=C3=A9cup=C3=A9ration=20d?= =?UTF-8?q?e=20l'annee=20et=20le=20mois=20pour=20les=20donnees=20qu'on=20a?= =?UTF-8?q?=20dans=20la=20bd=20pour=20la=20requ=C3=AAte=20d'=C3=A9volution?= =?UTF-8?q?=20du=20prix=20de=20vente=20moyen=20du=20m=C3=A8tre=20carr?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/composer.json | 1 + api/composer.lock | 59 ++++++++++++++++++++++++++- api/config/packages/doctrine.yaml | 4 ++ api/src/Controller/SaleController.php | 10 ++++- api/src/Service/SaleService.php | 11 ++++- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/api/composer.json b/api/composer.json index 8eed869..0169589 100644 --- a/api/composer.json +++ b/api/composer.json @@ -6,6 +6,7 @@ "ext-ctype": "*", "ext-iconv": "*", "api-platform/core": "^3.2", + "beberlei/doctrineextensions": "^1.3", "doctrine/doctrine-bundle": "^2.7", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.12", diff --git a/api/composer.lock b/api/composer.lock index 8845eb2..ec310a1 100644 --- a/api/composer.lock +++ b/api/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "934976d853cfcd0af63e3e1658634266", + "content-hash": "4a4de0353136f892d2f58fbbeba4ab1d", "packages": [ { "name": "api-platform/core", @@ -171,6 +171,63 @@ }, "time": "2023-12-20T17:01:35+00:00" }, + { + "name": "beberlei/doctrineextensions", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/beberlei/DoctrineExtensions.git", + "reference": "008f162f191584a6c37c03a803f718802ba9dd9a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/beberlei/DoctrineExtensions/zipball/008f162f191584a6c37c03a803f718802ba9dd9a", + "reference": "008f162f191584a6c37c03a803f718802ba9dd9a", + "shasum": "" + }, + "require": { + "doctrine/orm": "^2.7", + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.14", + "nesbot/carbon": "*", + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0", + "symfony/yaml": "^4.2 || ^5.0", + "zf1/zend-date": "^1.12", + "zf1/zend-registry": "^1.12" + }, + "type": "library", + "autoload": { + "psr-4": { + "DoctrineExtensions\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Steve Lacey", + "email": "steve@steve.ly" + } + ], + "description": "A set of extensions to Doctrine 2 that add support for additional query functions available in MySQL, Oracle, PostgreSQL and SQLite.", + "keywords": [ + "database", + "doctrine", + "orm" + ], + "support": { + "source": "https://github.com/beberlei/DoctrineExtensions/tree/v1.3.0" + }, + "time": "2020-11-29T07:37:23+00:00" + }, { "name": "doctrine/cache", "version": "2.2.0", diff --git a/api/config/packages/doctrine.yaml b/api/config/packages/doctrine.yaml index 559641f..f9a452e 100644 --- a/api/config/packages/doctrine.yaml +++ b/api/config/packages/doctrine.yaml @@ -21,6 +21,10 @@ doctrine: dir: '%kernel.project_dir%/src/Entity' prefix: 'App\Entity' alias: App + dql: + string_functions: + MONTH: DoctrineExtensions\Query\Mysql\Month + YEAR: DoctrineExtensions\Query\Mysql\Year when@test: doctrine: diff --git a/api/src/Controller/SaleController.php b/api/src/Controller/SaleController.php index e311ba9..c56869c 100644 --- a/api/src/Controller/SaleController.php +++ b/api/src/Controller/SaleController.php @@ -10,13 +10,19 @@ use Symfony\Component\HttpKernel\Attribute\AsController; #[AsController] class SaleController extends AbstractController { - public function __construct(private SaleService $saleService) - {} + private SaleService $saleService; + + public function __construct(SaleService $saleService) + { + $this->saleService = $saleService; + } public function __invoke(): JsonResponse { $result = $this->saleService->getMonthlyAveragePriceEvolution(); + echo $result; + return $this->json($result); } } diff --git a/api/src/Service/SaleService.php b/api/src/Service/SaleService.php index 732fb6b..45784a4 100644 --- a/api/src/Service/SaleService.php +++ b/api/src/Service/SaleService.php @@ -12,20 +12,27 @@ class SaleService public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; + + $emConfig = $this->entityManager->getConfiguration(); + $emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year'); + $emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month'); + $emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day'); } - public function getMonthlyAveragePriceEvolutionApiPlatform(): array + public function getMonthlyAveragePriceEvolution(): array { $queryBuilder = $this->entityManager->createQueryBuilder(); $result = $queryBuilder - ->select('MONTH(s.date) as month, YEAR(s.date) as year, AVG(s.amount / s.surface) as average_price') + ->select("MONTH(s.date) as month, YEAR(s.date) as year, AVG(s.amount / s.surface) as average_price") ->from(Sale::class, 's') ->groupBy('year, month') ->orderBy('year, month') ->getQuery() ->getResult(); + echo "=========================="; + return $result; } } -- GitLab From c98c611378eee16b1207734ce83b2007bd943d4a Mon Sep 17 00:00:00 2001 From: firdaous elhalafi Date: Mon, 1 Jan 2024 17:50:36 +0100 Subject: [PATCH 3/3] recuperation du prix moyen de vente du metre carre fin --- api/config/packages/doctrine.yaml | 4 ++-- api/src/Controller/SaleController.php | 4 ++-- .../Dto/MonthlyAveragePriceEvolutionDto.php | 17 ++++++++++++++++ api/src/Entity/Sale.php | 20 +++++++++---------- api/src/Service/SaleService.php | 19 ++++++++++-------- insertions/script-2.mjs | 2 +- 6 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 api/src/Dto/MonthlyAveragePriceEvolutionDto.php diff --git a/api/config/packages/doctrine.yaml b/api/config/packages/doctrine.yaml index f9a452e..53270fd 100644 --- a/api/config/packages/doctrine.yaml +++ b/api/config/packages/doctrine.yaml @@ -23,8 +23,8 @@ doctrine: alias: App dql: string_functions: - MONTH: DoctrineExtensions\Query\Mysql\Month - YEAR: DoctrineExtensions\Query\Mysql\Year + MONTH: DoctrineExtensions\Query\Postgresql\Month + YEAR: DoctrineExtensions\Query\Postgresql\Year when@test: doctrine: diff --git a/api/src/Controller/SaleController.php b/api/src/Controller/SaleController.php index c56869c..cad37dc 100644 --- a/api/src/Controller/SaleController.php +++ b/api/src/Controller/SaleController.php @@ -2,10 +2,12 @@ namespace App\Controller; +use ApiPlatform\Metadata\Get; use App\Service\SaleService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\Routing\Annotation\Route; #[AsController] class SaleController extends AbstractController @@ -21,8 +23,6 @@ class SaleController extends AbstractController { $result = $this->saleService->getMonthlyAveragePriceEvolution(); - echo $result; - return $this->json($result); } } diff --git a/api/src/Dto/MonthlyAveragePriceEvolutionDto.php b/api/src/Dto/MonthlyAveragePriceEvolutionDto.php new file mode 100644 index 0000000..fc48979 --- /dev/null +++ b/api/src/Dto/MonthlyAveragePriceEvolutionDto.php @@ -0,0 +1,17 @@ +month = $month; + $this->year = $year; + $this->averagePrice = $averagePrice; + } +} diff --git a/api/src/Entity/Sale.php b/api/src/Entity/Sale.php index 364367e..712002d 100644 --- a/api/src/Entity/Sale.php +++ b/api/src/Entity/Sale.php @@ -3,20 +3,20 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; -use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; use App\Controller\SaleController; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; - -#[ApiResource(operations: [ - new Get( - uriTemplate: '/sale/monthly_average_price_evolution', - controller: SaleController::class, - name: 'monthly_average_price_evolution' - ) -])] - +#[ApiResource( + operations: [ + new GetCollection( + uriTemplate: '/sales/monthly_average_price_evolution', + controller: SaleController::class, + name: 'monthly_average_price_evolution' + ) + ] +)] /** A sale. */ #[ORM\Entity] diff --git a/api/src/Service/SaleService.php b/api/src/Service/SaleService.php index 45784a4..6f2a07b 100644 --- a/api/src/Service/SaleService.php +++ b/api/src/Service/SaleService.php @@ -4,6 +4,7 @@ namespace App\Service; use App\Entity\Sale; use Doctrine\ORM\EntityManagerInterface; +use App\Dto\MonthlyAveragePriceEvolutionDto; class SaleService { @@ -12,11 +13,6 @@ class SaleService public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; - - $emConfig = $this->entityManager->getConfiguration(); - $emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year'); - $emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month'); - $emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day'); } public function getMonthlyAveragePriceEvolution(): array @@ -24,15 +20,22 @@ class SaleService $queryBuilder = $this->entityManager->createQueryBuilder(); $result = $queryBuilder - ->select("MONTH(s.date) as month, YEAR(s.date) as year, AVG(s.amount / s.surface) as average_price") + ->select('MONTH(s.date) as month', 'YEAR(s.date) as year', 'AVG(CASE WHEN s.surface <> 0 THEN s.amount / s.surface ELSE 0 END) as average_price') ->from(Sale::class, 's') ->groupBy('year, month') ->orderBy('year, month') ->getQuery() ->getResult(); - echo "=========================="; + $monthlyAveragePriceEvolutions = []; + foreach ($result as $row) { + $monthlyAveragePriceEvolutions[] = new MonthlyAveragePriceEvolutionDto( + (int)$row['month'], + (int)$row['year'], + (float)$row['average_price'] + ); + } - return $result; + return $monthlyAveragePriceEvolutions; } } diff --git a/insertions/script-2.mjs b/insertions/script-2.mjs index abf296d..d3c2ba5 100644 --- a/insertions/script-2.mjs +++ b/insertions/script-2.mjs @@ -134,6 +134,6 @@ rl.on('line', (line) => { }); rl.on('close', () => { - const str = `INSERT INTO vente(date, value, region, surface) VALUES\n${lines.join(",\n")};`; + const str = `INSERT INTO sale(date, amount, region, surface) VALUES\n${lines.join(",\n")};`; console.log(str); }); -- GitLab