Skip to content

Commit

Permalink
Merge pull request #61 from deanblackborough/summary-expanded-route
Browse files Browse the repository at this point in the history
Summary expanded route
  • Loading branch information
deanblackborough authored Nov 12, 2018
2 parents 366181c + 1f1fa47 commit 0a4c053
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
77 changes: 77 additions & 0 deletions app/Http/Controllers/ExpandedSummaryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

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;

/**
* Expanded summaries
*
* @author Dean Blackborough <[email protected]>
* @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);

$summary = (new Item())->expandedCategoriesSummary(
$resource_type_id,
$resource_id
);

return response()->json(
$summary->map(
function ($summary_row) {
return (new CategorySubCategorySummaryTransformer($summary_row))->toArray();
}
),
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);
}
}
22 changes: 22 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
39 changes: 39 additions & 0 deletions app/Transformers/CategorySubCategorySummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Transformers;

use App\Models\Item as ItemModel;

/**
* Transform the data returns from Eloquent into the format we want for the API
*
* @author Dean Blackborough <[email protected]>
* @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
];
}
}
1 change: 1 addition & 0 deletions config/api/descriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions config/api/version.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

return [
'version'=> '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'
Expand Down
8 changes: 8 additions & 0 deletions routes/api/convert-route-params_log-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 0a4c053

Please sign in to comment.