From 82ce603cc410cb48acddb611cbc8faaa36024cb6 Mon Sep 17 00:00:00 2001 From: julkwel Date: Mon, 15 Jan 2024 19:04:32 +0300 Subject: [PATCH] WIP in resource : add Code postale --- config/packages/api_platform.yaml | 2 +- src/Command/ImportPayloadCommand.php | 6 ++ src/Entity/CodePostale.php | 105 +++++++++++++++++++++++ src/Entity/Province.php | 44 +++++++++- src/Entity/Region.php | 4 +- src/Manager/ImportDataManager.php | 28 ++++++ src/Repository/CodePostaleRepository.php | 48 +++++++++++ 7 files changed, 232 insertions(+), 5 deletions(-) create mode 100644 src/Entity/CodePostale.php create mode 100644 src/Repository/CodePostaleRepository.php diff --git a/config/packages/api_platform.yaml b/config/packages/api_platform.yaml index 402e56b..bb5da86 100644 --- a/config/packages/api_platform.yaml +++ b/config/packages/api_platform.yaml @@ -1,6 +1,6 @@ api_platform: title: Madagascar Localization - version: 1.0.0 + version: 1.1.0 formats: jsonld: ['application/ld+json'] docs_formats: diff --git a/src/Command/ImportPayloadCommand.php b/src/Command/ImportPayloadCommand.php index 5772d68..8779048 100644 --- a/src/Command/ImportPayloadCommand.php +++ b/src/Command/ImportPayloadCommand.php @@ -23,6 +23,7 @@ public function __construct(private ImportDataManager $dataManager) { parent::__construct(); $this->addOption('province', null, InputOption::VALUE_NONE); + $this->addOption('code-postale', null, InputOption::VALUE_NONE); } protected function configure(): void @@ -32,6 +33,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $ioStyle = new SymfonyStyle($input, $output); + if ($input->getOption('code-postale')) { + $this->dataManager->importCodePostale($ioStyle); + exit(Command::SUCCESS); + } + if ($input->getOption('province')) { $this->dataManager->importProvince($ioStyle); exit(Command::SUCCESS); diff --git a/src/Entity/CodePostale.php b/src/Entity/CodePostale.php new file mode 100644 index 0000000..7edb24b --- /dev/null +++ b/src/Entity/CodePostale.php @@ -0,0 +1,105 @@ + ['code:read']], +)] +#[ApiFilter(SearchFilter::class, properties: ['codePostal' => 'ipartial', 'ville' => 'ipartial'])] +#[ApiFilter(OrderFilter::class, properties: ['codePostal'], arguments: ['orderParameterName' => 'order'])] +class CodePostale +{ + #[ORM\Id] + #[ORM\Column(type: UuidType::NAME, unique: true)] + #[ORM\GeneratedValue(strategy: 'CUSTOM')] + #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')] + #[Groups(['code:read'])] + private ?Uuid $id = null; + + #[ORM\Column(length: 5)] + #[Groups(['code:read'])] + private ?string $codePostal = null; + + #[ORM\Column(length: 255)] + #[Groups(['code:read'])] + private ?string $ville = null; + + #[ORM\ManyToOne] + #[Groups(['code:read'])] + private ?Region $region = null; + + #[ORM\ManyToOne(inversedBy: 'codePostales')] + #[ORM\JoinColumn(nullable: false)] + #[Groups(['code:read'])] + private ?Province $province = null; + + public function getId(): ?string + { + return $this->id; + } + + public function getCodePostal(): ?string + { + return $this->codePostal; + } + + public function setCodePostal(string $codePostal): static + { + $this->codePostal = $codePostal; + + return $this; + } + + public function getVille(): ?string + { + return $this->ville; + } + + public function setVille(string $ville): static + { + $this->ville = $ville; + + return $this; + } + + public function getRegion(): ?Region + { + return $this->region; + } + + public function setRegion(?Region $region): static + { + $this->region = $region; + + return $this; + } + + public function getProvince(): ?Province + { + return $this->province; + } + + public function setProvince(?Province $province): static + { + $this->province = $province; + + return $this; + } +} diff --git a/src/Entity/Province.php b/src/Entity/Province.php index a07a295..b92a56c 100644 --- a/src/Entity/Province.php +++ b/src/Entity/Province.php @@ -9,6 +9,8 @@ use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\GetCollection; use App\Repository\ProvinceRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Types\UuidType; use Symfony\Component\Serializer\Attribute\Groups; @@ -20,7 +22,7 @@ new Get(), new GetCollection() ], - normalizationContext: ['groups' => ['province:read']], + normalizationContext: ['groups' => ['province:read', 'code:read']], )] #[ApiFilter(SearchFilter::class, properties: ['name' => 'ipartial'])] #[ApiFilter(OrderFilter::class, properties: ['name'], arguments: ['orderParameterName' => 'order'])] @@ -34,9 +36,17 @@ class Province private ?Uuid $id = null; #[ORM\Column(length: 255)] - #[Groups(['province:read'])] + #[Groups(['province:read', 'code:read'])] private ?string $name = null; + #[ORM\OneToMany(mappedBy: 'province', targetEntity: CodePostale::class)] + private Collection $codePostales; + + public function __construct() + { + $this->codePostales = new ArrayCollection(); + } + public function getId(): ?string { return $this->id; @@ -53,4 +63,34 @@ public function setName(string $name): static return $this; } + + /** + * @return Collection + */ + public function getCodePostales(): Collection + { + return $this->codePostales; + } + + public function addCodePostale(CodePostale $codePostale): static + { + if (!$this->codePostales->contains($codePostale)) { + $this->codePostales->add($codePostale); + $codePostale->setProvince($this); + } + + return $this; + } + + public function removeCodePostale(CodePostale $codePostale): static + { + if ($this->codePostales->removeElement($codePostale)) { + // set the owning side to null (unless already changed) + if ($codePostale->getProvince() === $this) { + $codePostale->setProvince(null); + } + } + + return $this; + } } diff --git a/src/Entity/Region.php b/src/Entity/Region.php index 2b9f8a8..0ddf7e3 100644 --- a/src/Entity/Region.php +++ b/src/Entity/Region.php @@ -22,7 +22,7 @@ new GetCollection(), new Get() ], - normalizationContext: ['groups' => ['region:read', 'commune:read', 'district:read', 'fokontany:read']] + normalizationContext: ['groups' => ['region:read', 'commune:read', 'district:read', 'fokontany:read', 'code:read']] )] #[ApiFilter(SearchFilter::class, properties: ['name' => 'ipartial'])] #[ApiFilter(OrderFilter::class, properties: ['name'], arguments: ['orderParameterName' => 'order'])] @@ -36,7 +36,7 @@ class Region private ?Uuid $id = null; #[ORM\Column(length: 255)] - #[Groups(['region:read', 'commune:read', 'district:read', 'fokontany:read'])] + #[Groups(['region:read', 'commune:read', 'district:read', 'fokontany:read', 'code:read'])] private ?string $name = null; #[ORM\OneToMany(mappedBy: 'region', targetEntity: District::class)] diff --git a/src/Manager/ImportDataManager.php b/src/Manager/ImportDataManager.php index 7d3f447..ecf2d09 100644 --- a/src/Manager/ImportDataManager.php +++ b/src/Manager/ImportDataManager.php @@ -7,6 +7,7 @@ namespace App\Manager; +use App\Entity\CodePostale; use App\Entity\Commune; use App\Entity\District; use App\Entity\Fokontany; @@ -107,4 +108,31 @@ public function importProvince(SymfonyStyle $ioStyle): void $this->entityManager->flush(); } } + + public function importCodePostale(SymfonyStyle $ioStyle) + { + $payloadFile = $this->parameterBag->get('kernel.project_dir').'/in/json_province.json'; + $data = json_decode(file_get_contents($payloadFile), true); + + $progressBar = $ioStyle->createProgressBar(count($data)); + foreach ($data as $item) { + $progressBar->advance(); + $province = $this->entityManager->getRepository(Province::class)->findOneBy(['name' => $item['province']]); + $codePostale = $this->entityManager->getRepository(CodePostale::class)->findOneBy(['codePostal' => $item['zip'], 'province' => $province]) ?? new CodePostale(); + + $codePostale->setCodePostal($item['zip']); + $codePostale->setProvince($province); + $codePostale->setVille($item['ville']); + if ('Antananarivo Nord' === $item['ville']) { + $codePostale->setVille('Antananarivo Avaradrano'); + } + + if ('Antananarivo Sud' === $item['ville']) { + $codePostale->setVille('Antananarivo Atsimondrano'); + } + + $this->entityManager->persist($codePostale); + $this->entityManager->flush(); + } + } } \ No newline at end of file diff --git a/src/Repository/CodePostaleRepository.php b/src/Repository/CodePostaleRepository.php new file mode 100644 index 0000000..da5f7c5 --- /dev/null +++ b/src/Repository/CodePostaleRepository.php @@ -0,0 +1,48 @@ + + * + * @method CodePostale|null find($id, $lockMode = null, $lockVersion = null) + * @method CodePostale|null findOneBy(array $criteria, array $orderBy = null) + * @method CodePostale[] findAll() + * @method CodePostale[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class CodePostaleRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, CodePostale::class); + } + +// /** +// * @return CodePostale[] Returns an array of CodePostale objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('c') +// ->andWhere('c.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('c.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?CodePostale +// { +// return $this->createQueryBuilder('c') +// ->andWhere('c.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +}