Commit c0f35cd6 authored by Mat's avatar Mat

un select qui affiche les brasseries par pays

parent f0759c74
......@@ -7,6 +7,7 @@
</head>
<body>
<select id="pays"></select>
<ul id="brasserie"></ul>
<button id="new">New</button>
<form id="form">
......@@ -20,11 +21,45 @@
$(document).ready(function()
{
let targetedId;
$.ajax({
url: "http://0.0.0.0:81/web/app_dev.php/API/v2/pays/used",
method: 'GET',
dataType: 'JSON',
success: function(data) {
for (let elem of data) {
$('#pays').append(
`<option value="${elem.id}">${elem.nom}</option>`
);
}
},
error: (error) => {
console.log(error);
}
});
$('#pays').change(function(e) {
$.ajax({
url: "http://0.0.0.0:81/web/app_dev.php/API/v2/brasserie/parPays/" + $(this).val(),
dataType: 'JSON',
method: 'GET',
success: data => {
$('#brasserie').empty();
for (let elem of data) {
$('#brasserie').append(
`<li data-target="${elem.id}">${elem.nom}</li>`
);
}
}
});
});
$('#new').click(function(e) {
e.preventDefault();
targetedId = undefined;
$('input').val('');
});
$.ajax({
url: "http://0.0.0.0:81/web/app_dev.php/API/v2/brasserie",
dataType: 'JSON',
......
......@@ -3,6 +3,7 @@
namespace Api2Bundle\Controller;
use Api2Bundle\Entity\Brasserie;
use Api2Bundle\Entity\Pays;
use Api2Bundle\Form\BrasserieType;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
......@@ -28,7 +29,7 @@ class BrasserieController extends FOSRestController
* @param Brasserie $id
* @Rest\Put(path="/brasserie/{id}")
* @Rest\View()
* @return Brasserie
* @return Brasserie|\Symfony\Component\Form\FormInterface
*/
public function putAction(Request $request, Brasserie $id)
{
......@@ -67,7 +68,7 @@ class BrasserieController extends FOSRestController
* @param Request $request
* @Rest\Post(path="/brasserie")
* @Rest\View()
* @return Brasserie
* @return Brasserie|\Symfony\Component\Form\FormInterface
*/
public function newAction(Request $request)
{
......@@ -84,4 +85,17 @@ class BrasserieController extends FOSRestController
}
return $form;
}
/**
* @Rest\Get(path="/brasserie/parPays/{paysID}")
* @Rest\View()
* @return Brasserie[]
*/
public function getByCountryAction(Pays $paysID)
{
return $this->getDoctrine()
->getRepository(Brasserie::class)
->findBy(array('pays' => $paysID));
}
}
<?php
namespace Api2Bundle\Controller;
use Api2Bundle\Entity\Brasserie;
use Api2Bundle\Entity\Pays;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
class PaysController extends FOSRestController
{
/**
* @Rest\Get(path="/pays/used")
* @Rest\View()
* @return Pays
*/
public function getUsedPaysAction()
{
$repo = $this->getDoctrine()
->getRepository(Brasserie::class);
$pays = array_map(function($brasserie) {
/**
* @var $brasserie Brasserie
*/
return $brasserie->getPays();
}, $repo->getPays());
return $pays;
}
}
<?php
namespace Api2Bundle\Repository;
use Api2Bundle\Entity\Pays;
/**
* BrasserieRepository
......@@ -10,4 +11,17 @@ namespace Api2Bundle\Repository;
*/
class BrasserieRepository extends \Doctrine\ORM\EntityRepository
{
/**
* @return Pays
*/
public function getPays()
{
$qb = $this->createQueryBuilder('b')
->leftJoin('b.pays', 'p')
->addSelect('p')
->groupBy('p')
;
return $qb->getQuery()->getResult();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment