drop UserRepository dependency; controller now passes the authenticated User directly via CreateTextRequest, eliminating a redundant repository lookup.
44 lines
1 KiB
PHP
44 lines
1 KiB
PHP
<?php
|
|
|
|
namespace App\Text\UseCases;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Text\Text;
|
|
use App\Text\CreateTextDto;
|
|
use App\Text\TextRepository;
|
|
use App\Node\NodeRepository;
|
|
use App\Node\CreateNodeDto;
|
|
|
|
class CreateText
|
|
{
|
|
public function __construct(
|
|
private TextRepository $textRepo,
|
|
private NodeRepository $nodeRepo,
|
|
) {}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
*/
|
|
public function execute(CreateTextRequest $request): Text
|
|
{
|
|
if ($request->name === null) {
|
|
throw new BadRequestException('name is required');
|
|
}
|
|
if ($request->user === null) {
|
|
throw new BadRequestException('user is required');
|
|
}
|
|
|
|
$text = $this->textRepo->create(new CreateTextDto(
|
|
name: $request->name,
|
|
user: $request->user,
|
|
));
|
|
|
|
$this->nodeRepo->create(new CreateNodeDto(
|
|
text: $text,
|
|
title: $text->getName(),
|
|
parentNode: null,
|
|
));
|
|
|
|
return $text;
|
|
}
|
|
}
|