Skip to content

Commit

Permalink
#134. Add error getter for db
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jan 31, 2019
1 parent 849ee01 commit fbb39ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/extension/BaseRelationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use rjapi\blocks\FileManager;
use rjapi\helpers\Classes;
use rjapi\helpers\ConfigHelper;
use rjapi\helpers\Errors;
use rjapi\helpers\Json;
use rjapi\helpers\MigrationsHelper;
use rjapi\types\DirsInterface;
Expand All @@ -30,14 +31,7 @@ public function relations(Request $request, $id, string $relation) : Response
{
$model = $this->getEntity($id);
if (empty($model)) {
Json::outputErrors(
[
[
JSONApiInterface::ERROR_TITLE => 'Database object ' . $this->entity . ' with $id = ' . $id .
' - not found.',
],
]
);
Json::outputErrors((new Errors())->getModelNotFound($this->entity, $id));
}

$resource = Json::getRelations($model->$relation, $relation);
Expand Down Expand Up @@ -86,18 +80,13 @@ private function presetRelations(Request $request, $id, string $relation)
{
$json = Json::decode($request->getContent());
$this->setRelationships($json, $id, true);

// set include for relations
$_GET['include'] = $relation;
$model = $this->getEntity($id);

if (empty($model)) {
Json::outputErrors(
[
[
JSONApiInterface::ERROR_TITLE => 'Database object ' . $this->entity . ' with $id = ' . $id .
' - not found.',
],
]
);
Json::outputErrors((new Errors())->getModelNotFound($this->entity, $id));
}

return $model;
Expand Down Expand Up @@ -255,6 +244,7 @@ private function saveModel(string $ucEntity, string $lowEntity, $eId, $rId) : vo
{
$relEntity = Classes::getModelEntity($ucEntity);
$model = $this->getModelEntity($relEntity, $rId);

// swap table and field trying to find rels with inverse
if (!property_exists($model, $lowEntity . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID)) {
$ucTmp = $ucEntity;
Expand All @@ -267,6 +257,7 @@ private function saveModel(string $ucEntity, string $lowEntity, $eId, $rId) : vo
$model->save();
return;
}

$model->{$lowEntity . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID} = $eId;
$model->save();
}
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/Errors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace rjapi\helpers;

use rjapi\extension\JSONApiInterface;

class Errors
{
public function getModelNotFound(string $entity, $id) : array
{
return [
[
JSONApiInterface::ERROR_TITLE => 'Database object ' . $entity . ' with $id = ' . $id .
' - not found.',
],
];
}
}

0 comments on commit fbb39ef

Please sign in to comment.