Skip to content

Commit

Permalink
Correção nos agendamentos
Browse files Browse the repository at this point in the history
  • Loading branch information
caiqueportela committed Sep 2, 2019
1 parent f723fd9 commit 518fe7a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/Controller/AgendamentosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public function __construct(
/**
* @param Request $request
* @return Response
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
* @Route("/api/agendamentos", methods={"POST"})
*/
public function criar(Request $request): Response
Expand Down Expand Up @@ -119,8 +117,6 @@ public function remover(int $id): Response
* @param int $id
* @param Request $request
* @return Response
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
* @Route("/api/agendamentos/{id}", methods={"PUT"})
*/
public function atualizar(int $id, Request $request): Response
Expand All @@ -135,7 +131,7 @@ public function atualizar(int $id, Request $request): Response
throw new \InvalidArgumentException();
}

if (!$this->validarDisponibilidade($agendamentoEnviado)) {
if (!$this->validarDisponibilidade($agendamentoEnviado, $id)) {
return new JsonResponse([
'erro' => 'Sala já reservada para o período solicitado'
], Response::HTTP_BAD_REQUEST);
Expand Down Expand Up @@ -205,10 +201,10 @@ private function criarEntidade(string $json): Agendamentos

/**
* @param Agendamentos $agendamento
* @param int|null $id
* @return bool
* @throws \Doctrine\ORM\NonUniqueResultException
*/
private function validarDisponibilidade(Agendamentos $agendamento): bool
private function validarDisponibilidade(Agendamentos $agendamento, int $id = null): bool
{
$classeSalas = Salas::class;
$classeAgendamentos = Agendamentos::class;
Expand All @@ -221,9 +217,31 @@ private function validarDisponibilidade(Agendamentos $agendamento): bool
->setParameter("dataFinal", $agendamento->getDataFim()->format('Y-m-d'))
->setParameter('horaInicial', $agendamento->getHoraInicio()->format('H:i'))
->setParameter('horaFinal', $agendamento->getHoraFim()->format('H:i'));
/** @var Agendamentos $busca */
$busca = $query->getOneOrNullResult();

return is_null($busca);
/** @var Agendamentos $agendamentosLista */
$agendamentosLista = $query->getResult();

if (is_null($agendamentosLista)) {
return true;
}

$count = 0;
foreach ($agendamentosLista as $agend) {
if ($agendamento->getHoraInicio() > $agend->getHoraFim() || $agendamento->getHoraFim() < $agend->getHoraInicio()) {
if (!is_null($id)) {
if ($agend->getId() != $id && $agend->getSala()->getId() == $agendamento->getSala()->getId()) {
$count++;
}
} else {
$count++;
}
}
}

if ($count == 0) {
return true;
} else {
return false;
}
}
}
1 change: 1 addition & 0 deletions src/Entity/Agendamentos.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function jsonSerialize()
'horaInicio' => $this->getHoraInicioFormatado(),
'horaFim' => $this->getHoraFimFormatado(),
'observacao' => $this->observacao,
'salaId' => $this->getSala()->getId(),
'_links' => [
[
'rel' => 'self',
Expand Down

0 comments on commit 518fe7a

Please sign in to comment.