Skip to content

Commit

Permalink
#111. Add it_deletes_relation, Fix del rel on OneToMany/One
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Feb 4, 2019
1 parent 59bcb3a commit 57c67d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
15 changes: 10 additions & 5 deletions src/extension/BaseRelationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Schema;
use League\Fractal\Resource\Collection;
use rjapi\blocks\FileManager;
use rjapi\helpers\Classes;
Expand Down Expand Up @@ -126,13 +127,17 @@ public function deleteRelations(Request $request, $id, string $relation) : Respo
],
]
)->delete();
} else { // OneToOne/Many
$relEntity = Classes::getModelEntity($ucEntity);
$model = $this->getModelEntities(
$relEntity, [
$lowEntity . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID, $id,
} else { // OneToOne/Many - note this is always updates one row related to entity e.g.:
// find article by id and update tag_id or topic_id
$entity = Classes::getModelEntity($this->entity);

/** @var \Illuminate\Database\Eloquent\Builder $model */
$model = $this->getModelEntities(
$entity, [
ApiInterface::RAML_ID, $id,
]
);

$model->update([$relation . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID => 0]);
}
}
Expand Down
40 changes: 20 additions & 20 deletions tests/unit/extension/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,24 @@ public function it_gets_relations(array $reqData) : array
}

// @todo: deleteRelations
// /**
// * @test
// * @depends it_gets_relations
// * @param array $reqData
// */
// public function it_deletes_relation(array $reqData)
// {
// $req = $this->request([
// 'data' => [
// [
// 'type' => self::RELATION,
// 'id' => $reqData['data']['relationships'][self::RELATION]['data']['id'],
// ],
// ],
// ]);
//
// $resp = $this->baseController->deleteRelations($req, $reqData['data']['id'], self::RELATION);
//
// $this->assertEquals($resp->getStatusCode(), JSONApiInterface::HTTP_RESPONSE_CODE_NO_CONTENT);
// }
/**
* @test
* @depends it_gets_relations
* @param array $reqData
*/
public function it_deletes_relation(array $reqData)
{
$req = $this->request([
'data' => [
[
'type' => self::RELATION,
'id' => $reqData['data']['relationships'][self::RELATION]['data']['id'],
],
],
]);

$resp = $this->baseController->deleteRelations($req, $reqData['data']['id'], self::RELATION);

$this->assertEquals($resp->getStatusCode(), JSONApiInterface::HTTP_RESPONSE_CODE_NO_CONTENT);
}
}

0 comments on commit 57c67d0

Please sign in to comment.