Skip to content

Commit

Permalink
Merge pull request #194 from deanblackborough/v2.11.6
Browse files Browse the repository at this point in the history
v2.11.6
  • Loading branch information
deanblackborough authored Jul 1, 2020
2 parents 517b709 + 95cb216 commit 26de9d1
Show file tree
Hide file tree
Showing 107 changed files with 3,039 additions and 3,235 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

The complete changelog for the Costs to Expect REST API, our changelog follows the format defined at https://keepachangelog.com/en/1.0.0/

## [v2.11.6] - 2020-07-01
### Added
- We have added support for an `X-Skip-Cache` request header; If you include the header with your request we will skip response caching and fetch live values, please use this with care.

### Changed
- We have added separate links for the documentation and example page and the postman collection.
- We have simplified our `\Model\Transformer` classes and made it possible to alter the returned data format.
- We have added `public` as a sorting option for resource types.
- We have reworked our pagination class; we have moved it to a new workspace and also improved how it works.
- We have moved our `Hash` class; the `Hash` class now lives in the `Request` namespace.
- We have moved our `ModelUtility` class: the `ModelUtility` class now lives in the `Models` namespace.
- We have updated the indexes in our `Hash` request class; the indexes are consistent with the rest of the app.

### Fixed
- We have updated our pagination helper to include any defined filtering parameters.
- We have corrected pagination calls in all our controllers; we now include all possible request parameters.
- We have corrected calls to clear public caches; we were comparing different types.

## [v2.11.5] - 2020-06-24
### Added
- We have added a documentation page; the documentation page links to the API documentation and includes a couple of examples.
Expand Down
180 changes: 180 additions & 0 deletions app/Http/Controllers/CategoryManage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

namespace App\Http\Controllers;

Use App\Response\Cache;
use App\Request\Route;
use App\Models\Category;
use App\Models\Transformers\Category as CategoryTransformer;
use App\Request\Validate\Category as CategoryValidator;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

/**
* @author Dean Blackborough <[email protected]>
* @copyright Dean Blackborough 2018-2020
* @license https://github.com/costs-to-expect/api/blob/master/LICENSE
*/
class CategoryManage extends Controller
{
/**
* Create a new category
*
* @param $resource_type_id
*
* @return JsonResponse
*/
public function create($resource_type_id): JsonResponse
{
Route\Validate::resourceType(
(int) $resource_type_id,
$this->permitted_resource_types
);

$cache_control = new Cache\Control(Auth::user()->id);
$cache_key = new Cache\Key();

$validator = (new CategoryValidator)->create([
'resource_type_id' => $resource_type_id
]);
\App\Request\BodyValidation::validateAndReturnErrors($validator);

try {
$category = new Category([
'name' => request()->input('name'),
'description' => request()->input('description'),
'resource_type_id' => $resource_type_id
]);
$category->save();

$cache_control->clearPrivateCacheKeys([
$cache_key->categories($resource_type_id)
]);

if (in_array((int) $resource_type_id, $this->public_resource_types, true)) {
$cache_control->clearPublicCacheKeys([
$cache_key->categories($resource_type_id)
]);
}
} catch (Exception $e) {
\App\Response\Responses::failedToSaveModelForCreate();
}

return response()->json(
(new CategoryTransformer((new Category)->instanceToArray($category)))->asArray(),
201
);
}

/**
* Delete the requested category
*
* @param $resource_type_id
* @param $category_id
*
* @return JsonResponse
*/
public function delete(
$resource_type_id,
$category_id
): JsonResponse
{
Route\Validate::category(
(int) $resource_type_id,
(int) $category_id,
$this->permitted_resource_types,
true
);

$cache_control = new Cache\Control(Auth::user()->id);
$cache_key = new Cache\Key();

try {
(new Category())->find($category_id)->delete();
$cache_control->clearPrivateCacheKeys([
$cache_key->categories($resource_type_id)
]);

if (in_array((int) $resource_type_id, $this->public_resource_types, true)) {
$cache_control->clearPublicCacheKeys([
$cache_key->categories($resource_type_id)
]);
}

\App\Response\Responses::successNoContent();
} catch (QueryException $e) {
\App\Response\Responses::foreignKeyConstraintError();
} catch (Exception $e) {
\App\Response\Responses::notFound(trans('entities.category'), $e);
}
}

/**
* Update the selected category
*
* @param $resource_type_id
* @param $category_id
*
* @return JsonResponse
*/
public function update($resource_type_id, $category_id): JsonResponse
{
Route\Validate::category(
(int) $resource_type_id,
(int) $category_id,
$this->permitted_resource_types,
true
);

$cache_control = new Cache\Control(Auth::user()->id);
$cache_key = new Cache\Key();

$category = (new Category())->instance($category_id);

if ($category === null) {
\App\Response\Responses::failedToSelectModelForUpdateOrDelete();
}

\App\Request\BodyValidation::checkForEmptyPatch();

$validator = (new CategoryValidator)->update([
'resource_type_id' => (int)$category->resource_type_id,
'category_id' => (int)$category_id
]);
\App\Request\BodyValidation::validateAndReturnErrors($validator);

\App\Request\BodyValidation::checkForInvalidFields(
array_merge(
(new Category())->patchableFields(),
(new CategoryValidator)->dynamicDefinedFields()
)
);

foreach (request()->all() as $key => $value) {
$category->$key = $value;
}

try {
$category->save();

$cache_control->clearPrivateCacheKeys([
// We need to clear categories, resource type items
// and items due to includes so simpler to clear the entire
// resource type
$cache_key->resourceType($resource_type_id)
]);

if (in_array((int) $resource_type_id, $this->public_resource_types, true)) {
$cache_control->clearPublicCacheKeys([
$cache_key->resourceType($resource_type_id)
]);
}
} catch (Exception $e) {
\App\Response\Responses::failedToSaveModelForUpdate();
}

\App\Response\Responses::successNoContent();
}
}
Loading

0 comments on commit 26de9d1

Please sign in to comment.