Skip to content

Commit

Permalink
draft version of api's for accesing the eav resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sunel committed Apr 14, 2019
1 parent 7427d60 commit 78f207f
Show file tree
Hide file tree
Showing 29 changed files with 1,414 additions and 0 deletions.
107 changes: 107 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
|--------------------------------------------------------------------------
| Api Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Mods the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

/*
|--------------------------------------------------------------------------
| Common Routes
|--------------------------------------------------------------------------
|
*/

Route::get('/backend/types', [ 'as' => 'backend.types', 'uses' => 'SettingsController@backendType']);

Route::get('/frontend/types', [ 'as' => 'frontend.types', 'uses' => 'SettingsController@frontendType']);

Route::get('/select/sources', [ 'as' => 'select.sources', 'uses' => 'SettingsController@selectSources']);

/*
|--------------------------------------------------------------------------
| Entity Routes
|--------------------------------------------------------------------------
|
*/

Route::get('/entities', [ 'as' => 'entity.list', 'uses' => 'EntityController@list']);

Route::get('/entities/{code}', [ 'as' => 'entity.get', 'uses' => 'EntityController@get']);

/*
|--------------------------------------------------------------------------
| Attribute Set Routes
|--------------------------------------------------------------------------
|
*/

Route::get('/entities/{code}/set', [ 'as' => 'set.list', 'uses' => 'AttributeSetController@list']);

Route::get('/entities/{code}/set/{id}', [ 'as' => 'set.get', 'uses' => 'AttributeSetController@get']);

Route::post('/entities/{code}/set', [ 'as' => 'set.create', 'uses' => 'AttributeSetController@create']);

Route::put('/entities/{code}/set/{id}', [ 'as' => 'set.update', 'uses' => 'AttributeSetController@update']);

Route::delete('/entities/{code}/set/{id}', [ 'as' => 'set.delete', 'uses' => 'AttributeSetController@remove']);

/*
|--------------------------------------------------------------------------
| Attribute Group Routes
|--------------------------------------------------------------------------
|
*/

Route::get('/entities/{code}/set/{setId}/group', [ 'as' => 'group.list', 'uses' => 'AttributeGroupController@list']);

Route::get('/entities/{code}/set/{setId}/group/{id}/attributes', [ 'as' => 'group.attribute.list', 'uses' => 'AttributeGroupController@listAttributes']);

Route::get('/entities/{code}/set/{setId}/group/{id}', [ 'as' => 'group.get', 'uses' => 'AttributeGroupController@get']);

Route::post('/entities/{code}/set/{setId}/group', [ 'as' => 'group.create', 'uses' => 'AttributeGroupController@create']);

Route::put('/entities/{code}/set/{setId}/group/{id}', [ 'as' => 'group.update', 'uses' => 'AttributeGroupController@update']);

Route::delete('/entities/{code}/set/{setId}/group/{id}', [ 'as' => 'group.delete', 'uses' => 'AttributeGroupController@remove']);

/*
|--------------------------------------------------------------------------
| Attribute Routes
|--------------------------------------------------------------------------
|
*/

Route::get('/entities/{code}/attributes', [ 'as' => 'attribute.list', 'uses' => 'AttributeController@list']);

Route::get('/entities/{code}/attributes/{id}', [ 'as' => 'attribute.get', 'uses' => 'AttributeController@get']);

Route::post('/entities/{code}/attributes', [ 'as' => 'attribute.create', 'uses' => 'AttributeController@create']);

Route::put('/entities/{code}/attributes/{id}', [ 'as' => 'attribute.update', 'uses' => 'AttributeController@update']);

Route::delete('/entities/{code}/attributes/{id}', [ 'as' => 'attribute.delete', 'uses' => 'AttributeController@remove']);

Route::post('/entities/{entityCode}/attributes/map', [ 'as' => 'attribute.map', 'uses' => 'AttributeController@map']);

/*
|--------------------------------------------------------------------------
| Attribute Options Routes
|--------------------------------------------------------------------------
|
*/

Route::get('/entities/{entityCode}/attributes/{attrCode}/options', [ 'as' => 'options.list', 'uses' => 'AttributeOptionsController@list']);

Route::post('/entities/{entityCode}/attributes/{attrCode}/options', [ 'as' => 'options.create', 'uses' => 'AttributeOptionsController@create']);

Route::put('/entities/{entityCode}/attributes/{attrCode}/options', [ 'as' => 'options.update', 'uses' => 'AttributeOptionsController@update']);

Route::delete('/entities/{entityCode}/attributes/{attrCode}/options', [ 'as' => 'options.delete', 'uses' => 'AttributeOptionsController@remove']);

228 changes: 228 additions & 0 deletions src/Api/Http/Controllers/AttributeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php

namespace Eav\Api\Http\Controllers;

