Add municipality (province / municipality / city) data and CAPI call #540

This commit is contained in:
Kendrick Chan 2021-03-31 02:20:37 +08:00
parent 5d98216d21
commit 69fbfe196f
4 changed files with 143 additions and 0 deletions

View file

@ -57,3 +57,8 @@ access_keys:
acls:
- id: customer.register
label: Register Customer
- id: municipality
label: Municipality
acls:
- id: municipality.list
label: List

View file

@ -166,3 +166,9 @@ capi_cwarr_register:
controller: App\Controller\CAPI\CustomerWarrantyController::register
methods: [POST]
# municipality
capi_municipality_list:
path: /capi/municipality
controller: App\Controller\CAPI\MunicipalityController::getAll
methods: [GET]

View file

@ -0,0 +1,63 @@
<?php
namespace App\Controller\CAPI;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\Query;
use Doctrine\ORM\EntityManagerInterface;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\Municipality;
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
class MunicipalityController extends APIController
{
protected $acl_gen;
public function __construct(ACLGenerator $acl_gen)
{
$this->acl_gen = $acl_gen;
}
public function getAll(EntityManagerInterface $em)
{
$this->denyAccessUnlessGranted('municipality.list', null, 'No access.');
$muns = $em->getRepository(Municipality::class)->findBy([], ['province_name' => 'ASC', 'name' => 'ASC']);
$result = [];
$provinces = [];
foreach ($muns as $m)
{
$pid = $m->getProvinceID();
$pname = $m->getProvinceName();
$mid = $m->getID();
$mname = $m->getName();
if (!isset($provinces[$pid]))
{
$provinces[$pid] = [
'id' => $pid,
'name' => $pname,
'municipalities' => [],
];
}
// error_log("$pid - $pname - $mid - $mname");
$provinces[$pid]['municipalities'][$mid] = [
'id' => $mid,
'name' => $mname,
];
}
$data = [
'provinces' => $provinces,
];
return new APIResponse(true, 'Municipalities loaded.', $data);
}
}

View file

@ -0,0 +1,69 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="municipality")
*/
class Municipality
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="string", length=11)
*/
protected $code;
// name
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
// province id
/**
* @ORM\Column(type="string", length=11)
*/
protected $province_id;
// province name
/**
* @ORM\Column(type="string", length=100)
*/
protected $province_name;
public function __construct()
{
}
public function getID()
{
return $this->code;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function getProvinceID()
{
return $this->province_id;
}
public function getProvinceName()
{
return $this->province_name;
}
}