2018-06-24 01:33:06 -05:00
|
|
|
<?php
|
2021-03-06 00:20:49 -06:00
|
|
|
/*
|
|
|
|
* ShowController.php
|
|
|
|
* Copyright (c) 2021 james@firefly-iii.org
|
2018-06-24 01:33:06 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-06-24 01:33:06 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2018-06-24 01:33:06 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-06-24 01:33:06 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-06-24 01:33:06 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-06-24 01:33:06 -05:00
|
|
|
*/
|
|
|
|
|
2021-03-28 04:39:26 -05:00
|
|
|
declare(strict_types=1);
|
2018-06-24 01:33:06 -05:00
|
|
|
|
2021-03-28 04:39:26 -05:00
|
|
|
namespace FireflyIII\Api\V1\Controllers\Models\AvailableBudget;
|
2018-06-24 01:33:06 -05:00
|
|
|
|
2021-03-06 00:20:49 -06:00
|
|
|
use FireflyIII\Api\V1\Controllers\Controller;
|
2018-06-24 01:33:06 -05:00
|
|
|
use FireflyIII\Models\AvailableBudget;
|
2019-08-30 00:45:48 -05:00
|
|
|
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
|
2018-06-24 01:33:06 -05:00
|
|
|
use FireflyIII\Transformers\AvailableBudgetTransformer;
|
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
use League\Fractal\Resource\Collection as FractalCollection;
|
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
|
|
|
|
/**
|
2021-03-06 00:20:49 -06:00
|
|
|
* Class ShowController
|
2018-06-24 01:33:06 -05:00
|
|
|
*/
|
2021-03-06 00:20:49 -06:00
|
|
|
class ShowController extends Controller
|
2018-06-24 01:33:06 -05:00
|
|
|
{
|
2020-08-02 05:13:23 -05:00
|
|
|
private AvailableBudgetRepositoryInterface $abRepository;
|
2020-07-31 02:24:08 -05:00
|
|
|
|
2018-06-24 01:33:06 -05:00
|
|
|
/**
|
2019-04-11 21:53:18 -05:00
|
|
|
* AvailableBudgetController constructor.
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2018-06-24 01:33:06 -05:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
/** @var User $user */
|
2019-08-30 00:45:48 -05:00
|
|
|
$user = auth()->user();
|
|
|
|
$this->abRepository = app(AvailableBudgetRepositoryInterface::class);
|
|
|
|
$this->abRepository->setUser($user);
|
2018-06-24 01:33:06 -05:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
2019-04-11 21:53:18 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-06-24 01:33:06 -05:00
|
|
|
*/
|
2019-09-04 10:39:39 -05:00
|
|
|
public function index(): JsonResponse
|
2018-06-24 01:33:06 -05:00
|
|
|
{
|
2019-09-04 10:39:39 -05:00
|
|
|
$manager = $this->getManager();
|
2018-06-24 01:33:06 -05:00
|
|
|
|
|
|
|
// types to get, page size:
|
2021-03-21 03:15:40 -05:00
|
|
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
2018-06-24 01:33:06 -05:00
|
|
|
|
2019-01-26 05:09:16 -06:00
|
|
|
$start = $this->parameters->get('start');
|
2019-02-13 10:38:41 -06:00
|
|
|
$end = $this->parameters->get('end');
|
2019-01-26 05:09:16 -06:00
|
|
|
|
2019-06-04 13:42:11 -05:00
|
|
|
// get list of available budgets. Count it and split it.
|
2019-08-30 01:03:13 -05:00
|
|
|
$collection = $this->abRepository->getAvailableBudgetsByDate($start, $end);
|
2018-06-24 01:33:06 -05:00
|
|
|
$count = $collection->count();
|
|
|
|
$availableBudgets = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
|
|
|
|
|
|
|
|
// make paginator:
|
|
|
|
$paginator = new LengthAwarePaginator($availableBudgets, $count, $pageSize, $this->parameters->get('page'));
|
|
|
|
$paginator->setPath(route('api.v1.available_budgets.index') . $this->buildParams());
|
|
|
|
|
2018-12-15 15:03:05 -06:00
|
|
|
/** @var AvailableBudgetTransformer $transformer */
|
|
|
|
$transformer = app(AvailableBudgetTransformer::class);
|
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
|
|
|
|
$resource = new FractalCollection($availableBudgets, $transformer, 'available_budgets');
|
2018-06-24 01:33:06 -05:00
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
|
|
|
|
2020-10-03 09:51:44 -05:00
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
2018-06-24 01:33:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
2019-04-11 21:53:18 -05:00
|
|
|
* @param AvailableBudget $availableBudget
|
2018-06-24 01:33:06 -05:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
2019-05-30 05:39:06 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-06-24 01:33:06 -05:00
|
|
|
*/
|
2019-09-04 10:39:39 -05:00
|
|
|
public function show(AvailableBudget $availableBudget): JsonResponse
|
2018-06-24 01:33:06 -05:00
|
|
|
{
|
2019-09-04 10:39:39 -05:00
|
|
|
$manager = $this->getManager();
|
2018-12-15 15:03:05 -06:00
|
|
|
|
|
|
|
/** @var AvailableBudgetTransformer $transformer */
|
|
|
|
$transformer = app(AvailableBudgetTransformer::class);
|
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
|
|
|
|
$resource = new Item($availableBudget, $transformer, 'available_budgets');
|
2018-06-24 01:33:06 -05:00
|
|
|
|
2020-10-03 09:51:44 -05:00
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
2018-06-24 01:33:06 -05:00
|
|
|
}
|
|
|
|
|
2021-03-28 04:39:26 -05:00
|
|
|
}
|