Booking API - "api/booking/create"
- New endpoint to create bookings.
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
# SomniaRooms - API Endpoints and Objects
|
||||
|
||||
En este documento se indican los endpoints disponibles en la API de SomniaRooms, así como los mensajes de petición y respuesta en cada caso. (Vamos, un Swagger pero escrito a mano.)
|
||||
|
||||
## Config (`api/config`)
|
||||
|
||||
Permite al cliente obtener las variables de configuración del sistema. Ver más sobre las [variables de configuración](config-vars.md) en la documentación.
|
||||
|
||||
**TODO**
|
||||
|
||||
|
||||
## Room Category (`api/room/category`)
|
||||
|
||||
Estos endpoints permiten obtener la información de las categorias del sistema.
|
||||
|
||||
**TODO**
|
||||
|
||||
|
||||
## Search (`api/search`)
|
||||
|
||||
Conjunto de Endpoints para consultar el estado de las habitaciones.
|
||||
|
||||
|
||||
## Reservas (`api/booking`)
|
||||
|
||||
Endpoint para la gestión y consulta de las reservas.
|
||||
|
||||
### `api/booking/details`
|
||||
Permite obtener el detalle de una reserva.
|
||||
|
||||
Es necesario enviar la información de la reserva a consultar.
|
||||
|
||||
**Request (JSON):**
|
||||
```json
|
||||
{
|
||||
"refNumber": "2024001",
|
||||
"email": "[email protected]"
|
||||
}
|
||||
```
|
||||
|
||||
Se debe incluir el número de referencia de la reserva y el correo electrónico asociado.
|
||||
|
||||
**Response (JSON):**
|
||||
|
||||
TODO
|
||||
|
||||
|
||||
### `api/booking/create`
|
||||
|
||||
Endpoint para crear nuevas reservas.
|
||||
|
||||
Se enviarán los datos de la reserva con el siguiente formato:
|
||||
|
||||
**Request (JSON)_**
|
||||
```json
|
||||
{
|
||||
"arrivalDate": "20240510",
|
||||
"departureDate": "20240514",
|
||||
"customerDetails": {
|
||||
"name": "Juan",
|
||||
"surname": "Perez",
|
||||
"email": "[email protected]",
|
||||
"phoneNumber": "12345678"
|
||||
},
|
||||
"rooms": [
|
||||
{
|
||||
"roomCategory": 1,
|
||||
"guestNumber": 2,
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"roomCategory": 1,
|
||||
"guestNumber": 3,
|
||||
"amount": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Si la reserva es correcta se devolverá el número de referencia de la reserva:
|
||||
```json
|
||||
{
|
||||
"refNumber": "2024001",
|
||||
"email": "[email protected]",
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Booking;
|
||||
use App\Entity\BookingCustomer;
|
||||
use App\Entity\BookingRoom;
|
||||
use App\Repository\BookingRepository;
|
||||
use App\Repository\RoomCategoryRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class BookingController extends AbstractController
|
||||
{
|
||||
#[Route('/api/booking/details', name: 'app_booking_details')]
|
||||
public function getBookingDetails(Request $request, BookingRepository $repoBooking): JsonResponse
|
||||
{
|
||||
$payload = $request->toArray();
|
||||
if (\is_null($payload)) {
|
||||
throw new \Exception('Invalid Payload', 400);
|
||||
}
|
||||
|
||||
$refNumber = $payload["refNumber"];
|
||||
$customerEmail = $payload["email"];
|
||||
|
||||
$bookingList = $repoBooking->findBy(["refNumber" => $refNumber]);
|
||||
if (\is_null($bookingList) || \count($bookingList) == 0) {
|
||||
throw new HttpException(404, 'Invalid Booking');
|
||||
}
|
||||
|
||||
$customerBooking = $bookingList[0];
|
||||
if ($customerBooking->getBookingCustomer()->getEmail() != $customerEmail) {
|
||||
throw new HttpException(404, 'Invalid Booking');
|
||||
}
|
||||
|
||||
return $this->json($customerBooking);
|
||||
}
|
||||
|
||||
#[Route('/api/booking/create', name: 'app_booking_create')]
|
||||
public function newBooking(Request $request, BookingRepository $repoBooking, EntityManagerInterface $entityManager, RoomCategoryRepository $repoCat): JsonResponse
|
||||
{
|
||||
$payload = $request->toArray();
|
||||
if (\is_null($payload)) {
|
||||
throw new \Exception('Invalid Payload', 400);
|
||||
}
|
||||
|
||||
$booking = new Booking();
|
||||
try {
|
||||
$reqArrivalDate = $payload["arrivalDate"];
|
||||
$reqDepartureDate = $payload["departureDate"];
|
||||
|
||||
$arrivalDate = new \DateTime($reqArrivalDate);
|
||||
$departureDate = new \DateTime($reqDepartureDate);
|
||||
|
||||
if ($arrivalDate >= $departureDate) {
|
||||
throw new HttpException(400, "Arrival cant be before departure");
|
||||
}
|
||||
|
||||
$entityManager->beginTransaction();
|
||||
|
||||
$currentTime = new \DateTime();
|
||||
|
||||
// Create booking from request data.
|
||||
//$booking = new Booking();
|
||||
$booking->setBookingTime($currentTime);
|
||||
$booking->setArrivalDate($arrivalDate);
|
||||
$booking->setDepartureDate($departureDate);
|
||||
$booking->setRefNumber($currentTime->format("Y") . "001");
|
||||
|
||||
$entityManager->persist($booking);
|
||||
|
||||
// Create booking customer.
|
||||
$reqCustomer = $payload["customerDetails"];
|
||||
$bookingCustomer = new BookingCustomer();
|
||||
$bookingCustomer->setName($reqCustomer["name"]);
|
||||
$bookingCustomer->setSurname($reqCustomer["surname"]);
|
||||
$bookingCustomer->setEmail($reqCustomer["email"]);
|
||||
$bookingCustomer->setPhoneNumber($reqCustomer["phoneNumber"]);
|
||||
$bookingCustomer->setBooking($booking);
|
||||
|
||||
$entityManager->persist($bookingCustomer);
|
||||
|
||||
$reqRoomList = $payload["rooms"];
|
||||
if (\count($reqRoomList) == 0) {
|
||||
throw new HttpException(400, "Se debe indicar al menos una habitación");
|
||||
}
|
||||
|
||||
foreach ($reqRoomList as $roomItem) {
|
||||
$room = new BookingRoom();
|
||||
$room->setBooking($booking);
|
||||
|
||||
//Get Room category.
|
||||
$roomCategory = $repoCat->find($roomItem["roomCategory"]);
|
||||
if($roomCategory == null) {
|
||||
throw new HttpException(400, "La RoomCategory no es válida.");
|
||||
}
|
||||
|
||||
$room->setRoomCategory($roomCategory);
|
||||
$room->setGuestNumber($roomItem["guestNumber"]);
|
||||
$room->setAmount($roomItem["amount"]);
|
||||
$room->setRoomPrice(5000);
|
||||
|
||||
$entityManager->persist($room);
|
||||
}
|
||||
|
||||
$entityManager->commit();
|
||||
$entityManager->flush();
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
$entityManager->rollback();
|
||||
throw $th;
|
||||
}
|
||||
|
||||
$results = ["refNumber" => $booking->getRefNumber()];
|
||||
|
||||
return $this->json($results);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Entity;
|
||||
|
||||
use App\Repository\BookingCustomerRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation\Ignore;
|
||||
|
||||
#[ORM\Entity(repositoryClass: BookingCustomerRepository::class)]
|
||||
class BookingCustomer
|
||||
@@ -15,6 +16,7 @@ class BookingCustomer
|
||||
|
||||
#[ORM\OneToOne(inversedBy: 'bookingCustomer', cascade: ['persist', 'remove'])]
|
||||
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
|
||||
#[Ignore]
|
||||
private ?Booking $Booking = null;
|
||||
|
||||
#[ORM\Column(length: 64)]
|
||||
@@ -34,11 +36,13 @@ class BookingCustomer
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function getBooking(): ?Booking
|
||||
{
|
||||
return $this->Booking;
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function setBooking(Booking $Booking): static
|
||||
{
|
||||
$this->Booking = $Booking;
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Entity;
|
||||
|
||||
use App\Repository\BookingRoomRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation\Ignore;
|
||||
|
||||
#[ORM\Entity(repositoryClass: BookingRoomRepository::class)]
|
||||
class BookingRoom
|
||||
@@ -15,10 +16,12 @@ class BookingRoom
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'bookingRooms')]
|
||||
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
|
||||
#[Ignore]
|
||||
private ?Booking $Booking = null;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
#[Ignore]
|
||||
private ?RoomCategory $RoomCategory = null;
|
||||
|
||||
#[ORM\Column]
|
||||
@@ -35,11 +38,13 @@ class BookingRoom
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function getBooking(): ?Booking
|
||||
{
|
||||
return $this->Booking;
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function setBooking(?Booking $Booking): static
|
||||
{
|
||||
$this->Booking = $Booking;
|
||||
@@ -47,11 +52,13 @@ class BookingRoom
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function getRoomCategory(): ?RoomCategory
|
||||
{
|
||||
return $this->RoomCategory;
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function setRoomCategory(?RoomCategory $RoomCategory): static
|
||||
{
|
||||
$this->RoomCategory = $RoomCategory;
|
||||
@@ -59,6 +66,11 @@ class BookingRoom
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRoomCategoryId(): int
|
||||
{
|
||||
return $this->getRoomCategory()->getId();
|
||||
}
|
||||
|
||||
public function getGuestNumber(): ?int
|
||||
{
|
||||
return $this->GuestNumber;
|
||||
|
||||
Reference in New Issue
Block a user