From 462a74cbf1cfb9c34cc2ca592332d53512fb8190 Mon Sep 17 00:00:00 2001 From: Dean Blackborough Date: Mon, 12 Nov 2018 00:19:22 +0000 Subject: [PATCH 1/3] Added expanded summary controller * Added expanded summary controller. --- .../Controllers/ExpandedSummaryController.php | 67 +++++++++++++++++++ config/api/descriptions.php | 1 + .../api/convert-route-params_log-requests.php | 8 +++ 3 files changed, 76 insertions(+) create mode 100644 app/Http/Controllers/ExpandedSummaryController.php diff --git a/app/Http/Controllers/ExpandedSummaryController.php b/app/Http/Controllers/ExpandedSummaryController.php new file mode 100644 index 00000000..ce55cf3c --- /dev/null +++ b/app/Http/Controllers/ExpandedSummaryController.php @@ -0,0 +1,67 @@ + + * @copyright Dean Blackborough 2018 + * @license https://github.com/costs-to-expect/api/blob/master/LICENSE + */ +class ExpandedSummaryController extends Controller +{ + /** + * Return the TCO for the resource + * + * @param Request $request + * @param string $resource_type_id + * @param string $resource_id + * + * @return JsonResponse + */ + public function categories( + Request $request, + string $resource_type_id, + string $resource_id + ): JsonResponse { + Validate::resourceRoute($resource_type_id, $resource_id); + + return response()->json( + [ + + ], + 200 + ); + } + + /** + * Generate the OPTIONS request for the TCO + * + * @param Request $request + * @param string $resource_type_id + * @param string $resource_id + */ + public function optionsCategories( + Request $request, + string $resource_type_id, + string $resource_id + ) { + Validate::resourceRoute($resource_type_id, $resource_id); + + $routes = [ + 'GET' => [ + 'description' => Config::get('api.descriptions.summary.GET_expanded_categories'), + 'authenticated' => false, + 'parameters' => [] + ] + ]; + + $this->optionsResponse($routes); + } +} diff --git a/config/api/descriptions.php b/config/api/descriptions.php index 8cbdc22f..66e69429 100644 --- a/config/api/descriptions.php +++ b/config/api/descriptions.php @@ -57,6 +57,7 @@ 'summary' => [ 'GET_tco' => 'Total cost of ownership TCO for a resource', 'GET_categories' => 'Return the categories summary for a resource', + 'GET_expanded_categories' => 'Return the expanded categories summary for a resource', 'GET_category' => 'Return the category summary for a resource', 'GET_sub_categories' => 'Return the category sub categories summary for a resource', 'GET_sub_category' => 'Return the category sub category summary for a resource', diff --git a/routes/api/convert-route-params_log-requests.php b/routes/api/convert-route-params_log-requests.php index e3a0f51d..6b581501 100644 --- a/routes/api/convert-route-params_log-requests.php +++ b/routes/api/convert-route-params_log-requests.php @@ -107,6 +107,14 @@ function () { ); // Summary end points + Route::get( + 'resource_types/{resource_type_id}/resources/{resource_id}/expanded_summary/categories', + 'ExpandedSummaryController@categories' + ); + Route::options( + 'resource_types/{resource_type_id}/resources/{resource_id}/expanded_summary/categories', + 'ExpandedSummaryController@optionsCategories' + ); Route::get( 'resource_types/{resource_type_id}/resources/{resource_id}/summary/categories', 'SummaryController@categories' From 917b27f87d6d99047d9c9d3dce0bc9a630dc75f1 Mon Sep 17 00:00:00 2001 From: Dean Blackborough Date: Mon, 12 Nov 2018 23:21:10 +0000 Subject: [PATCH 2/3] Added expanded categories summary. * Added expanded categories summary. * Updated README with new route. --- README.md | 2 + .../Controllers/ExpandedSummaryController.php | 16 ++++++-- app/Models/Item.php | 22 +++++++++++ .../CategorySubCategorySummary.php | 39 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 app/Transformers/CategorySubCategorySummary.php diff --git a/README.md b/README.md index 9de21cd4..65433111 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,8 @@ X-Link-Previous and X-Link-Next can be null. | HTTP Verb(s) | Route | | :--- | :--- | +| GET/HEAD | v1/resource_types/{resource_type_id}/resources/{resource_id}/expanded-summary/categories | +| OPTIONS | v1/resource_types/{resource_type_id}/resources/{resource_id}/expanded-summary/categories | | GET/HEAD | v1/resource_types/{resource_type_id}/resources/{resource_id}/summary/tco | | OPTIONS | v1/resource_types/{resource_type_id}/resources/{resource_id}/summary/tco | | GET/HEAD | v1/resource_types/{resource_type_id}/resources/{resource_id}/summary/categories | diff --git a/app/Http/Controllers/ExpandedSummaryController.php b/app/Http/Controllers/ExpandedSummaryController.php index ce55cf3c..3f8ff557 100644 --- a/app/Http/Controllers/ExpandedSummaryController.php +++ b/app/Http/Controllers/ExpandedSummaryController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers; use App\Http\Parameters\Route\Validate; +use App\Models\Item; +use App\Transformers\CategorySubCategorySummary as CategorySubCategorySummaryTransformer; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Config; @@ -30,12 +32,20 @@ public function categories( string $resource_type_id, string $resource_id ): JsonResponse { + Validate::resourceRoute($resource_type_id, $resource_id); - return response()->json( - [ + $summary = (new Item())->expandedCategoriesSummary( + $resource_type_id, + $resource_id + ); - ], + return response()->json( + $summary->map( + function ($summary_row) { + return (new CategorySubCategorySummaryTransformer($summary_row))->toArray(); + } + ), 200 ); } diff --git a/app/Models/Item.php b/app/Models/Item.php index 4eb7758a..18c3274b 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -256,4 +256,26 @@ public function monthSummary(int $resource_type_id, int $resource_id, int $year, orderBy("month")-> get(); } + + public function expandedCategoriesSummary(int $resource_type_id, int $resource_id) + { + return $this-> + selectRaw("`category`.`name` AS `category`")-> + selectRaw("`sub_category`.`name` AS `sub_category`")-> + selectRaw("SUM(`item`.`actualised_total`) AS `actualised_total`")-> + selectRaw("COUNT(`item`.`id`) AS `items`")-> + join("item_category", "item_category.item_id", "item.id")-> + join("item_sub_category", "item_sub_category.item_category_id", "item_category.id")-> + join("category", "item_category.category_id", "category.id")-> + join("sub_category", "item_sub_category.sub_category_id", "sub_category.id")-> + join("resource", "item.resource_id", "resource.id")-> + join("resource_type", "resource.resource_type_id", "resource_type.id")-> + where('resource_type.id', '=', $resource_type_id)-> + where('resource.id', '=', $resource_id)-> + groupBy("sub_category.name")-> + groupBy("category.name")-> + orderBy("category.name")-> + orderBy("sub_category.name")-> + get(); + } } diff --git a/app/Transformers/CategorySubCategorySummary.php b/app/Transformers/CategorySubCategorySummary.php new file mode 100644 index 00000000..0d1274c5 --- /dev/null +++ b/app/Transformers/CategorySubCategorySummary.php @@ -0,0 +1,39 @@ + + * @copyright Dean Blackborough 2018 + * @license https://github.com/costs-to-expect/api/blob/master/LICENSE + */ +class CategorySubCategorySummary extends Transformer +{ + private $summary; + + /** + * CategorySubCategorySummary constructor. + * + * @param ItemModel $summary + */ + public function __construct(ItemModel $summary) + { + parent::__construct(); + + $this->summary = $summary; + } + + public function toArray(): array + { + return [ + 'category' => $this->summary->category, + 'sub_category' => $this->summary->sub_category, + 'total' => number_format($this->summary->actualised_total, 2, '.', ''), + 'items' => $this->summary->items + ]; + } +} From 1f1fa4750298c6fc1abfbf432c29c8a8f51c1740 Mon Sep 17 00:00:00 2001 From: Dean Blackborough Date: Mon, 12 Nov 2018 23:27:58 +0000 Subject: [PATCH 3/3] New release v1.09.0 * New release v1.09.0 --- CHANGELOG.md | 4 ++++ config/api/version.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d74b29b..49615320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Full changelog for the Costs to Expect REST API. +## 2018-11-12 - v1.09.0 + +* Added an expanded-summary categories route. + ## 2018-11-10 - v1.08.0 * Initial support for PATCHing, to start /resource_types/{resource_type_id}/resources/{resource_id}/items/{item_id}. diff --git a/config/api/version.php b/config/api/version.php index cf7ab95e..268aae4b 100644 --- a/config/api/version.php +++ b/config/api/version.php @@ -1,9 +1,9 @@ '1.08.0', + 'version'=> '1.09.0', 'prefix' => 'v1', - 'release_date' => '2018-11-10', + 'release_date' => '2018-11-12', 'changelog' => [ 'api' => '/v1/changelog', 'markdown' => 'https://github.com/costs-to-expect/api/blob/master/CHANGELOG.md'