This commit is contained in:
2023-10-04 13:32:30 +03:00
parent 215d238505
commit 1ca5f4e521
82 changed files with 519 additions and 1889 deletions
+2 -7
View File
@@ -3,9 +3,7 @@
namespace Lucent\Record;
use Illuminate\Contracts\Session\Session;
use Lucent\Primitive\Collection;
use Lucent\Query\Query;
use Lucent\Schema\Schema;
class Manager
{
@@ -79,19 +77,16 @@ class Manager
}, []);
}
/**
* @param Collection<Schema> $schemas
*/
public function getRecords(?string $ignoreId = null): array
{
$queryResult = $this->query
$graph = $this->query
->filter(["id_in" => $this->getIdsExcept($ignoreId)])
->limit(7)
->run();
$graph = $queryResult->getQueryRecords();
return $this->order($graph->records->toArray());
}
+4 -23
View File
@@ -9,6 +9,8 @@ class QueryRecord
function __construct(
public string $id,
public string $schema,
public Status $status,
public System $_sys,
public RecordData $data,
public bool $isRoot,
@@ -18,33 +20,12 @@ class QueryRecord
{
}
public function toArray(): array
{
return json_decode(json_encode($this), true);
}
/**
* @throws LucentException
*/
public static function fromArray(array $data): QueryRecord
{
return new QueryRecord(
id: $data["id"],
_sys: System::fromArray($data["_sys"]),
data: new RecordData($data["data"]),
isRoot: $data["isRoot"] ?? false,
_file: $data["_file"] ? new File(...$data["_file"]) : null,
);
}
public static function fromRecord(Record $record): QueryRecord
{
return new QueryRecord(
id: $record->id,
schema: $record->schema,
status: $record->status,
_sys: $record->_sys,
data: $record->data,
isRoot: false,
+6 -18
View File
@@ -11,6 +11,8 @@ class Record implements JsonSerializable
function __construct(
public string $id,
public string $schema,
public Status $status,
public System $_sys,
public RecordData $data,
public ?File $_file = null,
@@ -18,15 +20,13 @@ class Record implements JsonSerializable
{
}
public function toArray(): array
{
return \json_decode(\json_encode($this), true);
}
public function toDB(): array
{
return [
"id" => $this->id,
"status" => $this->status->value,
"schema" => $this->schema,
"_sys" => json_encode($this->_sys),
"_file" => json_encode($this->_file),
"data" => json_encode($this->data),
@@ -46,6 +46,8 @@ class Record implements JsonSerializable
return new Record(
id: $data->id,
schema: $data->schema,
status: Status::from($data->status),
_sys: System::fromArray(json_decode($data->_sys, true)),
data: new RecordData(json_decode($data->data, true)),
_file: $file,
@@ -58,20 +60,6 @@ class Record implements JsonSerializable
return $this;
}
public static function fromArray(array $data): Record
{
$file = null;
if (!empty($data["_file"])) {
$file = File::fromArray($data["_file"]);
}
return new Record(
id: $data["id"],
_sys: System::fromArray($data["_sys"]),
data: new RecordData($data["data"]),
_file: $file,
);
}
}
-31
View File
@@ -1,31 +0,0 @@
<?php
namespace Lucent\Record;
use Lucent\Edge\QueryEdge;
class RecordGraph
{
/**
* @param \Lucent\Edge\QueryEdge[] $edges
* @param \Lucent\Record\EdgeRecord[] $nodes
*/
function __construct(
public readonly array $edges,
public readonly array $nodes,
) {
}
public function toArray(): array
{
return \json_decode(\json_encode($this), true);
}
public static function fromArray(array $data): RecordGraph
{
return new RecordGraph(
edges: collect($data["edges"] ?? [])->map(fn ($edge) => new QueryEdge(...$edge))->toArray(),
nodes: collect($data["nodes"] ?? [])->map(fn ($node) => EdgeRecord::fromArray($node))->toArray(),
);
}
}
+2 -2
View File
@@ -18,10 +18,10 @@ class RecordRepo
/**
* @param array<string> $ids
*/
public static function updateStatusBulk(RecordStatus $status, array $ids): void
public static function updateStatusBulk(Status $status, array $ids): void
{
DB::table("records")->whereIn("id", $ids)->update([
'_sys->status' => $status->value()
'status' => $status->value
]);
}
+22 -25
View File
@@ -60,11 +60,9 @@ readonly class RecordService
$uploadResult = FileService::create($schema, $uploadFromUrl, $file);
$uniqueEdges = collect($edges)
->map(fn($e) => (array)(new Edge(...$e)))
->map(function ($edge, $index) {
$edgeData = (array)(new Edge(...$edge));
$edgeData["rank"] = $index;
return $edgeData;
$edge["rank"] = $index;
return (array)(new Edge(...$edge));
})
->unique(fn($e) => $e['field'] . $e['source'] . $e['target'] . $e['sourceSchema'])
->values()->toArray();
@@ -77,7 +75,9 @@ readonly class RecordService
$record = new Record(
id: $id ?? Id::new(),
_sys: System::newRecord($schema, $this->authService->currentUserId(), $status),
schema: $schema->name,
status: Status::from($status),
_sys: System::newRecord($this->authService->currentUserId()),
data: $formattedData,
_file: $uploadResult->recordFile,
);
@@ -109,33 +109,34 @@ readonly class RecordService
): void
{
$queryResult = $this->query->filter(["id" => $id])->run();
$record = $queryResult->getQueryRecords()->records[0] ?? null;
$record = $this->query->filter(["id" => $id])->run()->records->first();
if (empty($record)) {
throw new LucentException("Record id is missing");
}
$formattedData = $this->inputFormatter->fill($record->_sys->schema, new RecordData($data));
$formattedData = $this->inputFormatter->fill($record->schema, new RecordData($data));
if ($updateEdges) {
$uniqueEdges = collect($edges)
->map(function ($edge, $index) {
$edge["rank"] = $index;
$edgeData = (array)(new Edge(...$edge));
$edgeData["rank"] = $index;
return $edgeData;
})
->unique(fn($e) => $e['field'] . $e['source'] . $e['target'] . $e['sourceSchema'])
->values()->toArray();
$uniqueEdgesCollection = EdgeCollection::fromArray($uniqueEdges);
$errors = $this->recordValidator->check($record->_sys->schema, $formattedData, $uniqueEdgesCollection);
$errors = $this->recordValidator->check($record->schema, $formattedData, $uniqueEdgesCollection);
} else {
$errors = $this->recordValidator->check($record->_sys->schema, $formattedData, null);
$errors = $this->recordValidator->check($record->schema, $formattedData, null);
}
$newRecord = new Record(
id: $record->id,
_sys: $record->_sys->update($this->authService->currentUserId(), $status),
schema: $record->schema,
status: Status::from($status),
_sys: $record->_sys->update($this->authService->currentUserId()),
data: $record->data->merge($formattedData),
_file: $record->_file,
);
@@ -162,8 +163,7 @@ readonly class RecordService
array $recordsIds,
): void
{
$recordsStatus = (new RecordStatus($status));
RecordRepo::updateStatusBulk($recordsStatus, $recordsIds);
RecordRepo::updateStatusBulk(Status::from($status), $recordsIds);
}
/**
@@ -175,18 +175,17 @@ readonly class RecordService
string $recordId,
): string
{
$queryResult = $this->query
$graph = $this->query
->filter(["id" => $recordId])
->limit(1)
->childrenDepth(1)
->runWithCount();
->run();
$graph = $queryResult->getQueryRecords();
$record = $graph->records[0] ?? null;
$record = $graph->records->first();
if (empty($record)) {
throw new LucentException("Record id is missing");
}
$newRecordId = (string)Str::uuid();
$newEdgesData = $graph->edges
->filter(fn(Edge $edge) => $edge->source == $recordId)
@@ -199,7 +198,7 @@ readonly class RecordService
$record->id = $newRecordId;
return $this->create(
schemaName: $record->_sys->schema,
schemaName: $record->schema,
data: $record->data->toArray(),
id: $record->id,
file: $record->_file?->toArray() ?? [],
@@ -223,18 +222,14 @@ readonly class RecordService
* @throws ValidatorException
*/
public function rollback(
string $userId,
string $recordId,
int $version,
): void
{
$revision = $this->revisionService->getByRecordIdAndVersion($recordId, $version)->get();
$this->update(
userId: $userId,
id: $revision->recordId,
data: $revision->data->toArray(),
status: $revision->_sys->status,
updateEdges: false
);
}
@@ -254,7 +249,9 @@ readonly class RecordService
return new Record(
id: Id::new(),
_sys: System::newRecord($schema, $userId),
schema: $schema->name,
status: Status::DRAFT,
_sys: System::newRecord($userId),
data: $formattedData,
_file: null,
);
-31
View File
@@ -1,31 +0,0 @@
<?php
namespace Lucent\Record;
class RecordStatus
{
private string $value;
public function __construct(
public readonly ?string $status = null,
)
{
if (empty($status)) {
$this->value = "draft";
return;
}
$validStatuses = ["trashed", "published", "draft"];
if (!in_array($status, $validStatuses)) {
$status = "draft";
}
$this->value = $status;
}
public function value(): string
{
return $this->value;
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Lucent\Record;
enum Status: string
{
case DRAFT = "draft";
case PUBLISHED = "published";
case TRASHED = "trashed";
}
+8 -17
View File
@@ -5,18 +5,16 @@ namespace Lucent\Record;
use Carbon\Carbon;
use Lucent\Schema\Schema;
class System
readonly class System
{
function __construct(
public readonly string $schema,
public readonly int $version,
public readonly string $status,
public readonly string $createdBy,
public readonly string $updatedBy,
public readonly string $createdAt,
public readonly string $updatedAt,
public int $version,
public string $createdBy,
public string $updatedBy,
public string $createdAt,
public string $updatedAt,
)
{
@@ -26,9 +24,7 @@ class System
public static function fromArray(array $data): System
{
return new System(
schema: data_get($data, "schema"),
version: data_get($data, "version"),
status: data_get($data, "status"),
createdBy: data_get($data, "createdBy"),
updatedBy: data_get($data, "updatedBy"),
createdAt: data_get($data, "createdAt"),
@@ -37,13 +33,11 @@ class System
}
public static function newRecord(Schema $schema, string $userId, ?string $status = null): System
public static function newRecord(string $userId): System
{
$now = Carbon::now()->toJson();
return new System(
schema: $schema->name,
version: 1,
status: (new RecordStatus($status))->value(),
createdBy: $userId,
updatedBy: $userId,
createdAt: $now,
@@ -52,14 +46,11 @@ class System
}
public function update(string $userId, ?string $status = null): System
public function update(string $userId): System
{
$now = Carbon::now()->toJson();
$newStatus = $status ?? $this->status;
return new System(
schema: $this->schema,
version: $this->version + 1,
status: (new RecordStatus($newStatus))->value(),
createdBy: $this->createdBy,
updatedBy: $userId,
createdAt: $this->createdAt,