remove orphan edges

This commit is contained in:
2023-11-29 17:10:15 +02:00
parent 2e5c80e1f6
commit ba25850191
11 changed files with 114 additions and 16 deletions
+38 -2
View File
@@ -3,12 +3,16 @@
use Illuminate\Support\Facades\DB;
use Lucent\LucentException;
use PDOException;
use stdClass;
class EdgeRepo
{
public function __construct()
{
}
public static function insert(Edge $edge): void
public function insert(Edge $edge): void
{
try {
DB::table("edges")->insert($edge->toDB());
@@ -21,7 +25,7 @@ class EdgeRepo
}
public static function update(string $from, EdgeCollection $edges): void
public function update(string $from, EdgeCollection $edges): void
{
$edgesDB = collect($edges)->map(fn($e) => $e->toDB())->toArray();
DB::table("edges")->where("source", $from)->delete();
@@ -29,4 +33,36 @@ class EdgeRepo
}
public function findAll(): EdgeCollection
{
$edges = DB::table("edges")->get();
return new EdgeCollection(...$edges->map([$this, 'mapEdge'])->toArray());
}
public function mapEdge(stdClass $edge): Edge
{
return new Edge(
source: $edge->source,
target: $edge->target,
sourceSchema: $edge->sourceSchema,
targetSchema: $edge->targetSchema,
field: $edge->field,
rank: $edge->rank,
depth: $edge->depth ?? 0
);
}
public function remove(Edge $edge): void
{
DB::table("edges")
->where("source", $edge->source)
->where("target", $edge->target)
->where("sourceSchema", $edge->sourceSchema)
->where("targetSchema", $edge->targetSchema)
->where("field", $edge->field)
->delete();
}
}
+19 -3
View File
@@ -4,12 +4,14 @@ use Lucent\LucentException;
class EdgeService
{
public function __construct(public EdgeRepo $edgeRepo)
{
}
/**
* @throws LucentException
*/
public static function create(
public function create(
string $source,
string $target,
string $sourceSchema,
@@ -29,9 +31,23 @@ class EdgeService
field: $field,
rank: $rank,
);
EdgeRepo::insert($edge);
$this->edgeRepo->insert($edge);
return $edge;
}
public function update(string $from, EdgeCollection $edges): void
{
$this->edgeRepo->update($from, $edges);
}
public function findAll(): EdgeCollection
{
return $this->edgeRepo->findAll();
}
public function remove(Edge $edge): void
{
$this->edgeRepo->remove($edge);
}
}