-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload file from controller with e2e test
- Loading branch information
1 parent
e6b3cfc
commit a068730
Showing
17 changed files
with
305 additions
and
37 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
app/Http/Controllers/V1/Project/UploadProjectFileController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V1\Project; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\V1\Project\UploadProjectFileRequest; | ||
use Core\Application\Common\Inputs\FiletInput; | ||
use Core\Application\File\Gateways\FileCommandInterface; | ||
use Core\Application\Project\Commons\Gateways\ProjectMapperInterface; | ||
use Core\Domain\Enum\File\ContextFileEnum; | ||
use Core\Domain\Enum\File\ExtensionsEnum; | ||
use Core\Domain\Enum\File\TypeFileEnum; | ||
use Core\Domain\ValueObjects\BytesValueObject; | ||
use Core\Presentation\Http\Errors\ErrorPresenter; | ||
use Core\Services\Framework\FrameworkContract; | ||
use Core\Support\Exceptions\OutputErrorException; | ||
use Core\Support\Http\ResponseStatus; | ||
use Infra\Handlers\UseCases\Project\Upload\UploadProjectFileHandler; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
class UploadProjectFileController extends Controller | ||
{ | ||
public function __construct( | ||
private readonly FrameworkContract $framework, | ||
private readonly FileCommandInterface $fileCommand, | ||
private readonly ProjectMapperInterface $projectMapper | ||
) { | ||
} | ||
|
||
public function __invoke(UploadProjectFileRequest $request, string $uuid) | ||
{ | ||
$uploadedFile = $request->file('file'); | ||
$input = new FiletInput( | ||
name: $uploadedFile->name, | ||
type: TypeFileEnum::DOCUMENT, | ||
size: new BytesValueObject($uploadedFile->getSize()), | ||
extension: ExtensionsEnum::from($uploadedFile->getClientOriginalExtension()), | ||
contextFile: ContextFileEnum::PROJECT, | ||
uuid: Uuid::fromString($uuid) | ||
); | ||
try { | ||
$this->framework->transactionManager()->beginTransaction(); | ||
|
||
$projectFileHandler = new UploadProjectFileHandler( | ||
framework: $this->framework, | ||
fileProjectCommand: $this->fileCommand, | ||
projectMapper: $this->projectMapper | ||
); | ||
$fileEntity = $projectFileHandler->handle($input, $uploadedFile); | ||
|
||
$this->framework->transactionManager()->commit(); | ||
} catch (OutputErrorException $outputErrorException) { | ||
$this->framework->transactionManager()->rollBack(); | ||
return response()->json( | ||
data: (new ErrorPresenter( | ||
message: $outputErrorException->getMessage(), | ||
errors: $outputErrorException->getErrors() | ||
))->toArray(), | ||
status: $outputErrorException->getCode() | ||
); | ||
} | ||
|
||
return response()->json( | ||
data: [ | ||
'uuid' => $fileEntity->getUuid()->toString(), | ||
'project_uuid' => $uuid, | ||
'file_name' => $fileEntity->getUlidFileName() . '.' . $fileEntity->getExtension()->value, | ||
], | ||
status: ResponseStatus::OK->value | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\V1\Project; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UploadProjectFileRequest extends FormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function rules(): array | ||
{ | ||
return [ | ||
'project_uuid' => ['required', 'uuid'], | ||
'file' => ['required', 'file'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
infra/Handlers/UseCases/Project/Upload/UploadProjectFileHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Infra\Handlers\UseCases\Project\Upload; | ||
|
||
use Core\Application\Common\Inputs\FiletInput; | ||
use Core\Application\File\Gateways\FileCommandInterface; | ||
use Core\Application\Project\Commons\Gateways\ProjectMapperInterface; | ||
use Core\Application\Project\Upload\ProjectUploadFileUseCase; | ||
use Core\Domain\Entities\File\Root\FileEntity; | ||
use Core\Services\Framework\FrameworkContract; | ||
use Core\Support\Http\ResponseStatus; | ||
use Exception; | ||
use Illuminate\Http\UploadedFile; | ||
use Illuminate\Support\Facades\Storage; | ||
|
||
class UploadProjectFileHandler | ||
{ | ||
public function __construct( | ||
private readonly FrameworkContract $framework, | ||
private readonly FileCommandInterface $fileProjectCommand, | ||
private readonly ProjectMapperInterface $projectMapper | ||
) { | ||
} | ||
|
||
public function handle(FiletInput $projecFiletInput, UploadedFile $file): FileEntity | ||
{ | ||
$fileEntity = $this->applyRegisterFile($projecFiletInput); | ||
$this->pushFile($fileEntity, $file); | ||
$this->checkIfFileWasSaved($fileEntity); | ||
|
||
return $fileEntity; | ||
} | ||
|
||
private function applyRegisterFile(FiletInput $projecFiletInput): FileEntity | ||
{ | ||
$projectUploadFileUseCase = new ProjectUploadFileUseCase( | ||
framework: $this->framework, | ||
fileCommand: $this->fileProjectCommand, | ||
projectMapper: $this->projectMapper | ||
); | ||
return $projectUploadFileUseCase->execute($projecFiletInput, $this->framework->auth()->user()); | ||
} | ||
|
||
private function pushFile(FileEntity $fileEntity, UploadedFile $file): void | ||
{ | ||
$directory = dirname($fileEntity->getPath()); | ||
$baseName = basename($fileEntity->getPath()); | ||
|
||
Storage::disk(config('filesystems.default')) | ||
->putFileAs( | ||
$directory, | ||
$file, | ||
$baseName | ||
); | ||
} | ||
|
||
private function checkIfFileWasSaved(FileEntity $fileEntity): void | ||
{ | ||
$fileExists = Storage::disk(config('filesystems.default'))->exists($fileEntity->getPath()); | ||
if (!$fileExists) { | ||
throw new Exception( | ||
message: 'File not found in storage', | ||
code: ResponseStatus::INTERNAL_SERVER_ERROR->value | ||
); | ||
} | ||
$fileEntity->confirmUpload(); | ||
$this->fileProjectCommand->confirmUploadFile($fileEntity); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.