Commit 7406447a authored by Mat's avatar Mat

implémente l'action ajax sur les boutons editer

* il semblerait que pour faire une requête ajax en GET (récupérer les
données à afficher dans le formulaire), on ait besoin de l'action
readOne. c'est logique, malgré que submitURL soit réglé vers la route
update !!
parent 28d3f31f
......@@ -46,6 +46,25 @@ class ImageAPIController extends MasterAPIController
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function readOneAction($id)
{
$em = $this->getDoctrine()->getManager();
$image = $em->getRepository('TechnoBundle:Image')
->findOneForAPIRead($id);
if ($image === null) {
throw $this->api('Not found', Response::HTTP_NOT_FOUND);
}
return $this->api($image);
}
/**
* @param Request
* @param $id
......@@ -58,7 +77,7 @@ class ImageAPIController extends MasterAPIController
->findOneForAPIUpdate($id);
if ($image === null) {
throw $this->createNotFoundException('Image non trouvée');
throw $this->api($image, Response::HTTP_NOT_FOUND);
}
$form = $this->createForm(ImageAPIType::class, $image);
......@@ -70,7 +89,7 @@ class ImageAPIController extends MasterAPIController
$em->flush();
return $this->api($image);
}
return $this->api($form);
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
......
......@@ -35,7 +35,7 @@ class OwnerAPIController extends MasterAPIController
->findOneForAPIRead($id);
if ($owner === null) {
throw $this->createNotFoundException();
throw $this->api('Not found', Response::HTTP_NOT_FOUND);
}
return $this->api($owner);
......
......@@ -54,6 +54,14 @@ api_image_read:
options:
expose: true # façon crade d'esposer la route
api_image_read_one:
path: /image/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:ImageAPI:readOne
methods: [ GET ]
api_image_update:
path: /image/{id}
requirements:
......
......@@ -98,6 +98,24 @@ class ImageRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getResult();
}
/**
* @param $id
* @return Image
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIRead($id)
{
$qb = $this->createQueryBuilder('i')
->join('i.owner', 'o')
->addSelect('o')
->where('i.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return Image
......
......@@ -6,7 +6,7 @@
{% block sidebar %}
<h3>IMAGE</h3>
<h3>IMAGES</h3>
<a href="javascript: void(0);" class="btn btn-create">
<i class="fas fa-plus fa-fw"></i> Nouvelle image
......@@ -65,6 +65,8 @@
{% block customJS %}
<script type="text/javascript">
let submitURL = null;
let grid = document.querySelector('div#grid-images');
let imagesURL = Routing.generate('api_image_read');
......@@ -106,6 +108,18 @@
editBtn.innerHTML = '<i class="fas fa-edit fa-fw"></i> Editer';
editBtn.addEventListener('click', function(e)
{
let imageId = this.dataset.imageId;
submitURL = Routing.generate('api_image_update', {'id': imageId});
AJAX('GET', submitURL, function(request)
{
let json = JSON.parse(request.responseText);
form.querySelector('#image_name').value = json.name;
form.querySelector('#image_path').value = json.path;
form.querySelector('#image_legend').value = json.legend;
form.querySelector('#image_owner').value = json.owner.id;
form.parentNode.classList.remove('hidden');
});
});
modify.appendChild(editBtn);
......@@ -145,6 +159,10 @@
{
createBtn.addEventListener('click', function(e)
{
form.querySelector('#image_name').value = null;
form.querySelector('#image_path').value = null;
form.querySelector('#image_legend').value = null;
form.querySelector('#image_owner').value = null;
form.parentNode.classList.remove('hidden');
submitURL = Routing.generate('api_image_create');
......@@ -167,7 +185,7 @@
/*
* Soumission du formulaire (create)
* Soumission du formulaire (create/update)
*/
form.addEventListener('submit', function(e)
......
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