<?php
namespace App\Controller\Api\Prive;
use App\Entity\Contact;
use App\Entity\Prospect;
use App\Entity\Rdv;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/contact")
*/
class ApiContactController extends ApiController
{
/**
* @Route("/{id}", name="api_Contact_getById", methods={"GET"})
* @param int $id
* @return JsonResponse
*/
public function getById(int $id)
{
if ( $id > 0) {
$contact = $this->em->getRepository(Contact::class)->find($id);
if ($contact) {
return $this->apiOk($contact);
}
}
return $this->apiKo("Contact non trouvé !!");
}
/**
* @Route("/{id}", name="api_Contact_delete", methods={"DELETE"})
* @param int $id
* @return JsonResponse
*/
public function delete(int $id)
{
if ($id > 0) {
$contact = $this->em->getRepository(Contact::class)->find($id);
// 1 - Voir Régles de suppression de contacts avec malika
//$rdvs = $this->em->getRepository(Rdv::class)->findBy(["contact" => $contact]);
// if ($rdvs) {
// Suppression Logique
$contact->setActive(2);
$this->em->persist($contact);
$this->em->flush();
return $this->apiOk($id);
/* } else {
// Suppression Physique
try {
return $this->removeElement($contact);
} catch (\Exception $e) {
return $this->apiKo($e->getMessage());
}
}*/
}
return $this->apiKo("Contact non trouvé !!");
}
/**
* @Route("/", name="api_Contact_save", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
public function save(Request $request)
{
$data = json_decode($request->getContent(), true);
if (isset($data['prospect']) && isset($data['prospect']['id'])) {
$prospect = $this->getObject(Prospect::class,$data['prospect']['id']);
}
$contact = $this->saveContact($prospect,$data);
if($contact instanceof Contact){
return $this->apiOk($contact);
}else{
return $this->apiKo($contact);
}
}
/**
* @Route("/find", name="api_Contact_find", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
public function find(Request $request)
{
$data = json_decode($request->getContent(), true);
$data['filter']['and']['contact.active'] = ' <> 2 '; // ne pas affichier les elements supprimé logiquement
$sql = "
SELECT contact
FROM " . Contact::class . " contact
";
return $this->findElementsWithJoin($sql, $data);
}
}