Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/reservations #252

Merged
merged 19 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
| Separer le front par role & rajouter la securité & rajouter layout par niveau | Ossama DAHBI | https://github.com/Pietrucci-Blacher/Challenge-Semestriel-1-5IW/pull/152 |
| Workflow Demande pour devenir prestataire | Ossama DAHBI | https://github.com/Pietrucci-Blacher/Challenge-Semestriel-1-5IW/pull/133 |
| Gerer son equipe | Ossama DAHBI | https://github.com/Pietrucci-Blacher/Challenge-Semestriel-1-5IW/pull/191 |
| Gerer son planning | Ossama DAHBI | https://github.com/Pietrucci-Blacher/Challenge-Semestriel-1-5IW/pull/216 |
| Reservation | Ossama DAHBI | https://github.com/Pietrucci-Blacher/Challenge-Semestriel-1-5IW/pull/252 |
100 changes: 100 additions & 0 deletions back/api/src/Controller/Reservation/CreateReservation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace App\Controller\Reservation;

use App\Dto\CreateReservationDto;
use App\Entity\Reservation;
use App\Entity\Schedule;
use App\Repository\EstablishmentRepository;
use App\Repository\ReservationRepository;
use App\Repository\ScheduleRepository;
use App\Repository\ServiceRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Response;

class CreateReservation extends AbstractController
{
public function __construct(
readonly Security $security,
readonly EstablishmentRepository $establishmentRepository,
readonly ServiceRepository $serviceRepository,
readonly UserRepository $userRepository,
readonly ReservationRepository $reservationRepository,
readonly ScheduleRepository $scheduleRepository,
readonly EntityManagerInterface $entityManager
)
{
}

public function __invoke(CreateReservationDto $createReservationDto)
{
$establishment = $this->establishmentRepository->findOneBy(["id" => $createReservationDto->establishment_id]);
$service = $this->serviceRepository->findOneBy(["id" => $createReservationDto->service_id]);
$teacher = $this->userRepository->findOneBy(["id" => $createReservationDto->teacher_id]);


if (!$establishment || !$service || !$teacher) {
// Return a 404 Not Found response with an error message
return new Response(
json_encode(['error' => 'Establishment, service, or teacher not found']),
Response::HTTP_NOT_FOUND,
['Content-Type' => 'application/json']
);
}
if (!in_array('ROLE_TEACHER', $teacher->getRoles())) {
return new Response(
json_encode(['error' => 'The selected user is not a teacher']),
Response::HTTP_BAD_REQUEST,
['Content-Type' => 'application/json']
);
}


$startTime = new \DateTime($createReservationDto->startTime);
$endTime = new \DateTime($createReservationDto->endTime);

$conflictingSchedules = $this->scheduleRepository->findSchedulesByTeacherAndTimeRange(
$teacher->getId(),
$startTime,
$endTime
);

if (count($conflictingSchedules) > 0) {
return new Response(
json_encode(['error' => 'The teacher has a scheduling conflict']),
Response::HTTP_CONFLICT,
['Content-Type' => 'application/json']
);
}

$client = $this->security->getUser();

$schedule = new Schedule();
$schedule->setEstablishment($establishment)
->setStartTime($startTime)
->setEndTime($endTime)
->setAssignedTo($teacher)
->setReason("Prestation pour {$client->getUserIdentifier()}");

$this->entityManager->persist($schedule);
$this->entityManager->flush();

$reservation = new Reservation();
$reservation->setEstablishment($establishment)
->setService($service)
->setTeacher($teacher)
->setCustomer($this->security->getUser())
->setStartTime($startTime)
->setEndTime($endTime)
->setSpecialRequests($createReservationDto->specialRequests)
->setSchedule($schedule);

$this->entityManager->persist($reservation);
$this->entityManager->flush();

return $reservation;
}
}
14 changes: 14 additions & 0 deletions back/api/src/Dto/CreateReservationDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Dto;

class CreateReservationDto
{
public $establishment_id;
public $teacher_id;
public $service_id;
public $startTime;
public $endTime;
public $specialRequests = null;

}
48 changes: 41 additions & 7 deletions back/api/src/Entity/Establishment.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,37 +61,37 @@ class Establishment
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['establishment:read', 'team_invitation:read'])]
#[Groups(['establishment:read', 'team_invitation:read', 'service:read'])]
private ?int $id = null;

#[ORM\ManyToOne(inversedBy: 'establishments')]
#[UserField('owner')]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['establishment:read', 'establishment:write'])]
#[Groups(['establishment:read', 'establishment:write', 'service:read'])]
private ?User $owner = null;

#[ORM\Column]
#[Groups(['establishment:read', 'establishment:write'])]
#[Groups(['establishment:read', 'establishment:write', 'service:read'])]
private ?\DateTimeImmutable $createdAt = null;

#[ORM\Column(nullable: true)]
#[Groups(['establishment:read', 'establishment:write'])]
private ?\DateTimeImmutable $updatedAt = null;

#[ORM\Column(length: 50)]
#[Groups(['establishment:read', 'establishment:write', 'user:read', 'team_invitation:read'])]
#[Groups(['establishment:read', 'establishment:write', 'user:read', 'team_invitation:read', 'service:read'])]
private ?string $name = null;

#[ORM\Column(length: 50, nullable: true)]
#[Groups(['establishment:read', 'establishment:write', 'team_invitation:read'])]
#[Groups(['establishment:read', 'establishment:write', 'team_invitation:read', 'service:read'])]
private ?string $street = null;

#[ORM\Column(length: 50, nullable: true)]
#[Groups(['establishment:read', 'establishment:write', 'team_invitation:read'])]
#[Groups(['establishment:read', 'establishment:write', 'team_invitation:read', 'service:read'])]
private ?string $city = null;

#[ORM\Column(length: 5, nullable: true)]
#[Groups(['establishment:read', 'establishment:write', 'team_invitation:read'])]
#[Groups(['establishment:read', 'establishment:write', 'team_invitation:read', 'service:read'])]
private ?string $zipCode = null;


Expand All @@ -104,12 +104,16 @@ class Establishment
#[ORM\OneToMany(mappedBy: 'establishment', targetEntity: Service::class, orphanRemoval: true)]
private Collection $services;

#[ORM\OneToMany(mappedBy: 'establishment', targetEntity: Reservation::class, orphanRemoval: true)]
private Collection $reservations;

public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->teamInvitations = new ArrayCollection();
$this->schedules = new ArrayCollection();
$this->services = new ArrayCollection();
$this->reservations = new ArrayCollection();
}

#[ORM\PrePersist]
Expand Down Expand Up @@ -302,4 +306,34 @@ public function removeService(Service $service): static

return $this;
}

/**
* @return Collection<int, Reservation>
*/
public function getReservations(): Collection
{
return $this->reservations;
}

public function addReservation(Reservation $reservation): static
{
if (!$this->reservations->contains($reservation)) {
$this->reservations->add($reservation);
$reservation->setEstablishment($this);
}

return $this;
}

public function removeReservation(Reservation $reservation): static
{
if ($this->reservations->removeElement($reservation)) {
// set the owning side to null (unless already changed)
if ($reservation->getEstablishment() === $this) {
$reservation->setEstablishment(null);
}
}

return $this;
}
}
Loading
Loading