-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from deanblackborough/summary-expanded-route
Summary expanded route
- Loading branch information
Showing
8 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters