-
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.
- Loading branch information
Showing
11 changed files
with
419 additions
and
3 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
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,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); | ||
} | ||
|
||
|
||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
api/database/migrations/2015_08_19_150932_create_tags_and_article_tags_table.php
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,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()); | ||
} | ||
} |
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
Oops, something went wrong.