use Eav\Entity;
use Eav\Attribute as AttributeModel;
use Eav\Api\Http\Resources\Attribute;
use Eav\Api\Http\Resources\AttributeCollection;
use ApiHelper\Http\Resources\Error;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class AttributeController extends Controller
{
public function list(Request $request, $code)
{
try {
$entity = Entity::findByCode($code);
} catch (ModelNotFoundException $e) {
return (new Error([
'code' => '101',
'title' => 'Invalid Code',
'detail' => 'Given Code does not exist.',
]))->response()
->setStatusCode(404);
}

if ((int) $request->input('filter.unassigned', 0) === 1) {
$set = $request->input('filter.set', null);
$attributes = $entity->unassignedAttributes($set);
} else {
$attributes = $entity->attributes();
}

$search = $request->input('filter.search', false);

if ($search !== false) {
$attributes->where(function ($query) use ($search) {
$query->where('attribute_code', 'like', '%' . $search . '%')
->orWhere('frontend_label', 'like', '%' . $search . '%');
});
}

return new AttributeCollection(
$this->paginate($attributes->with($this->getIncludes($request)->all()))
);
}

public function create(Request $request, $code)
{
try {
$this->validateData($request);
} catch (ValidationException $e) {
return (new Error($e->validator))
->response()
->setStatusCode(400);
}

try {
$entity = Entity::findByCode($code);
} catch (ModelNotFoundException $e) {
return (new Error([
'code' => '101',
'title' => 'Invalid Code',
'detail' => 'Given Code does not exist.',
]))->response()
->setStatusCode(404);
}

try {
$request->validate([
'data.attributes.code' => Rule::unique('attributes', 'attribute_code')->where('entity_id', $entity->getKey())
], [], [
'data.attributes.code' => 'code',
]);
} catch (ValidationException $e) {
return (new Error($e->validator))
->response()
->setStatusCode(400);
}

$attributes = $request->input('data.attributes');

$data = array_only($attributes, [
'frontend_label', 'frontend_type',
'is_required', 'is_filterable',
'is_searchable', 'backend_type'
]);

$data['attribute_code'] = $attributes['code'];
$data['entity_code'] = $code;

$data['default_value'] = array_get($attributes, 'default_value', '');


return new Attribute(AttributeModel::add($data));
}

public function get(Request $request, $code, $id)
{
try {
$entity = Entity::findByCode($code);
} catch (ModelNotFoundException $e) {
return (new Error([
'code' => '101',
'title' => 'Invalid Entity Code',
'detail' => 'Given Entity Code does not exist.',
]))->response()
->setStatusCode(404);
}


try {
$attribute = AttributeModel::findByCode($id, $code);
} catch (ModelNotFoundException $e) {
return (new Error([
'code' => '101',
'title' => 'Invalid Attribute Code',
'detail' => 'Given Attribute Code does not exist.',
]))->response()
->setStatusCode(404);
}

return new Attribute($attribute);
}

public function update(Request $request, $code, $id)
{
try {
$entity = Entity::findByCode($code);
} catch (ModelNotFoundException $e) {
return (new Error([
'code' => '101',
'title' => 'Invalid Entity Code',
'detail' => 'Given Entity Code does not exist.',
]))->response()
->setStatusCode(404);
}


try {
$attribute = AttributeModel::findByCode($id, $code);
} catch (ModelNotFoundException $e) {
return (new Error([
'code' => '101',
'title' => 'Invalid Attribute Code',
'detail' => 'Given Attribute Code does not exist.',
]))->response()
->setStatusCode(404);
}

$attributes = $request->input('data.attributes');

$data = array_only($attributes, [
'frontend_label', 'frontend_type',
'is_required', 'is_filterable',
'is_searchable', 'backend_type'
]);

$data['default_value'] = array_get($attributes, 'default_value', '');

$attribute->fill($data)->save();

return new Attribute($attribute);
}

public function remove(Request $request, $code, $id)
{
try {
$entity = Entity::findByCode($code);
} catch (ModelNotFoundException $e) {
return (new Error([
'code' => '101',
'title' => 'Invalid Entity Code',
'detail' => 'Given Entity Code does not exist.',
]))->response()
->setStatusCode(404);
}


try {
$attribute = AttributeModel::findByCode($id, $code);
} catch (ModelNotFoundException $e) {
return (new Error([
'code' => '101',
'title' => 'Invalid Attribute Code',
'detail' => 'Given Attribute Code does not exist.',
]))->response()
->setStatusCode(404);
}

$attribute->delete();

return response(null, 204);
}

/**
* Validate the given request.
*
* @param \Illuminate\Http\Request $request
*/
protected function validateData(Request $request)
{
$request->validate([
'data.attributes.code' => 'required|max:255',
'data.attributes.frontend_label' => 'required',
'data.attributes.frontend_type' => [
'required',
Rule::in(config('eav.elementTypes', [])),
],
'data.attributes.is_required' => 'required|boolean',
'data.attributes.is_filterable' => 'required|boolean',
'data.attributes.is_searchable' => 'required|boolean',
'data.attributes.backend_type' => [
'required',
Rule::in(config('eav.fieldTypes', [])),
],
], [], [
'data.attributes.code' => 'code',
'data.attributes.frontend_label' => 'frontend label',
'data.attributes.frontend_type' => 'frontend type',
'data.attributes.is_required' => 'required',
'data.attributes.backend_type' => 'backend type',
]);
}
}
Loading

0 comments on commit 78f207f

Please sign in to comment.