Commit d206c098 authored by Mat's avatar Mat

fonctionnel avec GET, PUT, et POST

parent 1a6575f5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<ul id="brasserie"></ul>
<form id="form">
Nom <input type="text" name="nom"><br>
Adresse <input type="text" name="adresse"><br>
Pays <input type="text" name="pays"><br>
<button id="send">Envoyer</button>
</form>
<script>
$(document).ready(function()
{
let targetedId;
$.ajax({
url: "http://0.0.0.0:81/web/app_dev.php/API/v2/brasserie",
dataType: 'JSON',
method: 'GET',
success: function(data) {
for (let elem of data)
{
$('#brasserie').append(
`<li data-target="${elem.id}">${elem.nom}</li>`
);
}
}
});
$('#brasserie').on('click', 'li', function()
{
targetedId = $(this).attr('data-target');
$.ajax({
url: "http://0.0.0.0:81/web/app_dev.php/API/v2/brasserie/" + targetedId,
method: 'GET',
success: function(data) {
$('input[name=nom]').val(data.nom);
$('input[name=adresse]').val(data.adresse);
$('input[name=pays]').val(data.pays.id);
},
error: function(xhr) {
console.log(xhr);
}
});
});
$('#send').click(function(e)
{
e.preventDefault();
if (typeof targetedId === 'undefined') {
$.ajax({
url: "http://0.0.0.0:81/web/app_dev.php/API/v2/brasserie",
dataType: 'JSON',
method: 'POST',
data: $('#form').serialize(),
success: function(data) {
alert('success');
},
error: function(xhr) {
console.log(xhr);
alert('error');
}
});
} else {
$.ajax({
url: "http://0.0.0.0:81/web/app_dev.php/API/v2/brasserie/" + targetedId,
dataType: 'JSON',
method: 'PUT',
data: $('#form').serialize(),
success: function(data) {
alert('success');
},
error: function(xhr) {
console.log(xhr);
alert('error');
}
});
}
});
});
</script>
</html>
</body>
......@@ -25,21 +25,63 @@ class BrasserieController extends FOSRestController
/**
* @param Request $request
* @param $id
* @param Brasserie $id
* @Rest\Put(path="/brasserie/{id}")
* @Rest\View()
* @return Brasserie
*/
public function putAction(Request $request, Brasserie $id)
{
// le fait de typer $id fait qu'il retourne un objet !
$form = $this->createForm(BrasserieType::class, $id);
$form = $this->createForm(BrasserieType::class, $id,
array(
'method' => 'put'
)
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
dump($form); die;
$em = $this->getDoctrine()->getManager();
$em->flush();
return $id;
}
dump('form invalide'); die;
return $form;
}
/**
* @param Brasserie $id
* @Rest\Get(path="/brasserie/{id}")
* @Rest\View()
* @return Brasserie
*/
public function getByIdAction(Brasserie $id)
{
return $id;
}
/**
* @param Request $request
* @Rest\Post(path="/brasserie")
* @Rest\View()
* @return Brasserie
*/
public function newAction(Request $request)
{
$brasserie = new Brasserie();
$form = $this->createForm(BrasserieType::class, $brasserie);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($brasserie);
$em->flush();
return $brasserie;
}
return $form;
}
}
......@@ -25,7 +25,8 @@ class BrasserieType extends AbstractType
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Api2Bundle\Entity\Brasserie'
'data_class' => 'Api2Bundle\Entity\Brasserie',
'csrf_protection' => false
));
}
......
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