diff --git a/src/Graph/Edge/EdgeService.php b/src/Graph/Edge/EdgeService.php index 7453070..062ed4e 100644 --- a/src/Graph/Edge/EdgeService.php +++ b/src/Graph/Edge/EdgeService.php @@ -6,15 +6,21 @@ use Lucent\Graph\Record\Contracts\RecordEdgeData; use Lucent\Graph\Record\Record; use Lucent\LucentException; use Lucent\Query\Query; +use Lucent\Schema\Schema\Schema; +use Lucent\Schema\Validator\DataValidator; use Lucent\Support\Collection; +use Lucent\Support\Option\Option; +use Lucent\Support\Result\Error; use Lucent\Support\Result\Result; use Lucent\Support\Result\Success; class EdgeService { public function __construct( - public EdgeRepo $edgeRepo, - public Mapper $mapper, + public EdgeRepo $edgeRepo, + public Mapper $mapper, + public DataValidator $recordValidator, + public ChannelService $channelService, ) { } @@ -35,9 +41,25 @@ class EdgeService public function update(EdgeData $data) :Result{ $edge = $this->mapper->fromArray(toArray($data)); + $schema = $this->getEdgeDataSchema($edge); + $errors = $this->recordValidator->check($schema->get(), $edge->data); + if ($errors->isNotEmpty()) { + return Error::create($errors); + } + $this->edgeRepo->update($edge); return Success::create($edge); } + + /** + * @param Edge $edge + * @return Option + */ + private function getEdgeDataSchema(Edge $edge): Option + { + $edgeSchemaName = $this->channelService->getSchema($edge->sourceSchema)->get()->fields->where("name", $edge->field)->first()->data; + return $this->channelService->getSchema($edgeSchemaName); + } // // /** // * @throws LucentException diff --git a/src/Graph/Edge/Mapper.php b/src/Graph/Edge/Mapper.php index 05bc5fe..5d6400f 100644 --- a/src/Graph/Edge/Mapper.php +++ b/src/Graph/Edge/Mapper.php @@ -39,7 +39,6 @@ class Mapper private function getEdgeDataSchema(Edge $edge): string { - return $this->channelService->getSchema($edge->sourceSchema)->get()->fields->where("name", $edge->field)->first()->data; } } \ No newline at end of file diff --git a/src/Graph/Record/RecordService.php b/src/Graph/Record/RecordService.php index 4470107..2494553 100644 --- a/src/Graph/Record/RecordService.php +++ b/src/Graph/Record/RecordService.php @@ -19,7 +19,7 @@ use Lucent\Query\Query; use Lucent\Revision\RevisionService; use Lucent\Schema\Field\FieldDataInterface; use Lucent\Schema\Schema\Schema; -use Lucent\Schema\Validator\Validator; +use Lucent\Schema\Validator\DataValidator; use Lucent\Schema\Validator\ValidatorError; use Lucent\Schema\Validator\ValidatorException; use Lucent\Support\Collection; @@ -34,7 +34,7 @@ readonly class RecordService private AuthService $authService, private RevisionService $revisionService, private ChannelService $channelService, - private Validator $recordValidator, + private DataValidator $recordValidator, private Query $query, private InputFormatter $inputFormatter, private RecordRepo $recordRepo, @@ -62,7 +62,7 @@ readonly class RecordService ); if ($data->status === Status::PUBLISHED) { - $errors = $this->recordValidator->check($data->schemaName, $record->data); + $errors = $this->recordValidator->check($schema, $record->data); if ($errors->isNotEmpty()) { return Error::create($errors); } @@ -88,7 +88,8 @@ readonly class RecordService $formattedData = $this->inputFormatter->fill($record->schema, new FieldData($data->data)); if ($data->status === Status::PUBLISHED) { - $errors = $this->recordValidator->check($record->schema, $record->data); + $schema = $this->channelService->getSchema($record->schema)->get(); + $errors = $this->recordValidator->check($schema, $record->data); if ($errors->isNotEmpty()) { return Error::create($errors); } diff --git a/src/Http/Controller/EdgeController.php b/src/Http/Controller/EdgeController.php index 53a0aee..e3b4bd3 100644 --- a/src/Http/Controller/EdgeController.php +++ b/src/Http/Controller/EdgeController.php @@ -28,6 +28,11 @@ class EdgeController extends Controller data: $request->input("data"), rank: $request->input("rank"), )); + + if ($res->error()->isDefined()) { + return result($res); + } + return result($res); } diff --git a/src/Http/Controller/RecordController.php b/src/Http/Controller/RecordController.php index 99efcda..a369669 100644 --- a/src/Http/Controller/RecordController.php +++ b/src/Http/Controller/RecordController.php @@ -233,7 +233,6 @@ class RecordController extends Controller } $schema = $this->channelService->getSchema($record->schema)->get(); - $recordHistory = $this->recordManager->fromSession($request->session())->push($rid)->getRecords($rid); return $this->svelte->render( layout: "channel", view: "recordEdit", diff --git a/src/LucentServiceProvider.php b/src/LucentServiceProvider.php index 7f9fe08..3b4d47a 100644 --- a/src/LucentServiceProvider.php +++ b/src/LucentServiceProvider.php @@ -70,11 +70,11 @@ class LucentServiceProvider extends ServiceProvider if ($this->app->runningInConsole()) { $this->commands([ -// CompileSchemas::class, -// RebuildThumbnails::class, -// LiveLink::class, -// RemoveOrphanEdges::class, -// GenerateJsonSchema::class, + CompileSchemas::class, + RebuildThumbnails::class, + LiveLink::class, + RemoveOrphanEdges::class, + GenerateJsonSchema::class, ]); } diff --git a/src/Schema/Validator/Validator.php b/src/Schema/Validator/DataValidator.php similarity index 93% rename from src/Schema/Validator/Validator.php rename to src/Schema/Validator/DataValidator.php index 739c142..0c89617 100644 --- a/src/Schema/Validator/Validator.php +++ b/src/Schema/Validator/DataValidator.php @@ -5,11 +5,12 @@ namespace Lucent\Schema\Validator; use Lucent\Channel\ChannelService; use Lucent\Graph\Data\FieldData; use Lucent\Schema\Field\FieldDataInterface; +use Lucent\Schema\Schema\Schema; use Lucent\Support\Collection; use Lucent\Support\Option\Option; -class Validator +class DataValidator { @@ -23,12 +24,12 @@ class Validator * @return Collection */ public function check( - string $schemaName, + Schema $schema, FieldData $data, ): Collection { - $schema = $this->channelService->getSchema($schemaName)->get(); + return $schema->getDataFields() ->map(fn(FieldDataInterface $f) => $this->validate($f, $data)) ->filter(fn(Option $error) => $error->isDefined())