init
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user