-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
article images basic methods
- Loading branch information
Showing
8 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Image; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Http\Request; | ||
use Spira\Model\Collection\Collection; | ||
use Spira\Model\Model\BaseModel; | ||
use Spira\Responder\Response\ApiResponse; | ||
|
||
class ArticleImageController extends ChildEntityController | ||
{ | ||
public function getAll(Request $request, $id, $group = null) | ||
{ | ||
$model = $this->findParentEntity($id); | ||
$childEntities = $this->findAllChildren($model, $group); | ||
$childEntities = $this->getWithNested($childEntities, $request); | ||
|
||
return $this->getResponse() | ||
->transformer($this->getTransformer()) | ||
->collection($childEntities); | ||
} | ||
|
||
/** | ||
* Put many entities. | ||
* | ||
* @param Request $request | ||
* @param string $id | ||
* @param $group | ||
* @return ApiResponse | ||
*/ | ||
public function putMany(Request $request, $id, $group) | ||
{ | ||
$parent = $this->findParentEntity($id); | ||
|
||
$requestCollection = $request->data; | ||
$this->validateRequestCollection($requestCollection, $this->getValidationRules()); | ||
|
||
$existingChildModels = Image::whereIn('image_id', $this->getIds($requestCollection, 'image_id'))->get(); | ||
|
||
$childModels = $this->getChildModel() | ||
->hydrateRequestCollection($requestCollection, $existingChildModels) | ||
->each(function (BaseModel $model) { | ||
if (!$model->exists) { | ||
$model->save(); | ||
} | ||
}); | ||
|
||
foreach ($childModels as $childModel) { | ||
$this->getRelation($parent)->attach($childModel,['group_type'=>$group]); | ||
} | ||
|
||
return $this->getResponse() | ||
->transformer($this->getTransformer()) | ||
->createdCollection($childModels); | ||
} | ||
|
||
/** | ||
* Delete an entity. | ||
* | ||
* @param string $id | ||
* @param string $childId | ||
* @param $group | ||
* @return ApiResponse | ||
* @throws \Exception | ||
*/ | ||
public function deleteOne($id, $childId, $group) | ||
{ | ||
$model = $this->findParentEntity($id); | ||
$childModel = $this->findOrFailChildEntity($childId, $model, $group); | ||
|
||
$childModel->pivot->delete(); | ||
|
||
return $this->getResponse()->noContent(); | ||
} | ||
|
||
|
||
/** | ||
* @param $parent | ||
* @param null $group | ||
* @return Collection | ||
*/ | ||
protected function findAllChildren($parent, $group = null) | ||
{ | ||
$relation = $this->getRelation($parent); | ||
if (!is_null($group)){ | ||
$relation->wherePivot('group_type','=',$group); | ||
} | ||
|
||
return $relation->getResults(); | ||
} | ||
|
||
/** | ||
* @param $id | ||
* @param BaseModel $parent | ||
* @param $group | ||
* @return BaseModel | ||
*/ | ||
protected function findOrFailChildEntity($id, BaseModel $parent, $group) | ||
{ | ||
try { | ||
return $this->getRelation($parent)->wherePivot('group_type','=',$group)->findOrFail($id); | ||
} catch (ModelNotFoundException $e) { | ||
throw $this->notFoundException($this->getChildModel()->getKeyName()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
class ArticleImageTest extends TestCase | ||
{ | ||
|
||
} |
There is no need to retrieve just a group of images from an article - there won't be many of them, and the data received is tiny being just image references. It adds too much unnecessary complexity to the frontend too, when it can just do a simple filter on the response to get the just the group it wants.