revisions complete
This commit is contained in:
+14
-41
@@ -3,6 +3,9 @@
|
||||
namespace Lucent\Revision;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Edge\EdgeCollection;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Record\File;
|
||||
use Lucent\Record\Record;
|
||||
use Lucent\Record\RecordData;
|
||||
@@ -13,51 +16,20 @@ readonly class Revision
|
||||
{
|
||||
|
||||
function __construct(
|
||||
public string $id,
|
||||
public string $recordId,
|
||||
public string $schema,
|
||||
public System $_sys,
|
||||
public RecordData $data,
|
||||
public ?File $_file = null,
|
||||
public string $id,
|
||||
public string $recordId,
|
||||
public string $schema,
|
||||
public System $_sys,
|
||||
public RecordData $data,
|
||||
public EdgeCollection $_edges,
|
||||
public ?File $_file = null,
|
||||
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromDB(stdClass $data): Revision
|
||||
{
|
||||
|
||||
$file = json_decode($data->_file, true);
|
||||
if (!empty($file)) {
|
||||
|
||||
$file = new File(...$file);
|
||||
} else {
|
||||
$file = null;
|
||||
}
|
||||
|
||||
return new Revision(
|
||||
id: $data->id,
|
||||
recordId: $data->recordId,
|
||||
schema: $data->schema,
|
||||
_sys: System::fromArray(json_decode($data->_sys, true)),
|
||||
data: new RecordData(json_decode($data->data, true)),
|
||||
_file: $file,
|
||||
);
|
||||
}
|
||||
|
||||
public function toDB(): array
|
||||
{
|
||||
return [
|
||||
"id" => $this->id,
|
||||
"recordId" => $this->recordId,
|
||||
"schema" => $this->schema,
|
||||
"_sys" => json_encode($this->_sys),
|
||||
"_file" => json_encode($this->_file),
|
||||
"data" => json_encode($this->data),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public static function fromRecord(Record $record): Revision
|
||||
public static function fromRecord(Record $record, EdgeCollection $edges): Revision
|
||||
{
|
||||
return new Revision(
|
||||
id: (string)Str::uuid(),
|
||||
@@ -65,7 +37,8 @@ readonly class Revision
|
||||
schema: $record->schema,
|
||||
_sys: $record->_sys,
|
||||
data: $record->data,
|
||||
_file: $record->_file,
|
||||
_edges: $edges,
|
||||
_file: $record->_file
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Revision;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use function Lucent\Response\ok;
|
||||
|
||||
class RevisionController extends Controller
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private readonly RevisionRepo $revisionRepo,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$revisions = $this->revisionRepo->getByRecordId($request->route("rid"));
|
||||
return ok($revisions->toArray());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,8 +3,14 @@
|
||||
namespace Lucent\Revision;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Edge\EdgeCollection;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Record\File;
|
||||
use Lucent\Record\RecordData;
|
||||
use Lucent\Record\System;
|
||||
use PhpOption\Option;
|
||||
use stdClass;
|
||||
|
||||
class RevisionRepo
|
||||
{
|
||||
@@ -13,7 +19,7 @@ class RevisionRepo
|
||||
|
||||
public function create(Revision $revision): string
|
||||
{
|
||||
$revisionDB = $revision->toDB();
|
||||
$revisionDB = $this->toDB($revision);
|
||||
DB::table($this->table)->insert($revisionDB);
|
||||
return $revision->id;
|
||||
}
|
||||
@@ -27,13 +33,28 @@ class RevisionRepo
|
||||
$revisions = DB::table($this->table)
|
||||
->where("recordId", $rid)
|
||||
->get()
|
||||
->map([Revision::class, 'fromDB'])
|
||||
->map([$this, 'fromDB'])
|
||||
->sortByDesc("_sys.version")
|
||||
->toArray();
|
||||
|
||||
return new Collection($revisions);
|
||||
}
|
||||
|
||||
public function cleanupRecord(string $rid, int $numKeep): void
|
||||
{
|
||||
$revisionIds = DB::table($this->table)
|
||||
->where("recordId", $rid)
|
||||
->orderBy("_sys->version", "desc")
|
||||
->limit(100)
|
||||
->skip($numKeep)
|
||||
->get()
|
||||
->pluck("id");
|
||||
|
||||
DB::table($this->table)
|
||||
->whereIn("id", $revisionIds)
|
||||
->delete();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Option<Revision>
|
||||
@@ -49,6 +70,42 @@ class RevisionRepo
|
||||
return none();
|
||||
}
|
||||
|
||||
return some(Revision::fromDB($res));
|
||||
return some($this->fromDB($res));
|
||||
}
|
||||
|
||||
public function toDB(Revision $revision): array
|
||||
{
|
||||
return [
|
||||
"id" => $revision->id,
|
||||
"recordId" => $revision->recordId,
|
||||
"schema" => $revision->schema,
|
||||
"_sys" => json_encode($revision->_sys),
|
||||
"_file" => json_encode($revision->_file),
|
||||
"data" => json_encode($revision->data),
|
||||
"_edges" => $revision->_edges->toJson(),
|
||||
];
|
||||
}
|
||||
|
||||
public function fromDB(stdClass $data): Revision
|
||||
{
|
||||
$file = json_decode($data->_file, true);
|
||||
if (!empty($file)) {
|
||||
|
||||
$file = new File(...$file);
|
||||
} else {
|
||||
$file = null;
|
||||
}
|
||||
|
||||
$edges = array_map(fn($e) => Edge::fromArray($e), json_decode($data->_edges ?? "[]", true));
|
||||
|
||||
return new Revision(
|
||||
id: $data->id,
|
||||
recordId: $data->recordId,
|
||||
schema: $data->schema,
|
||||
_sys: System::fromArray(json_decode($data->_sys, true)),
|
||||
data: new RecordData(json_decode($data->data, true)),
|
||||
_edges: new EdgeCollection(...$edges),
|
||||
_file: $file
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace Lucent\Revision;
|
||||
|
||||
use Lucent\Channel\ChannelService;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Edge\EdgeCollection;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Record\Record;
|
||||
use PhpOption\Option;
|
||||
|
||||
@@ -9,11 +13,21 @@ readonly class RevisionService
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private ChannelService $channelService,
|
||||
private RevisionRepo $revisionRepo,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<Revision>
|
||||
*/
|
||||
public function getByRecordId(string $recordId): Collection
|
||||
{
|
||||
|
||||
return $this->revisionRepo->getByRecordId($recordId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Option<Revision>
|
||||
*/
|
||||
@@ -23,10 +37,16 @@ readonly class RevisionService
|
||||
return $this->revisionRepo->getByRecordIdAndVersion($recordId, $version);
|
||||
}
|
||||
|
||||
public function create(Record $record): string
|
||||
|
||||
public function create(Record $record, EdgeCollection $edges): void
|
||||
{
|
||||
$revision = Revision::fromRecord($record);
|
||||
return $this->revisionRepo->create($revision);
|
||||
$schema = $this->channelService->getSchema($record->schema)->get();
|
||||
if($schema->revisions <= 0){
|
||||
return;
|
||||
}
|
||||
$revision = Revision::fromRecord($record, $edges);
|
||||
$this->revisionRepo->create($revision);
|
||||
$this->revisionRepo->cleanupRecord($record->id,$schema->revisions);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user