boboko lulnar

This commit is contained in:
2026-04-20 21:07:35 +03:00
parent 57b0727788
commit 4a7eb217a1
32 changed files with 1350 additions and 858 deletions
+112 -94
View File
@@ -23,20 +23,17 @@ use Lucent\Schema\Validator\ValidatorException;
readonly class RecordService
{
public function __construct(
private AuthService $authService,
private AuthService $authService,
private RevisionService $revisionService,
private ChannelService $channelService,
private Validator $recordValidator,
private Query $query,
private InputFormatter $inputFormatter,
private RecordRepo $recordRepo,
private EdgeService $edgeService,
private FileService $fileService,
)
{
}
private ChannelService $channelService,
private Validator $recordValidator,
private Query $query,
private InputFormatter $inputFormatter,
private RecordRepo $recordRepo,
private EdgeService $edgeService,
private FileService $fileService,
) {}
/**
* @param string $url
@@ -47,14 +44,15 @@ readonly class RecordService
* @throws ValidatorException
*/
public function createFromUrl(
string $url,
string $url,
RecordInputData $data,
array $edges
): string
{
array $edges,
): string {
$schema = $this->channelService->getSchema($data->schemaName)->get();
if ($schema->type !== Type::FILES) {
throw new LucentException("You can't upload a file to a regular record");
throw new LucentException(
"You can't upload a file to a regular record",
);
}
$fileData = $this->fileService->createFromUrl($schema, $url);
if ($fileData->isDuplicate) {
@@ -64,14 +62,15 @@ readonly class RecordService
}
public function createFromUploadedFile(
UploadedFile $uploadedFile,
UploadedFile $uploadedFile,
RecordInputData $data,
array $edges
): string
{
array $edges,
): string {
$schema = $this->channelService->getSchema($data->schemaName)->get();
if ($schema->type !== Type::FILES) {
throw new LucentException("You can't upload a file to a regular record");
throw new LucentException(
"You can't upload a file to a regular record",
);
}
$fileData = $this->fileService->upload($schema, $uploadedFile);
if ($fileData->isDuplicate) {
@@ -81,14 +80,15 @@ readonly class RecordService
}
public function createFromFileData(
FileData $fileData,
FileData $fileData,
RecordInputData $data,
array $edges
): string
{
array $edges,
): string {
$schema = $this->channelService->getSchema($data->schemaName)->get();
if ($schema->type !== Type::FILES) {
throw new LucentException("You can't upload a file to a regular record");
throw new LucentException(
"You can't upload a file to a regular record",
);
}
return $this->create($data, $fileData, $edges);
}
@@ -102,12 +102,13 @@ readonly class RecordService
*/
public function create(
RecordInputData $data,
?FileData $file = null,
array $edges = []
): string
{
$formattedData = $this->inputFormatter->fill($data->schemaName, new RecordData($data->data));
?FileData $file = null,
array $edges = [],
): string {
$formattedData = $this->inputFormatter->fill(
$data->schemaName,
new RecordData($data->data),
);
$newRecordId = empty($data->id) ? Id::new() : $data->id;
$record = new Record(
@@ -120,38 +121,49 @@ readonly class RecordService
);
if ($data->status === Status::PUBLISHED) {
$errors = $this->recordValidator->check($data->schemaName, $record->data);
$errors = $this->recordValidator->check(
$data->schemaName,
$record->data,
);
if ($errors->isNotEmpty()) {
$this->recordValidator->throwException($errors);
}
}
RecordRepo::create($record);
$newEdges = $this->edgeService->createManyForRecord($record->id, $record->schema, $edges);
$newEdges = $this->edgeService->createManyForRecord(
$record->id,
$record->schema,
$edges,
);
$this->revisionService->create($record, $newEdges);
return $record->id;
}
/**
* @throws LucentException
* @throws ValidatorException
*/
public function update(
string $id,
array $data,
Status $status,
): void
public function update(string $id, array $data, Status $status): void
{
$record = $this->query->filter(["id" => $id])->run()->records->first();
$record = $this->query
->filter(["id" => $id])
->run()
->records->first();
if (empty($record)) {
throw new LucentException("Record id is missing");
}
$formattedData = $this->inputFormatter->fill($record->schema, new RecordData($data));
$formattedData = $this->inputFormatter->fill(
$record->schema,
new RecordData($data),
);
if ($status === Status::PUBLISHED) {
$errors = $this->recordValidator->check($record->schema, $formattedData);
$errors = $this->recordValidator->check(
$record->schema,
$formattedData,
);
if ($errors->isNotEmpty()) {
$this->recordValidator->throwException($errors);
}
@@ -169,7 +181,6 @@ readonly class RecordService
RecordRepo::update($newRecord);
$newEdges = $this->edgeService->findForSource($record->id);
$this->revisionService->create($newRecord, $newEdges);
}
/**
@@ -178,20 +189,28 @@ readonly class RecordService
*/
public function updateWithEdges(
string $id,
array $data,
array $data,
Status $status,
array $edges
): void
{
$record = $this->query->filter(["id" => $id])->run()->records->first();
array $edges,
): void {
$record = $this->query
->filter(["id" => $id])
->run()
->records->first();
if (empty($record)) {
throw new LucentException("Record id is missing");
}
$formattedData = $this->inputFormatter->fill($record->schema, new RecordData($data));
$formattedData = $this->inputFormatter->fill(
$record->schema,
new RecordData($data),
);
if ($status === Status::PUBLISHED) {
$errors = $this->recordValidator->check($record->schema, $formattedData);
$errors = $this->recordValidator->check(
$record->schema,
$formattedData,
);
if ($errors->isNotEmpty()) {
$this->recordValidator->throwException($errors);
}
@@ -207,15 +226,15 @@ readonly class RecordService
);
RecordRepo::update($newRecord);
$newEdges = $this->edgeService->replaceManyForRecord($record->id, $record->schema, $edges);
$newEdges = $this->edgeService->replaceManyForRecord(
$record->id,
$record->schema,
$edges,
);
$this->revisionService->create($newRecord, $newEdges);
}
public function changeStatusBulk(
string $status,
array $recordsIds,
): void
public function changeStatusBulk(string $status, array $recordsIds): void
{
RecordRepo::updateStatusBulk(Status::from($status), $recordsIds);
}
@@ -224,10 +243,7 @@ readonly class RecordService
* @throws LucentException
* @throws ValidatorException
*/
public
function clone(
string $recordId,
): string
public function clone(string $recordId): string
{
$graph = $this->query
->filter(["id" => $recordId])
@@ -240,15 +256,18 @@ readonly class RecordService
throw new LucentException("Record id is missing");
}
$newRecordId = (string)Str::uuid();
$newRecordId = (string) Str::uuid();
$newEdgesData = $graph->edges
->filter(fn(Edge $edge) => $edge->source == $recordId)
->values()
->map(fn(Edge $edge) => new EdgeInputData(
target: $edge->target,
targetSchema: $edge->targetSchema,
field: $edge->field,
))->toArray();
->map(
fn(Edge $edge) => new EdgeInputData(
target: $edge->target,
targetSchema: $edge->targetSchema,
field: $edge->field,
),
)
->toArray();
$record->id = $newRecordId;
@@ -257,62 +276,62 @@ readonly class RecordService
schemaName: $record->schema,
id: $record->id,
data: $record->data->toArray(),
status: Status::DRAFT
status: Status::DRAFT,
),
file: $record->_file,
edges: $newEdgesData
edges: $newEdgesData,
);
}
public function deleteMany(
array $recordsIds,
): void
public function deleteMany(array $recordsIds): void
{
$this->recordRepo->deleteMany($recordsIds);
}
public function emptyTrash(
string $schemaName,
): void
public function emptyTrash(string $schemaName): void
{
$schema = $this->channelService->getSchema($schemaName)->get();
$this->recordRepo->deleteTrashedBySchema($schemaName);
}
/**
* @throws LucentException
* @throws ValidatorException
*/
public function rollback(
string $recordId,
int $version,
): void
public function rollback(string $recordId, int $version): void
{
$revision = $this->revisionService->getByRecordIdAndVersion($recordId, $version)->get();
$revision = $this->revisionService
->getByRecordIdAndVersion($recordId, $version)
->get();
$this->updateWithEdges(
id: $revision->recordId,
data: $revision->data->toArray(),
status: Status::DRAFT,
edges: array_map(fn(Edge $edge) => new EdgeInputData($edge->target, $edge->targetSchema, $edge->field), $revision->_edges),
edges: array_map(
fn(Edge $edge) => new EdgeInputData(
$edge->target,
$edge->targetSchema,
$edge->field,
),
$revision->_edges,
),
);
}
public function createEmpty(
Schema $schema,
): Record
public function createEmpty(Schema $schema): Record
{
$defaultValues = $schema->fields->reduce(function ($carry, FieldInterface $f) {
$defaultValues = $schema->fields->reduce(function (
$carry,
FieldInterface $f,
) {
$carry[$f->name] = $f->default ?? null;
return $carry;
}, []);
$formattedData = $this->inputFormatter->fill($schema->name, new RecordData($defaultValues));
$formattedData = $this->inputFormatter->fill(
$schema->name,
new RecordData($defaultValues),
);
return new Record(
id: Id::new(),
@@ -322,6 +341,5 @@ readonly class RecordService
data: $formattedData,
_file: null,
);
}
}