feat: add the booking domain layer
This commit is contained in:
1 parent
ef8a88ed2a
commit
cb407477bf
13 files changed
+693
-6
No files matched your search
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\BookingRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* One person holding one resource for one whole day.
|
||||
*
|
||||
* The unique constraint is the actual guard against double-booking -- checking first and then
|
||||
* inserting is racy, so callers insert and catch UniqueConstraintViolationException instead.
|
||||
*/
|
||||
#[ORM\Entity(repositoryClass: BookingRepository::class)]
|
||||
#[ORM\UniqueConstraint(name: 'uniq_booking_resource_date', columns: ['resource_id', 'date'])]
|
||||
class Booking
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
|
||||
private User $user;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(nullable: false, onDelete: 'RESTRICT')]
|
||||
private Resource $resource;
|
||||
|
||||
#[ORM\Column(type: 'date_immutable')]
|
||||
private \DateTimeImmutable $date;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private \DateTimeImmutable $createdAt;
|
||||
|
||||
public function __construct(User $user, Resource $resource, \DateTimeImmutable $date)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->resource = $resource;
|
||||
// Normalised to midnight so the unique constraint cannot be sidestepped by a time part.
|
||||
$this->date = $date->setTime(0, 0);
|
||||
$this->createdAt = new \DateTimeImmutable();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUser(): User
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function getResource(): Resource
|
||||
{
|
||||
return $this->resource;
|
||||
}
|
||||
|
||||
public function getDate(): \DateTimeImmutable
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): \DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function belongsTo(User $user): bool
|
||||
{
|
||||
return $this->user->getId() === $user->getId();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user