* PersonService switched to exception handled error methods
* exception messages are now returnes when an error happens * Person entity is fixed to have nullable fields
This commit is contained in:
parent
bcf87d3cdc
commit
7753739d8b
@ -3,6 +3,8 @@
|
|||||||
namespace App\Action\Person;
|
namespace App\Action\Person;
|
||||||
|
|
||||||
use App\Service\EntityServiceInterface;
|
use App\Service\EntityServiceInterface;
|
||||||
|
use Doctrine\ORM\EntityNotFoundException;
|
||||||
|
use Doctrine\ORM\ORMInvalidArgumentException;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
@ -24,14 +26,22 @@ class DeleteAction
|
|||||||
{
|
{
|
||||||
$id = $request->getAttribute('id');
|
$id = $request->getAttribute('id');
|
||||||
|
|
||||||
$result = $this->entityService->delete($id);
|
try {
|
||||||
|
$this->entityService->delete($id);
|
||||||
|
} catch (EntityNotFoundException $exception) {
|
||||||
|
$ret = new JsonResponse([
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
]);
|
||||||
|
|
||||||
$return = new JsonResponse($result);
|
return $ret->withStatus(404, sprintf('Entity with id `%s` not found.', $id));
|
||||||
|
} catch (ORMInvalidArgumentException $exception) {
|
||||||
|
$res = new JsonResponse([
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
]);
|
||||||
|
|
||||||
if (false === $result) {
|
return $res->withStatus(500, 'Failed to delete record.');
|
||||||
return $return->withStatus(500, 'Failed to delete record.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $return;
|
return new JsonResponse(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Action\Person;
|
namespace App\Action\Person;
|
||||||
|
|
||||||
use App\Action\Article\DeleteAction;
|
use App\Action\Person\DeleteAction;
|
||||||
use App\Service\Article\PersonService;
|
use App\Service\Person\PersonService;
|
||||||
use Interop\Container\ContainerInterface;
|
use Interop\Container\ContainerInterface;
|
||||||
|
|
||||||
class DeleteFactory
|
class DeleteFactory
|
||||||
|
|||||||
@ -3,6 +3,8 @@
|
|||||||
namespace App\Action\Person;
|
namespace App\Action\Person;
|
||||||
|
|
||||||
use App\Service\EntityServiceInterface;
|
use App\Service\EntityServiceInterface;
|
||||||
|
use Doctrine\ORM\NonUniqueResultException;
|
||||||
|
use Doctrine\ORM\NoResultException;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
@ -23,11 +25,20 @@ class GetAction
|
|||||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
{
|
{
|
||||||
$id = $request->getAttribute('id');
|
$id = $request->getAttribute('id');
|
||||||
|
try {
|
||||||
$entity = $this->entityService->get($id);
|
$entity = $this->entityService->get($id);
|
||||||
|
} catch (NoResultException $ex) {
|
||||||
|
$ret = new JsonResponse([
|
||||||
|
'message' => $ex->getMessage(),
|
||||||
|
]);
|
||||||
|
|
||||||
if (null === $entity) {
|
return $ret->withStatus(404, sprintf('Entity with id `%s` not found.', $id));
|
||||||
$ret = new JsonResponse(false);
|
} catch (NonUniqueResultException $ex) {
|
||||||
return $ret->withStatus(404);
|
$ret = new JsonResponse([
|
||||||
|
'message' => $ex->getMessage(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $ret->withStatus(500, 'Duplicate key in database.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonResponse($entity);
|
return new JsonResponse($entity);
|
||||||
|
|||||||
@ -4,6 +4,7 @@ namespace App\Action\Person;
|
|||||||
|
|
||||||
use App\Action\AbstractFormAction;
|
use App\Action\AbstractFormAction;
|
||||||
use App\Service\EntityServiceInterface;
|
use App\Service\EntityServiceInterface;
|
||||||
|
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
@ -25,11 +26,13 @@ class PostAction extends AbstractFormAction
|
|||||||
{
|
{
|
||||||
$data = $this->getRequestData($request);
|
$data = $this->getRequestData($request);
|
||||||
|
|
||||||
|
try {
|
||||||
$result = $this->entityService->create($data);
|
$result = $this->entityService->create($data);
|
||||||
|
} catch (NotNullConstraintViolationException $exception) {
|
||||||
if (false === $result) {
|
$res = new JsonResponse([
|
||||||
$result = new JsonResponse(false);
|
'message' => $exception->getMessage(),
|
||||||
return $result->withStatus(500, 'Failed to insert record.');
|
]);
|
||||||
|
return $res->withStatus(500, 'Failed to insert record. A required field was not set.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonResponse($result);
|
return new JsonResponse($result);
|
||||||
|
|||||||
@ -4,6 +4,7 @@ namespace App\Action\Person;
|
|||||||
|
|
||||||
use App\Action\AbstractFormAction;
|
use App\Action\AbstractFormAction;
|
||||||
use App\Service\EntityServiceInterface;
|
use App\Service\EntityServiceInterface;
|
||||||
|
use Doctrine\ORM\EntityNotFoundException;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
@ -26,11 +27,13 @@ class PutAction extends AbstractFormAction
|
|||||||
$id = $request->getAttribute('id', false);
|
$id = $request->getAttribute('id', false);
|
||||||
$data = $this->getRequestData($request);
|
$data = $this->getRequestData($request);
|
||||||
|
|
||||||
|
try {
|
||||||
$result = $this->entityService->modify($id, $data);
|
$result = $this->entityService->modify($id, $data);
|
||||||
|
} catch (EntityNotFoundException $exception) {
|
||||||
if (false === $result) {
|
$res = new JsonResponse([
|
||||||
$result = new JsonResponse(false);
|
'message' => $exception->getMessage(),
|
||||||
return $result->withStatus(500, 'Failed to update record.');
|
]);
|
||||||
|
return $res->withStatus(404, sprintf('Failed to update record with id `%s`', $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonResponse($result);
|
return new JsonResponse($result);
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use DateTime;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use JsonSerializable;
|
use JsonSerializable;
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ class Person implements JsonSerializable
|
|||||||
private $firstName;
|
private $firstName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(name="middle_name", type="string", length=150)
|
* @ORM\Column(name="middle_name", type="string", length=150, nullable=true)
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $middleName;
|
private $middleName;
|
||||||
@ -54,43 +54,43 @@ class Person implements JsonSerializable
|
|||||||
private $gender;
|
private $gender;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(name="date_of_birth", type="date", nullable=false)
|
* @ORM\Column(name="date_of_birth", type="date", nullable=true)
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $dateOfBirth;
|
private $dateOfBirth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(name="place_of_birth", type="string", length=150, nullable=false)
|
* @ORM\Column(name="place_of_birth", type="string", length=150, nullable=true)
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $placeOfBirth;
|
private $placeOfBirth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(name="primary_language", type="string", length=150)
|
* @ORM\Column(name="primary_language", type="string", length=150, nullable=true)
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $primaryLanguage;
|
private $primaryLanguage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(name="address", type="string", length=255)
|
* @ORM\Column(name="address", type="string", length=255, nullable=true)
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $address;
|
private $address;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\ManyToOne(targetEntity="Person")
|
* @ORM\ManyToOne(targetEntity="Person")
|
||||||
* @ORM\JoinColumn(name="mother_id", referencedColumnName="id")
|
* @ORM\JoinColumn(name="mother_id", referencedColumnName="id", nullable=true)
|
||||||
*/
|
*/
|
||||||
private $mother;
|
private $mother;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\ManyToOne(targetEntity="Person")
|
* @ORM\ManyToOne(targetEntity="Person")
|
||||||
* @ORM\JoinColumn(name="father_id", referencedColumnName="id")
|
* @ORM\JoinColumn(name="father_id", referencedColumnName="id", nullable=true)
|
||||||
*/
|
*/
|
||||||
private $father;
|
private $father;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(name="email", type="string", length=150)
|
* @ORM\Column(name="email", type="string", length=150, nullable=true)
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $email;
|
private $email;
|
||||||
@ -159,7 +159,7 @@ class Person implements JsonSerializable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return datetime
|
* @return DateTime
|
||||||
*/
|
*/
|
||||||
public function getDateOfBirth()
|
public function getDateOfBirth()
|
||||||
{
|
{
|
||||||
@ -224,7 +224,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setId(int $id)
|
public function setId(int $id)
|
||||||
{
|
{
|
||||||
@ -234,7 +234,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $personId
|
* @param string $personId
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setPersonId(string $personId)
|
public function setPersonId(string $personId)
|
||||||
{
|
{
|
||||||
@ -244,7 +244,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $firstName
|
* @param string $firstName
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setFirstName(string $firstName)
|
public function setFirstName(string $firstName)
|
||||||
{
|
{
|
||||||
@ -254,7 +254,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $middleName
|
* @param string $middleName
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setMiddleName(string $middleName)
|
public function setMiddleName(string $middleName)
|
||||||
{
|
{
|
||||||
@ -264,7 +264,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $lastName
|
* @param string $lastName
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setLastName(string $lastName)
|
public function setLastName(string $lastName)
|
||||||
{
|
{
|
||||||
@ -274,7 +274,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $gender
|
* @param int $gender
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setGender(int $gender)
|
public function setGender(int $gender)
|
||||||
{
|
{
|
||||||
@ -284,9 +284,9 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param datetime $dateOfBirth
|
* @param datetime $dateOfBirth
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setDateOfBirth(datetime $dateOfBirth)
|
public function setDateOfBirth(DateTime $dateOfBirth)
|
||||||
{
|
{
|
||||||
$this->dateOfBirth = $dateOfBirth;
|
$this->dateOfBirth = $dateOfBirth;
|
||||||
return $this;
|
return $this;
|
||||||
@ -294,7 +294,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $placeOfBirth
|
* @param string $placeOfBirth
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setPlaceOfBirth(string $placeOfBirth)
|
public function setPlaceOfBirth(string $placeOfBirth)
|
||||||
{
|
{
|
||||||
@ -304,7 +304,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $primaryLanguage
|
* @param string $primaryLanguage
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setPrimaryLanguage(string $primaryLanguage)
|
public function setPrimaryLanguage(string $primaryLanguage)
|
||||||
{
|
{
|
||||||
@ -314,7 +314,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $address
|
* @param string $address
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setAddress(string $address)
|
public function setAddress(string $address)
|
||||||
{
|
{
|
||||||
@ -324,7 +324,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Person $mother
|
* @param Person $mother
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setMother(Person $mother)
|
public function setMother(Person $mother)
|
||||||
{
|
{
|
||||||
@ -334,7 +334,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Person $father
|
* @param Person $father
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setFather(Person $father)
|
public function setFather(Person $father)
|
||||||
{
|
{
|
||||||
@ -344,7 +344,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $email
|
* @param string $email
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setEmail(string $email)
|
public function setEmail(string $email)
|
||||||
{
|
{
|
||||||
@ -354,7 +354,7 @@ class Person implements JsonSerializable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $active
|
* @param bool $active
|
||||||
* @return User
|
* @return Person
|
||||||
*/
|
*/
|
||||||
public function setActive(bool $active)
|
public function setActive(bool $active)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,9 +5,13 @@ namespace App\Service\Person;
|
|||||||
use App\Entity\Person;
|
use App\Entity\Person;
|
||||||
use App\Hydrator\DoctrineObject;
|
use App\Hydrator\DoctrineObject;
|
||||||
use App\Service\EntityServiceInterface;
|
use App\Service\EntityServiceInterface;
|
||||||
|
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
|
use Doctrine\ORM\EntityNotFoundException;
|
||||||
|
use Doctrine\ORM\NonUniqueResultException;
|
||||||
|
use Doctrine\ORM\NoResultException;
|
||||||
|
use Doctrine\ORM\ORMInvalidArgumentException;
|
||||||
use Doctrine\ORM\Query;
|
use Doctrine\ORM\Query;
|
||||||
use Exception;
|
|
||||||
|
|
||||||
class PersonService implements EntityServiceInterface
|
class PersonService implements EntityServiceInterface
|
||||||
{
|
{
|
||||||
@ -22,12 +26,20 @@ class PersonService implements EntityServiceInterface
|
|||||||
*/
|
*/
|
||||||
private $hydrator;
|
private $hydrator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param EntityManager $em
|
||||||
|
* @param DoctrineObject $hydrator
|
||||||
|
*/
|
||||||
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->hydrator = $hydrator;
|
$this->hydrator = $hydrator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return mixed[]
|
||||||
|
*/
|
||||||
public function getList()
|
public function getList()
|
||||||
{
|
{
|
||||||
$qb = $this->em->createQueryBuilder();
|
$qb = $this->em->createQueryBuilder();
|
||||||
@ -38,6 +50,13 @@ class PersonService implements EntityServiceInterface
|
|||||||
return $entities;
|
return $entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @return mixed
|
||||||
|
* @throws NonUniqueResultException
|
||||||
|
* @throws NoResultException
|
||||||
|
*/
|
||||||
public function get($id)
|
public function get($id)
|
||||||
{
|
{
|
||||||
$qb = $this->em->createQueryBuilder();
|
$qb = $this->em->createQueryBuilder();
|
||||||
@ -47,27 +66,36 @@ class PersonService implements EntityServiceInterface
|
|||||||
->where('p.id = :id')
|
->where('p.id = :id')
|
||||||
->setParameter('id', $id)
|
->setParameter('id', $id)
|
||||||
->getQuery()
|
->getQuery()
|
||||||
->getOneOrNullResult(Query::HYDRATE_ARRAY);
|
->getSingleResult(Query::HYDRATE_ARRAY);
|
||||||
|
|
||||||
return $entity;
|
return $entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param type $data
|
||||||
|
* @return type
|
||||||
|
* @throws NotNullConstraintViolationException
|
||||||
|
*/
|
||||||
public function create($data)
|
public function create($data)
|
||||||
{
|
{
|
||||||
$entity = $this->hydrator->hydrate($data, new Person());
|
$entity = $this->hydrator->hydrate($data, new Person());
|
||||||
try {
|
|
||||||
$this->em->persist($entity);
|
$this->em->persist($entity);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
} catch (Exception $ex) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $entity;
|
return $entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @param mixed $data
|
||||||
|
* @return Person
|
||||||
|
* @throws EntityNotFoundException
|
||||||
|
*/
|
||||||
public function modify($id, $data)
|
public function modify($id, $data)
|
||||||
{
|
{
|
||||||
if (null === ($entity = $this->em->find(Person::class, $id))) {
|
if (null === ($entity = $this->em->find(Person::class, $id))) {
|
||||||
return false;
|
throw new EntityNotFoundException('No result was found for query although at least one row was expected.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$entity = $this->hydrator->hydrate($data, $entity);
|
$entity = $this->hydrator->hydrate($data, $entity);
|
||||||
@ -77,18 +105,21 @@ class PersonService implements EntityServiceInterface
|
|||||||
return $entity;
|
return $entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @return boolean
|
||||||
|
* @throws EntityNotFoundException
|
||||||
|
* @throws ORMInvalidArgumentException
|
||||||
|
*/
|
||||||
public function delete($id)
|
public function delete($id)
|
||||||
{
|
{
|
||||||
if (null === ($entity = $this->em->find(Person::class, $id))) {
|
if (null === ($entity = $this->em->find(Person::class, $id))) {
|
||||||
return false;
|
throw new EntityNotFoundException('No result was found for query although at least one row was expected.');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
$this->em->remove($entity);
|
$this->em->remove($entity);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
} catch (Exception $ex) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user