create set endpoint
This commit is contained in:
parent
50df56a0cb
commit
18073f147f
4 changed files with 228 additions and 1 deletions
|
|
@ -3,15 +3,23 @@
|
|||
namespace App\Controllers;
|
||||
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Set\Set as DomainSet;
|
||||
use App\Set\SetRepository;
|
||||
use App\Set\UseCases\CreateSetWithRoot\CreateSetWithRoot;
|
||||
use App\Set\UseCases\CreateSetWithRoot\CreateSetWithRootRequest;
|
||||
use App\Shared\Files\Filesystem;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
|
||||
class SetController
|
||||
{
|
||||
public function __construct(
|
||||
private SetRepository $setRepository,
|
||||
private ElementRepository $elementRepository,
|
||||
private CreateSetWithRoot $createSetWithRoot,
|
||||
private Filesystem $filesystem,
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -27,6 +35,32 @@ class SetController
|
|||
], 200);
|
||||
}
|
||||
|
||||
public function create(Request $request): JsonResponse
|
||||
{
|
||||
$iconImage = $this->uploadedFileInput($request, 'iconImage');
|
||||
|
||||
try {
|
||||
$set = $this->createSetWithRoot->execute(
|
||||
new CreateSetWithRootRequest(
|
||||
name: $this->stringInput($request, 'name'),
|
||||
description: $this->stringInput($request, 'description'),
|
||||
iconImageContents: $iconImage['contents'],
|
||||
iconImageOriginalName: $iconImage['originalName'],
|
||||
iconImageMimeType: $iconImage['mimeType'],
|
||||
iconImageSizeBytes: $iconImage['sizeBytes'],
|
||||
)
|
||||
);
|
||||
} catch (BadRequestException $exception) {
|
||||
return new JsonResponse([
|
||||
'error' => $exception->getMessage(),
|
||||
], 400);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'set' => $this->buildSetPayload($set),
|
||||
], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* id: int,
|
||||
|
|
@ -44,8 +78,77 @@ class SetController
|
|||
'id' => $set->getId(),
|
||||
'name' => $set->getName(),
|
||||
'description' => $set->getDescription(),
|
||||
'iconImageUrl' => $set->getIconImageUrl(),
|
||||
'iconImageUrl' => $this->storageUrl(
|
||||
$set->getIconImageUrl(),
|
||||
'set-icons/',
|
||||
),
|
||||
'rootElementId' => $rootElement?->getId(),
|
||||
];
|
||||
}
|
||||
|
||||
private function stringInput(Request $request, string $key): ?string
|
||||
{
|
||||
if (! $request->exists($key)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = $request->input($key);
|
||||
if ($value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (! is_string($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* contents: string|null,
|
||||
* originalName: string|null,
|
||||
* mimeType: string|null,
|
||||
* sizeBytes: int|null
|
||||
* }
|
||||
*/
|
||||
private function uploadedFileInput(Request $request, string $key): array
|
||||
{
|
||||
$emptyFileInput = [
|
||||
'contents' => null,
|
||||
'originalName' => null,
|
||||
'mimeType' => null,
|
||||
'sizeBytes' => null,
|
||||
];
|
||||
|
||||
if (! $request->hasFile($key)) {
|
||||
return $emptyFileInput;
|
||||
}
|
||||
|
||||
$file = $request->file($key);
|
||||
if (! $file instanceof UploadedFile) {
|
||||
return $emptyFileInput;
|
||||
}
|
||||
|
||||
$realPath = $file->getRealPath();
|
||||
if ($realPath === false) {
|
||||
return $emptyFileInput;
|
||||
}
|
||||
|
||||
return [
|
||||
'contents' => (string) file_get_contents($realPath),
|
||||
'originalName' => $file->getClientOriginalName(),
|
||||
'mimeType' => (string) $file->getMimeType(),
|
||||
'sizeBytes' => (int) $file->getSize(),
|
||||
];
|
||||
}
|
||||
|
||||
private function storageUrl(string $path, string $folderPrefix): string
|
||||
{
|
||||
if (str_starts_with($path, $folderPrefix)) {
|
||||
return $this->filesystem->url($path);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
106
backend/app/Set/UseCases/CreateSetWithRoot/CreateSetWithRoot.php
Normal file
106
backend/app/Set/UseCases/CreateSetWithRoot/CreateSetWithRoot.php
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
namespace App\Set\UseCases\CreateSetWithRoot;
|
||||
|
||||
use App\Element\CreateElementDto;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Set\CreateSetDto;
|
||||
use App\Set\Set;
|
||||
use App\Set\SetRepository;
|
||||
use App\Shared\Files\FileToUpload;
|
||||
use App\Shared\Files\Filesystem;
|
||||
|
||||
class CreateSetWithRoot
|
||||
{
|
||||
private const ALLOWED_MIME_TYPES = [
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'image/webp',
|
||||
];
|
||||
|
||||
private const MAX_SIZE_BYTES = 5 * 1024 * 1024;
|
||||
|
||||
public function __construct(
|
||||
private SetRepository $setRepository,
|
||||
private ElementRepository $elementRepository,
|
||||
private Filesystem $filesystem,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
public function execute(CreateSetWithRootRequest $request): Set
|
||||
{
|
||||
if ($request->name === null || $request->name === '') {
|
||||
throw new BadRequestException('name is required');
|
||||
}
|
||||
if ($request->description === null || $request->description === '') {
|
||||
throw new BadRequestException('description is required');
|
||||
}
|
||||
|
||||
$iconPath = $this->uploadIconImage($request);
|
||||
$set = $this->setRepository->create(new CreateSetDto(
|
||||
name: $request->name,
|
||||
description: $request->description,
|
||||
iconImageUrl: $iconPath,
|
||||
));
|
||||
|
||||
$this->elementRepository->create(new CreateElementDto(
|
||||
set: $set,
|
||||
title: $set->getName(),
|
||||
description: $set->getDescription(),
|
||||
iconImageUrl: $iconPath,
|
||||
richText: '',
|
||||
shortPdfPath: null,
|
||||
longPdfPath: null,
|
||||
youtubeUrl: null,
|
||||
parentElement: null,
|
||||
));
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
private function uploadIconImage(CreateSetWithRootRequest $request): string
|
||||
{
|
||||
if (
|
||||
$request->iconImageContents === null
|
||||
|| $request->iconImageOriginalName === null
|
||||
|| $request->iconImageMimeType === null
|
||||
|| $request->iconImageSizeBytes === null
|
||||
) {
|
||||
throw new BadRequestException('icon image is required');
|
||||
}
|
||||
|
||||
if (
|
||||
! in_array(
|
||||
$request->iconImageMimeType,
|
||||
self::ALLOWED_MIME_TYPES,
|
||||
true,
|
||||
)
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
'icon image must be a jpeg, png or webp image',
|
||||
);
|
||||
}
|
||||
|
||||
if ($request->iconImageSizeBytes > self::MAX_SIZE_BYTES) {
|
||||
throw new BadRequestException(
|
||||
'icon image must be 5MB or smaller',
|
||||
);
|
||||
}
|
||||
|
||||
$iconImage = new FileToUpload(
|
||||
contents: $request->iconImageContents,
|
||||
originalName: $request->iconImageOriginalName,
|
||||
mimeType: $request->iconImageMimeType,
|
||||
sizeBytes: $request->iconImageSizeBytes,
|
||||
);
|
||||
|
||||
return $this->filesystem->upload($iconImage, 'set-icons');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace App\Set\UseCases\CreateSetWithRoot;
|
||||
|
||||
class CreateSetWithRootRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $name,
|
||||
public ?string $description,
|
||||
public ?string $iconImageContents,
|
||||
public ?string $iconImageOriginalName,
|
||||
public ?string $iconImageMimeType,
|
||||
public ?int $iconImageSizeBytes,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,8 @@ Route::post('/logout', [AuthController::class, 'logout']);
|
|||
Route::get('/me', [AuthController::class, 'me'])
|
||||
->middleware(AuthMiddleware::class);
|
||||
Route::get('/sets', [SetController::class, 'index']);
|
||||
Route::post('/sets', [SetController::class, 'create'])
|
||||
->middleware(AuthMiddleware::class);
|
||||
Route::get('/elements/{id}', [ElementController::class, 'show']);
|
||||
Route::get('/element-pdfs/{folder}/{fileName}', [
|
||||
ElementController::class,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue