Skip to content

Commit

Permalink
article images documentation
Browse files Browse the repository at this point in the history
article images basic methods
  • Loading branch information
Redjik committed Aug 26, 2015
1 parent cf00258 commit 1871467
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 2 deletions.
108 changes: 108 additions & 0 deletions api/app/Http/Controllers/ArticleImageController.php
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());
}
}
}
5 changes: 5 additions & 0 deletions api/app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@

$app->get('{id}/tags', 'App\Http\Controllers\ArticleTagController@getAll');
$app->put('{id}/tags', 'App\Http\Controllers\ArticleTagController@putMany');

$app->get('{id}/images', 'App\Http\Controllers\ArticleImageController@getAll');
$app->get('{id}/images/{group}', 'App\Http\Controllers\ArticleImageController@getAll');
$app->put('{id}/images', 'App\Http\Controllers\ArticleImageController@putMany');
$app->delete('{id}/image/{childId}/{group}', 'App\Http\Controllers\ArticleImageController@deleteOne');
});

$app->group(['prefix' => 'tags'], function (Application $app) {
Expand Down
11 changes: 10 additions & 1 deletion api/app/Models/Article.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace App\Models;

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Str;
use Rhumsaa\Uuid\Uuid;
use Spira\Model\Collection\Collection;
Expand Down Expand Up @@ -120,7 +121,7 @@ public function setPermalinkAttribute($permalink)
}

/**
* If there is no defined exerpt for the text, create it from the content
* If there is no defined excerpt for the text, create it from the content
* @param $excerpt
* @return string
*/
Expand All @@ -147,4 +148,12 @@ public function tags()
{
return $this->belongsToMany(Tag::class, 'tag_article');
}

/**
* @return BelongsToMany
*/
public function images()
{
return $this->belongsToMany(Image::class, 'image_article')->withPivot('group_type');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
Schema::create('image_article', function (Blueprint $table) {
$table->uuid('image_id');
$table->uuid('article_id');
$table->enum('group_type',['primary','carousel','extra']);
$table->enum('group_type',['primary','thumbnail','carousel']);
$table->smallInteger('position',false,true)->default(1);

$table->primary(['image_id','article_id','group_type']);
Expand Down
7 changes: 7 additions & 0 deletions api/database/seeds/ArticleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Models\Article;
use App\Models\ArticleMeta;
use App\Models\Image;
use App\Models\Tag;
use App\Models\ArticlePermalink;
use Illuminate\Database\Seeder;
Expand All @@ -23,10 +24,16 @@ public function run()
$permalinks = factory(ArticlePermalink::class, rand(0, 4))->make()->all();
$metas = factory(ArticleMeta::class, 2)->make()->all();
$tags = factory(Tag::class, 2)->make()->all();
$image1 = $entity = factory(Image::class)->create();
$image2 = $entity = factory(Image::class)->create();
$image3 = $entity = factory(Image::class)->create();
$article->save();
$article->metas()->saveMany($metas);
$article->permalinks()->saveMany($permalinks);
$article->tags()->saveMany($tags);
$article->images()->save($image1,['group_type'=>'primary']);
$article->images()->save($image2,['group_type'=>'thumbnail']);
$article->images()->save($image3,['group_type'=>'carousel']);
})
;

Expand Down
58 changes: 58 additions & 0 deletions api/resources/views/documentation/sections/article.blade.apib
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,61 @@ Note that this route returns paginated results as defined in the `Content-Range`
->json()
!!}

## Article Images [/articles/{articleId}/images]
+ Parameters
+ articleId (required, uuid, `{!! $faker->uuid !!}`) ... Unique `id` or `uri` of the article to perform action with.

### Retrieve all article's images [GET]
#### Restrictions
* Allowed - [all]

+ Response 200 (application/json)
+ Body

{!!
$factory->get(\App\Models\Image::class)
->count(3)
->json()
!!}

## Article Images with group [/articles/{articleId}/images/{group}]

This comment has been minimized.

Copy link
@zakhenry

zakhenry Aug 27, 2015

Collaborator

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.

+ Parameters
+ articleId (required, uuid, `{!! $faker->uuid !!}`) ... Unique `id` or `uri` of the article to perform action with.
+ group (required, string, `primary`) ... `group` image belongs to

### Retrieve all article's images [GET]
#### Restrictions
* Allowed - [all]

+ Response 200 (application/json)
+ Body

{!!
$factory->get(\App\Models\Image::class)
->count(3)
->json()
!!}

### Add images to article [PUT]
#### Restrictions
* Allowed - [admin]

+ Request
+ Body

{!!
$factory->get(\App\Models\Image::class)
->count(3)
->hide(['_self'])
->json()
!!}

+ Response 201 (application/json)
+ Body

{!!
$factory->get(\App\Models\Image::class)
->showOnly(['_self'])
->json()
!!}

1 change: 1 addition & 0 deletions api/src/Model/Model/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @method static Collection get
* @method static Collection findMany($ids)
* @method static Builder where($value,$operator,$operand)
* @method static Builder whereIn($column,$ids)
* @method static BaseModel skip($offset)
* @method static BaseModel take($limit)
*
Expand Down
6 changes: 6 additions & 0 deletions api/tests/integration/ArticleImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class ArticleImageTest extends TestCase
{

}

0 comments on commit 1871467

Please sign in to comment.