This commit is contained in:
2023-10-02 23:10:49 +03:00
commit c6cb488379
255 changed files with 18731 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
<?php
namespace Lucent\Revision;
use Illuminate\Support\Str;
use Lucent\Record\File;
use Lucent\Record\Record;
use Lucent\Record\RecordData;
use Lucent\Record\System;
use stdClass;
readonly class Revision
{
function __construct(
public string $id,
public string $recordId,
public System $_sys,
public RecordData $data,
public ?File $_file = null,
)
{
}
public function toArray(): array
{
return json_decode(json_encode($this), true);
}
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,
_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,
"_sys" => json_encode($this->_sys),
"_file" => json_encode($this->_file),
"data" => json_encode($this->data),
];
}
public static function fromRecord(Record $record): Revision
{
return new Revision(
id: (string)Str::uuid(),
recordId: $record->id,
_sys: $record->_sys,
data: $record->data,
_file: $record->_file,
);
}
}
+26
View File
@@ -0,0 +1,26 @@
<?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());
}
}
+54
View File
@@ -0,0 +1,54 @@
<?php
namespace Lucent\Revision;
use Illuminate\Support\Facades\DB;
use Lucent\Primitive\Collection;
use PhpOption\Option;
class RevisionRepo
{
private string $table = "revisions";
public function create(Revision $revision): string
{
$revisionDB = $revision->toDB();
DB::table($this->table)->insert($revisionDB);
return $revision->id;
}
/**
* @return Collection<Revision>
**/
public function getByRecordId(string $rid): Collection
{
$revisions = DB::table($this->table)
->where("recordId", $rid)
->get()
->map([Revision::class, 'fromDB'])
->sortByDesc("_sys.version")
->toArray();
return new Collection($revisions);
}
/**
* @return Option<Revision>
*/
public function getByRecordIdAndVersion(string $rid, int $version): Option
{
$res = DB::table($this->table)
->where("recordId", $rid)
->where('_sys->version', $version)->first();
if (empty($res)) {
return none();
}
return some(Revision::fromDB($res));
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace Lucent\Revision;
use Lucent\Record\Record;
use PhpOption\Option;
readonly class RevisionService
{
public function __construct(
private RevisionRepo $revisionRepo,
)
{
}
/**
* @return Option<Revision>
*/
public function getByRecordIdAndVersion(string $recordId, int $version): Option
{
return $this->revisionRepo->getByRecordIdAndVersion($recordId, $version);
}
public function create(Record $record): string
{
$revision = Revision::fromRecord($record);
return $this->revisionRepo->create($revision);
}
}