add dated assignment endpoint
This commit is contained in:
parent
6afe7864be
commit
483ca6fc1d
9 changed files with 252 additions and 8 deletions
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace App\Schedule\UseCases\ListAssignmentsForDate;
|
||||
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Schedule\AssignmentForDate;
|
||||
use App\Schedule\ScheduleRepository;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
|
||||
class ListAssignmentsForDate
|
||||
{
|
||||
public function __construct(
|
||||
private ScheduleRepository $scheduleRepository,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return list<AssignmentForDate>
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
public function execute(ListAssignmentsForDateRequest $request): array
|
||||
{
|
||||
$date = $this->parseDate($request->date);
|
||||
|
||||
return $this->scheduleRepository->findAssignmentsForUserOnDate(
|
||||
$request->user,
|
||||
$date,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
private function parseDate(?string $value): DateTimeImmutable
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
throw new BadRequestException('date is required');
|
||||
}
|
||||
|
||||
$date = DateTimeImmutable::createFromFormat(
|
||||
'!Y-m-d',
|
||||
$value,
|
||||
new DateTimeZone('UTC'),
|
||||
);
|
||||
$errors = DateTimeImmutable::getLastErrors();
|
||||
if (
|
||||
$date === false
|
||||
|| $date->format('Y-m-d') !== $value
|
||||
|| ($errors !== false
|
||||
&& ($errors['warning_count'] > 0 || $errors['error_count'] > 0))
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
'date must be a valid date in YYYY-MM-DD format',
|
||||
);
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace App\Schedule\UseCases\ListAssignmentsForDate;
|
||||
|
||||
use App\User\User;
|
||||
|
||||
final readonly class ListAssignmentsForDateRequest
|
||||
{
|
||||
public function __construct(
|
||||
public User $user,
|
||||
public ?string $date,
|
||||
) {}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue