file uploads
This commit is contained in:
+17
-21
@@ -2,51 +2,47 @@
|
||||
|
||||
namespace Lucent\Revision;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Record\FileData;
|
||||
use Lucent\Record\Record;
|
||||
use Lucent\Record\RecordData;
|
||||
use Lucent\Record\System;
|
||||
|
||||
readonly class Revision
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $recordId
|
||||
* @param string $schema
|
||||
* @param System $_sys
|
||||
* @param RecordData $data
|
||||
* @param list<Edge> $_edges
|
||||
* @param FileData|null $_file
|
||||
*/
|
||||
function __construct(
|
||||
public string $id,
|
||||
public string $recordId,
|
||||
public string $schema,
|
||||
public System $_sys,
|
||||
public string $id,
|
||||
public string $recordId,
|
||||
public string $schema,
|
||||
public Carbon $createdAt,
|
||||
public Carbon $updatedAt,
|
||||
public string $createdBy,
|
||||
public string $updatedBy,
|
||||
public int $version,
|
||||
public RecordData $data,
|
||||
public array $_edges,
|
||||
public ?FileData $_file = null,
|
||||
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public array $_edges,
|
||||
) {}
|
||||
|
||||
public static function fromRecord(Record $record, array $edges): Revision
|
||||
{
|
||||
return new Revision(
|
||||
id: (string)Str::uuid(),
|
||||
id: (string) Str::uuid(),
|
||||
recordId: $record->id,
|
||||
schema: $record->schema,
|
||||
_sys: $record->_sys,
|
||||
createdAt: $record->createdAt,
|
||||
updatedAt: $record->updatedAt,
|
||||
createdBy: $record->createdBy,
|
||||
updatedBy: $record->updatedBy,
|
||||
version: $record->version,
|
||||
data: $record->data,
|
||||
_edges: $edges,
|
||||
_file: $record->_file
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,15 +5,12 @@ namespace Lucent\Revision;
|
||||
use Lucent\Database\Database;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Record\FileData;
|
||||
use Lucent\Record\RecordData;
|
||||
use Lucent\Record\System;
|
||||
use PhpOption\Option;
|
||||
use stdClass;
|
||||
|
||||
class RevisionRepo
|
||||
{
|
||||
|
||||
private string $table = "lucent_revisions";
|
||||
|
||||
public function create(Revision $revision): string
|
||||
@@ -23,17 +20,17 @@ class RevisionRepo
|
||||
return $revision->id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Collection<Revision>
|
||||
**/
|
||||
public function getByRecordId(string $rid): Collection
|
||||
{
|
||||
$revisions = Database::make()->table($this->table)
|
||||
$revisions = Database::make()
|
||||
->table($this->table)
|
||||
->where("recordId", $rid)
|
||||
->get()
|
||||
->map([$this, 'fromDB'])
|
||||
->sortByDesc("_sys.version")
|
||||
->map([$this, "fromDB"])
|
||||
->sortByDesc("version")
|
||||
->toArray();
|
||||
|
||||
return new Collection($revisions);
|
||||
@@ -41,29 +38,31 @@ class RevisionRepo
|
||||
|
||||
public function cleanupRecord(string $rid, int $numKeep): void
|
||||
{
|
||||
$revisionIds = Database::make()->table($this->table)
|
||||
$revisionIds = Database::make()
|
||||
->table($this->table)
|
||||
->where("recordId", $rid)
|
||||
->orderBy("_sys->version", "desc")
|
||||
->orderBy("version", "desc")
|
||||
->limit(100)
|
||||
->skip($numKeep)
|
||||
->get()
|
||||
->pluck("id");
|
||||
|
||||
Database::make()->table($this->table)
|
||||
Database::make()
|
||||
->table($this->table)
|
||||
->whereIn("id", $revisionIds)
|
||||
->delete();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Option<Revision>
|
||||
*/
|
||||
public function getByRecordIdAndVersion(string $rid, int $version): Option
|
||||
{
|
||||
|
||||
$res = Database::make()->table($this->table)
|
||||
$res = Database::make()
|
||||
->table($this->table)
|
||||
->where("recordId", $rid)
|
||||
->where('_sys->version', $version)->first();
|
||||
->where("version", $version)
|
||||
->first();
|
||||
|
||||
if (empty($res)) {
|
||||
return none();
|
||||
@@ -78,8 +77,11 @@ class RevisionRepo
|
||||
"id" => $revision->id,
|
||||
"recordId" => $revision->recordId,
|
||||
"schema" => $revision->schema,
|
||||
"_sys" => json_encode($revision->_sys),
|
||||
"_file" => json_encode($revision->_file),
|
||||
"createdAt" => $revision->createdAt->toDateTimeString(),
|
||||
"updatedAt" => $revision->updatedAt->toDateTimeString(),
|
||||
"createdBy" => $revision->createdBy,
|
||||
"updatedBy" => $revision->updatedBy,
|
||||
"version" => $revision->version,
|
||||
"data" => json_encode($revision->data),
|
||||
"_edges" => json_encode($revision->_edges),
|
||||
];
|
||||
@@ -87,24 +89,22 @@ class RevisionRepo
|
||||
|
||||
public function fromDB(stdClass $data): Revision
|
||||
{
|
||||
$file = json_decode($data->_file, true);
|
||||
if (!empty($file)) {
|
||||
|
||||
$file = FileData::fromArray($file);
|
||||
} else {
|
||||
$file = null;
|
||||
}
|
||||
|
||||
$edges = array_map(fn($e) => Edge::fromArray($e), json_decode($data->_edges ?? "[]", true));
|
||||
$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)),
|
||||
createdBy: $data->createdBy,
|
||||
updatedBy: $data->updatedBy,
|
||||
createdAt: $data->createdAt,
|
||||
updatedAt: $data->updatedAt,
|
||||
version: $data->version,
|
||||
data: new RecordData(json_decode($data->data, true)),
|
||||
_edges: $edges,
|
||||
_file: $file
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user