Skip to content

Commit

Permalink
#100. Add createBulk impl
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Aug 16, 2018
1 parent b89251a commit f2c2c9c
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 25 deletions.
17 changes: 9 additions & 8 deletions src/extension/ApiController.php
Expand Up @@ -27,13 +27,13 @@ class ApiController extends Controller implements JSONApiInterface
// JSON API support enabled by default
protected $jsonApi = true;

private $props = [];
private $entity;
protected $props = [];
protected $entity;
/** @var BaseModel $model */
private $model;
protected $model;
/** @var EntitiesTrait $modelEntity */
private $modelEntity;
private $middleWare;
protected $middleWare;
private $relsRemoved = false;
private $defaultOrderBy = [];
/** @var ConfigOptions $configOptions */
Expand Down Expand Up @@ -116,15 +116,15 @@ public function index(Request $request)
*/
public function view(Request $request, $id)
{
$meta = [];
$data = ($request->input(ModelsInterface::PARAM_DATA) === null) ? ModelsInterface::DEFAULT_DATA
$meta = [];
$data = ($request->input(ModelsInterface::PARAM_DATA) === null) ? ModelsInterface::DEFAULT_DATA
: json_decode(urldecode($request->input(ModelsInterface::PARAM_DATA)), true);
$sqlOptions = $this->setSqlOptions($request);
$sqlOptions->setId($id);
$sqlOptions->setData($data);
if (true === $this->isTree) {
$tree = $this->getSubTreeEntities($sqlOptions, $id);
$meta = [strtolower($this->entity) . PhpInterface::UNDERSCORE . JSONApiInterface::META_TREE => $tree];
$tree = $this->getSubTreeEntities($sqlOptions, $id);
$meta = [strtolower($this->entity) . PhpInterface::UNDERSCORE . JSONApiInterface::META_TREE => $tree];
}

if ($this->configOptions->isCached()) {
Expand All @@ -143,6 +143,7 @@ public function view(Request $request, $id)
* POST Creates one entry specified by all input fields in $request
*
* @param Request $request
* @throws \LogicException
* @throws \rjapi\exceptions\AttributesException
*/
public function create(Request $request)
Expand Down
84 changes: 71 additions & 13 deletions src/extension/BaseController.php
Expand Up @@ -3,35 +3,96 @@
namespace rjapi\extension;

use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\DB;
use League\Fractal\Resource\Collection;
use rjapi\exceptions\HeadersException;
use rjapi\helpers\Json;
use rjapi\helpers\Request as RequestHelper;

class BaseController extends ApiController
{

/**
* BaseController constructor.
* @param Route $route
* @throws HeadersException
*/
public function __construct(Route $route)
{
parent::__construct($route);
if (in_array($route->getActionMethod(), self::AVAILABLE_BULKS, true) && RequestHelper::isExt(request(), self::EXT_BULK) === false) {
throw new HeadersException('There is no ' . self::EXT_BULK . ' value in ' . self::EXT . ' key of ' . self::CONTENT_TYPE_KEY . ' header');
}
}

/**
* Creates bulk of items in transaction mode
*
* @param Request $request
* @throws \LogicException
* @throws \rjapi\exceptions\AttributesException
*/
public function create(Request $request)
public function createBulk(Request $request)
{
if (RequestHelper::isExt($request, self::EXT_BULK)) {
// todo: impl create bulk
} else {
parent::create($request);
$meta = [];
$json = Json::decode($request->getContent());
$jsonApiAttributes = Json::getBulkAttributes($json);

try {
$collection = new Collection();
DB::beginTransaction();
foreach ($jsonApiAttributes as $jsonObject) {
// FSM initial state check
if ($this->configOptions->isStateMachine() === true) {
$this->checkFsmCreate($jsonObject);
}
// spell check
if ($this->configOptions->isSpellCheck() === true) {
$meta[] = $this->spellCheck($jsonObject);
}
// fill in model
foreach ($this->props as $k => $v) {
// request fields should match Middleware fields
if (isset($jsonObject[$k])) {
$this->model->$k = $jsonObject[$k];
}
}
// set bit mask
if (true === $this->configOptions->isBitMask()) {
$this->setMaskCreate($jsonObject);
}

$collection->setData($this->model);
$this->model->save();
// jwt
if ($this->configOptions->getIsJwtAction() === true) {
$this->createJwtUser(); // !!! model is overridden
}
// set bit mask from model -> response
if (true === $this->configOptions->isBitMask()) {
$this->model = $this->setFlagsCreate();
}
}
DB::commit();
} catch (\PDOException $e) {
echo $e->getTraceAsString();
DB::rollBack();
}

$this->setRelationships($json, $this->model->id);
$resource = Json::getResource($this->middleWare, $collection, $this->entity, false, $meta);
Json::outputSerializedData($resource, JSONApiInterface::HTTP_RESPONSE_CODE_CREATED);
}

/**
* @param Request $request
* @throws HeadersException
* @throws \rjapi\exceptions\AttributesException
*/
public function updateBulk(Request $request)
{
if (RequestHelper::isExt($request, self::EXT_BULK) === false) {
throw new HeadersException('There is no ' . self::EXT_BULK . ' value in ' . self::SUPPORTED_EXT . ' key of ' . self::CONTENT_TYPE_KEY);
}
// todo: impl update bulk

}

/**
Expand All @@ -40,9 +101,6 @@ public function updateBulk(Request $request)
*/
public function deleteBulk(Request $request)
{
if (RequestHelper::isExt($request, self::EXT_BULK) === false) {
throw new HeadersException('There is no ' . self::EXT_BULK . ' value in ' . self::SUPPORTED_EXT . ' key of ' . self::CONTENT_TYPE_KEY);
}
// todo: impl delete bulk

}
}
2 changes: 1 addition & 1 deletion src/extension/BaseRelationsTrait.php
Expand Up @@ -148,7 +148,7 @@ public function deleteRelations(Request $request, $id, string $relation) : void
* @param int|string $eId
* @param bool $isRemovable
*/
private function setRelationships(array $json, $eId, bool $isRemovable = false) : void
protected function setRelationships(array $json, $eId, bool $isRemovable = false) : void
{
$jsonApiRels = Json::getRelationships($json);
if (empty($jsonApiRels) === false) {
Expand Down
4 changes: 2 additions & 2 deletions src/extension/FsmTrait.php
Expand Up @@ -18,7 +18,7 @@ trait FsmTrait
* @param array $jsonProps JSON input properties
* @throws \rjapi\exceptions\AttributesException
*/
private function checkFsmCreate(array &$jsonProps) : void
protected function checkFsmCreate(array &$jsonProps) : void
{
$stateMachine = new StateMachine($this->entity);
$stateField = $stateMachine->getField();
Expand Down Expand Up @@ -51,7 +51,7 @@ private function checkFsmCreate(array &$jsonProps) : void
* @param $model
* @throws \rjapi\exceptions\AttributesException
*/
private function checkFsmUpdate(array $jsonProps, $model) : void
protected function checkFsmUpdate(array $jsonProps, $model) : void
{
$stateMachine = new StateMachine($this->entity);
$field = $stateMachine->getField();
Expand Down
8 changes: 8 additions & 0 deletions src/extension/JSONApiInterface.php
@@ -1,12 +1,20 @@
<?php

namespace rjapi\extension;

use Illuminate\Http\Request;

interface JSONApiInterface
{
public const AVAILABLE_BULKS = [
'createBulk',
'updateBulk',
'deleteBulk',
];

public const CONTENT_TYPE_KEY = 'Content-Type';
public const SUPPORTED_EXT = 'supported-ext';
public const EXT = 'ext';
public const EXT_BULK = 'bulk';

public const HEADER_CONTENT_TYPE = 'Content-Type: ';
Expand Down
2 changes: 1 addition & 1 deletion src/extension/JWTTrait.php
Expand Up @@ -17,7 +17,7 @@ trait JWTTrait
/**
* Creates new user with JWT + password hashed
*/
private function createJwtUser()
protected function createJwtUser()
{
if(empty($this->model->password)) {
Json::outputErrors(
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/Json.php
Expand Up @@ -28,6 +28,17 @@ public static function getAttributes(array $jsonApiArr) : array
return empty($jsonApiArr[RamlInterface::RAML_DATA][RamlInterface::RAML_ATTRS]) ? [] : $jsonApiArr[RamlInterface::RAML_DATA][RamlInterface::RAML_ATTRS];
}

/**
* Returns an array of bulk attributes for each element
*
* @param array $jsonApiArr
* @return array
*/
public static function getBulkAttributes(array $jsonApiArr) : array
{
return empty($jsonApiArr[RamlInterface::RAML_DATA]) ? [] : $jsonApiArr[RamlInterface::RAML_DATA];
}

/**
* @param array $jsonApiArr
*
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/Request.php
Expand Up @@ -15,6 +15,10 @@ class Request
public static function getSupportedExtensions(Req $request) : array
{
$contentType = $request->header(JSONApiInterface::CONTENT_TYPE_KEY);
if (mb_strpos($contentType, JSONApiInterface::EXT) === false) {
return [];
}

[$i, $ext] = explode(';', $contentType);
[$j, $supportedExt] = explode('=', $ext);

Expand Down

0 comments on commit f2c2c9c

Please sign in to comment.