Skip to content

Commit

Permalink
tags migration, crud, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Redjik committed Aug 20, 2015
1 parent 509c14e commit 7d1cc11
Show file tree
Hide file tree
Showing 11 changed files with 419 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/app/Extensions/Controller/RequestValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected function checkEntityIdMatchesRoute(Request $request, $id, BaseModel $m
}
}

if ($request->input($keyName) !== $id) {
if ((string)$request->input($keyName) !== (string)$id) {
throw new BadRequestException("Provided entity body does not match route parameter. The entity key cannot be updated");
}

Expand Down
61 changes: 61 additions & 0 deletions api/app/Http/Controllers/ArticleTagController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Created by PhpStorm.
* User: ivanmatveev
* Date: 20.08.15
* Time: 11:55
*/

namespace App\Http\Controllers;


use App\Http\Transformers\EloquentModelTransformer;
use App\Models\Article;
use App\Models\BaseModel;
use App\Models\Tag;
use Illuminate\Http\Request;
use Spira\Responder\Response\ApiResponse;

class ArticleTagController extends ChildEntityController
{
protected $relationName = 'tags';

public function __construct(Article $parentModel, EloquentModelTransformer $transformer)
{
parent::__construct($parentModel, $transformer);
}

/**
* Put many entities.
*
* @param string $id
* @param Request $request
* @return ApiResponse
*/
public function putMany(Request $request, $id)
{
$parent = $this->findParentEntity($id);

$requestCollection = $request->data;
$this->validateRequestCollection($requestCollection, $this->getValidationRules());

$existingChildModels = Tag::whereIn('tag',$this->getIds($requestCollection,'tag'))->get();

$childModels = $this->getChildModel()
->hydrateRequestCollection($requestCollection, $existingChildModels)
->each(function(BaseModel $model){
if (!$model->exists){
$model->save();
}
});


$this->getRelation($parent)->sync($childModels->lists('tag_id')->toArray());

return $this->getResponse()
->transformer($this->getTransformer())
->createdCollection($childModels);
}


}
3 changes: 2 additions & 1 deletion api/app/Http/Controllers/ChildEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Extensions\Controller\RequestValidationTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Http\Request;
use Spira\Model\Collection\Collection;
Expand Down Expand Up @@ -269,7 +270,7 @@ public function getChildModel()

/**
* @param BaseModel $model
* @return HasOneOrMany|Builder
* @return HasOneOrMany|BelongsToMany|Builder
*/
protected function getRelation(BaseModel $model)
{
Expand Down
22 changes: 22 additions & 0 deletions api/app/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: ivanmatveev
* Date: 20.08.15
* Time: 19:10
*/

namespace App\Http\Controllers;


use App\Http\Transformers\EloquentModelTransformer;
use App\Models\Tag;


class TagController extends EntityController
{
public function __construct(Tag $model, EloquentModelTransformer $transformer)
{
parent::__construct($model, $transformer);
}
}
10 changes: 10 additions & 0 deletions api/app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
$app->get('{id}/meta', 'App\Http\Controllers\ArticleMetaController@getAll');
$app->put('{id}/meta', 'App\Http\Controllers\ArticleMetaController@putMany');
$app->delete('{id}/meta/{childId}', 'App\Http\Controllers\ArticleMetaController@deleteOne');

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

$app->group(['prefix' => 'tags'], function (Application $app) {
$app->get('{id}', ['as'=>\App\Models\Tag::class, 'uses'=>'App\Http\Controllers\TagController@getOne']);
$app->post('/', 'App\Http\Controllers\TagController@postOne');
$app->patch('{id}', 'App\Http\Controllers\TagController@patchOne');
$app->delete('{id}', 'App\Http\Controllers\TagController@deleteOne');
});

$app->group(['prefix' => 'test'], function (Application $app) {
Expand Down
5 changes: 5 additions & 0 deletions api/app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@ public function metas()
{
return $this->hasMany(ArticleMeta::class, 'article_id', 'article_id');
}

public function tags()
{
return $this->belongsToMany(Tag::class, 'tag_article');
}
}
57 changes: 57 additions & 0 deletions api/app/Models/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Created by PhpStorm.
* User: redjik
* Date: 13.07.15
* Time: 15:07
*/

namespace App\Models;

use Illuminate\Database\Eloquent\ModelNotFoundException;

class Tag extends BaseModel
{
public $table = 'tags';

protected $primaryKey = 'tag_id';

public $incrementing = true;

public $timestamps = false;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['tag'];

protected static $validationRules = [
'tag' => 'required|string|alphaDashSpace'
];

/**
* Provide a UUID
*
* @param $model
* @return bool|void
*/
protected function provideUuidKey($model) {
return true;
}

/**
* @param mixed $id
* @return BaseModel
* @throws ModelNotFoundException
*/
public function findByIdentifier($id)
{
try{
return $this->where('tag','=',$id)->firstOrFail();
}catch (ModelNotFoundException $e){
return $this->findOrFail($id);
}
}
}
6 changes: 6 additions & 0 deletions api/database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@
];
});

$factory->define(App\Models\Tag::class, function (\Faker\Generator $faker) {
return [
'tag' => $faker->unique()->slug,
];
});

$factory->define(App\Models\Article::class, function (\Faker\Generator $faker) {

return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use App\Models\Tag;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTagsAndArticleTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(Tag::getTableName(), function (Blueprint $table) {
$table->integer('tag_id',true,true);
$table->string('tag', 255)->unique();
}
);

Schema::create('tag_article', function (Blueprint $table) {
$table->integer('tag_id');
$table->uuid('article_id');

$table->primary(['tag_id','article_id']);

$table->foreign('article_id')
->references('article_id')->on(\App\Models\Article::getTableName())
->onDelete('cascade');

$table->foreign('tag_id')
->references('tag_id')->on(Tag::getTableName())
->onDelete('cascade');
}
);


}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tag_article');
Schema::drop(Tag::getTableName());
}
}
2 changes: 1 addition & 1 deletion api/src/Model/Model/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public function hydrateRequestCollection(array $requestCollection, Collection $e
$models = array_map(function ($item) use ($keyName, $existingModels) {

$model = null;
$entityId = $item[$keyName];
$entityId = isset($item[$keyName])?$item[$keyName]:null;

if ($existingModels) {
$model = $existingModels->get($entityId, $this->newInstance());
Expand Down
Loading

0 comments on commit 7d1cc11

Please sign in to comment.