records and edgs

This commit is contained in:
2024-08-19 17:48:10 +03:00
parent 509d7c13f2
commit c97be8666e
46 changed files with 4790 additions and 1387 deletions
+129 -83
View File
@@ -2,19 +2,22 @@
namespace Lucent\Record;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
use Lucent\Account\AuthService;
use Lucent\Channel\ChannelService;
use Lucent\Edge\Edge;
use Lucent\Edge\EdgeCollection;
use Lucent\Edge\EdgeService;
use Lucent\File\FileService;
use Lucent\Id\Id;
use Lucent\LucentException;
use Lucent\Query\Query;
use Lucent\Record\InputData\EdgeInputData;
use Lucent\Record\InputData\RecordInputData;
use Lucent\Revision\RevisionService;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\Schema;
use Lucent\Schema\Type;
use Lucent\Schema\Validator\Validator;
use Lucent\Schema\Validator\ValidatorException;
@@ -29,75 +32,101 @@ readonly class RecordService
private Query $query,
private InputFormatter $inputFormatter,
private RecordRepo $recordRepo,
private EdgeService $edgeService
private EdgeService $edgeService,
private FileService $fileService,
)
{
}
/**
* @param string $url
* @param RecordInputData $data
* @param list<EdgeInputData> $edges
* @return string
* @throws LucentException
* @throws ValidatorException
*/
public
function create(
string $schemaName,
array $data,
?string $id = null,
array $file = [],
array $edges = [],
string $status = "draft",
string $uploadFromUrl = "",
public function createFromUrl(
string $url,
RecordInputData $data,
array $edges
): string
{
$schema = $this->channelService->getSchema($schemaName)->get();
$formattedData = $this->inputFormatter->fill($schemaName, new RecordData($data));
if (empty($formattedData["id"])) {
$formattedData["id"] = Id::new();
$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");
}
$uploadResult = FileService::create($schema, $uploadFromUrl, $file);
$fileData = $this->fileService->createFromUrl($schema, $url);
return $this->create($data, $fileData->recordFile, $edges);
}
$newRecordId = empty($id) ? Id::new() : $id;
$uniqueEdges = collect($edges)
->map(function ($edge, $index) use ($newRecordId, $schemaName) {
$edge['source'] = $newRecordId;
$edge['sourceSchema'] = $schemaName;
$edge["rank"] = $index;
return (array)(new Edge(...$edge));
})
->unique(fn($e) => $e['field'] . $e['source'] . $e['target'] . $e['sourceSchema'])
->values()->toArray();
$uniqueEdgesCollection = EdgeCollection::fromArray($uniqueEdges);
if ($uploadResult->isDuplicate) {
$this->edgeService->update($uploadResult->duplicateId, $uniqueEdgesCollection);
return $uploadResult->duplicateId;
public function createFromUploadedFile(
UploadedFile $uploadedFile,
RecordInputData $data,
array $edges
)
{
$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");
}
$fileData = $this->fileService->upload($schema, $uploadedFile);
return $this->create($data, $fileData->recordFile, $edges);
}
public function createFromFileData(
FileData $fileData,
RecordInputData $data,
array $edges
)
{
$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");
}
return $this->create($data, $fileData, $edges);
}
/**
* @param RecordInputData $data
* @param FileData|null $file
* @param list<EdgeInputData> $edges
* @return string
* @throws ValidatorException
*/
public function create(
RecordInputData $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(
id: $newRecordId,
schema: $schema->name,
status: Status::from($status),
schema: $data->schemaName,
status: $data->status,
_sys: System::newRecord($this->authService->currentUserId()),
data: $formattedData,
_file: $uploadResult->recordFile,
_file: !empty($file) ? FileData::fromArray(toArray($file)) : null,
);
if (Status::from($status) === Status::PUBLISHED) {
$errors = $this->recordValidator->check($schemaName, $record->data, $uniqueEdgesCollection);
if ($data->status === Status::PUBLISHED) {
$errors = $this->recordValidator->check($data->schemaName, $record->data);
if ($errors->isNotEmpty()) {
$this->recordValidator->throwException($errors);
}
}
RecordRepo::create($record);
$this->edgeService->update($record->id, $uniqueEdgesCollection);
$this->revisionService->create($record, $uniqueEdgesCollection);
$newEdges = $this->edgeService->createManyForRecord($record->id, $record->schema, $edges);
$this->revisionService->create($record, $newEdges);
return $record->id;
}
/**
* @throws LucentException
* @throws ValidatorException
@@ -105,9 +134,7 @@ readonly class RecordService
public function update(
string $id,
array $data,
string $status = "draft",
array $edges = [],
bool $updateEdges = false
Status $status,
): void
{
$record = $this->query->filter(["id" => $id])->run()->records->first();
@@ -117,52 +144,68 @@ readonly class RecordService
}
$formattedData = $this->inputFormatter->fill($record->schema, new RecordData($data));
$uniqueEdgesCollection = new EdgeCollection();
if ($updateEdges) {
$uniqueEdges = collect($edges)
->map(function ($edge, $index) {
$edge["rank"] = $index;
$edgeData = (array)(new Edge(...$edge));
return $edgeData;
})
->unique(fn($e) => $e['field'] . $e['source'] . $e['target'] . $e['sourceSchema'])
->values()->toArray();
$uniqueEdgesCollection = EdgeCollection::fromArray($uniqueEdges);
}
if (Status::from($status) === Status::PUBLISHED) {
$errors = $this->recordValidator->check($record->schema, $formattedData, $uniqueEdgesCollection);
if ($errors->isNotEmpty()) {
$this->recordValidator->throwException($errors);
}
if ($status === Status::PUBLISHED) {
$errors = $this->recordValidator->check($record->schema, $formattedData);
if ($errors->isNotEmpty()) {
$this->recordValidator->throwException($errors);
}
}
$newRecord = new Record(
id: $record->id,
schema: $record->schema,
status: Status::from($status),
status: $status,
_sys: $record->_sys->update($this->authService->currentUserId()),
data: $record->data->merge($formattedData),
_file: $record->_file,
);
RecordRepo::update($newRecord);
if ($updateEdges) {
$this->edgeService->update($newRecord->id, $uniqueEdgesCollection);
}
$this->revisionService->create($newRecord, $uniqueEdgesCollection);
$newEdges = $this->edgeService->findForSource($record->id);
$this->revisionService->create($newRecord, $newEdges);
}
/**
* @throws LucentException
* @throws ValidatorException
*/
public function updateWithEdges(
string $id,
array $data,
Status $status,
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));
if ($status === Status::PUBLISHED) {
$errors = $this->recordValidator->check($record->schema, $formattedData);
if ($errors->isNotEmpty()) {
$this->recordValidator->throwException($errors);
}
}
$newRecord = new Record(
id: $record->id,
schema: $record->schema,
status: $status,
_sys: $record->_sys->update($this->authService->currentUserId()),
data: $record->data->merge($formattedData),
_file: $record->_file,
);
RecordRepo::update($newRecord);
$newEdges = $this->edgeService->replaceManyForRecord($record->id, $record->schema, $edges);
$this->revisionService->create($newRecord, $newEdges);
}
public function changeStatusBulk(
string $status,
array $recordsIds,
@@ -195,20 +238,23 @@ readonly class RecordService
$newEdgesData = $graph->edges
->filter(fn(Edge $edge) => $edge->source == $recordId)
->values()
->map(function (Edge $edge) use ($newRecordId) {
$edge->source = $newRecordId;
return $edge->toArray();
})->toArray();
->map(fn(Edge $edge) => new EdgeInputData(
target: $edge->target,
targetSchema: $edge->targetSchema,
field: $edge->field,
))->toArray();
$record->id = $newRecordId;
return $this->create(
schemaName: $record->schema,
data: $record->data->toArray(),
id: $record->id,
file: $record->_file?->toArray() ?? [],
edges: $newEdgesData,
status: "draft"
new RecordInputData(
schemaName: $record->schema,
id: $record->id,
data: $record->data->toArray(),
status: Status::DRAFT
),
file: $record->_file,
edges: $newEdgesData
);
}
@@ -241,11 +287,11 @@ readonly class RecordService
): void
{
$revision = $this->revisionService->getByRecordIdAndVersion($recordId, $version)->get();
$this->update(
$this->updateWithEdges(
id: $revision->recordId,
data: $revision->data->toArray(),
edges: toArray($revision->_edges),
updateEdges: true
status: Status::DRAFT,
edges: array_map(fn(Edge $edge) => new EdgeInputData($edge->target, $edge->targetSchema, $edge->field), $revision->_edges),
);
}