Skip to content

Commit

Permalink
WIP in resource : add Code postale
Browse files Browse the repository at this point in the history
  • Loading branch information
julkwel committed Jan 15, 2024
1 parent f8bb71d commit 82ce603
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
api_platform:
title: Madagascar Localization
version: 1.0.0
version: 1.1.0
formats:
jsonld: ['application/ld+json']
docs_formats:
Expand Down
6 changes: 6 additions & 0 deletions src/Command/ImportPayloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
105 changes: 105 additions & 0 deletions src/Entity/CodePostale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace App\Entity;

use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\Repository\CodePostaleRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: CodePostaleRepository::class)]
#[ApiResource(
operations: [
new Get(),
new GetCollection()
],
normalizationContext: ['groups' => ['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;
}
}
44 changes: 42 additions & 2 deletions src/Entity/Province.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'])]
Expand All @@ -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;
Expand All @@ -53,4 +63,34 @@ public function setName(string $name): static

return $this;
}

/**
* @return Collection<int, CodePostale>
*/
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;
}
}
4 changes: 2 additions & 2 deletions src/Entity/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])]
Expand All @@ -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)]
Expand Down
28 changes: 28 additions & 0 deletions src/Manager/ImportDataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace App\Manager;

use App\Entity\CodePostale;
use App\Entity\Commune;
use App\Entity\District;
use App\Entity\Fokontany;
Expand Down Expand Up @@ -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();
}
}
}
48 changes: 48 additions & 0 deletions src/Repository/CodePostaleRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Repository;

use App\Entity\CodePostale;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends ServiceEntityRepository<CodePostale>
*
* @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()
// ;
// }
}

0 comments on commit 82ce603

Please sign in to comment.