2018-06-28 15:15:22 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RecurrenceController.php
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Api\V1\Controllers;
|
|
|
|
|
2018-06-29 12:27:07 -05:00
|
|
|
use FireflyIII\Api\V1\Requests\RecurrenceRequest;
|
|
|
|
use FireflyIII\Models\Recurrence;
|
2018-06-29 07:07:33 -05:00
|
|
|
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
|
|
|
|
use FireflyIII\Transformers\RecurrenceTransformer;
|
2018-06-28 15:15:22 -05:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
2018-06-29 07:07:33 -05:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
use League\Fractal\Manager;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
use League\Fractal\Resource\Collection as FractalCollection;
|
2018-06-29 12:27:07 -05:00
|
|
|
use League\Fractal\Resource\Item;
|
2018-06-29 07:07:33 -05:00
|
|
|
use League\Fractal\Serializer\JsonApiSerializer;
|
2018-06-28 15:15:22 -05:00
|
|
|
|
2018-06-29 07:07:33 -05:00
|
|
|
/**
|
|
|
|
* Class RecurrenceController
|
|
|
|
*/
|
|
|
|
class RecurrenceController extends Controller
|
2018-06-28 15:15:22 -05:00
|
|
|
{
|
2018-07-06 00:15:42 -05:00
|
|
|
/** @var RecurringRepositoryInterface The recurring transaction repository */
|
2018-06-29 07:07:33 -05:00
|
|
|
private $repository;
|
|
|
|
|
2018-07-06 00:15:42 -05:00
|
|
|
/**
|
|
|
|
* RecurrenceController constructor.
|
|
|
|
*/
|
2018-06-28 15:15:22 -05:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
2018-06-29 07:07:33 -05:00
|
|
|
/** @var RecurringRepositoryInterface repository */
|
|
|
|
$this->repository = app(RecurringRepositoryInterface::class);
|
|
|
|
$this->repository->setUser($user);
|
|
|
|
|
2018-06-28 15:15:22 -05:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the resource.
|
|
|
|
*
|
2018-07-02 13:02:20 -05:00
|
|
|
* @param Recurrence $recurrence
|
2018-06-28 15:15:22 -05:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2018-06-29 12:27:07 -05:00
|
|
|
public function delete(Recurrence $recurrence): JsonResponse
|
2018-06-28 15:15:22 -05:00
|
|
|
{
|
2018-06-29 12:27:07 -05:00
|
|
|
$this->repository->destroy($recurrence);
|
2018-06-28 15:15:22 -05:00
|
|
|
|
|
|
|
return response()->json([], 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List all of them.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse]
|
|
|
|
*/
|
|
|
|
public function index(Request $request): JsonResponse
|
|
|
|
{
|
2018-06-29 07:07:33 -05:00
|
|
|
// create some objects:
|
|
|
|
$manager = new Manager;
|
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
|
|
|
|
|
|
|
// types to get, page size:
|
|
|
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
|
|
|
|
|
|
|
// get list of budgets. Count it and split it.
|
|
|
|
$collection = $this->repository->getAll();
|
|
|
|
$count = $collection->count();
|
|
|
|
$piggyBanks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
|
|
|
|
|
|
|
|
// make paginator:
|
|
|
|
$paginator = new LengthAwarePaginator($piggyBanks, $count, $pageSize, $this->parameters->get('page'));
|
|
|
|
$paginator->setPath(route('api.v1.recurrences.index') . $this->buildParams());
|
|
|
|
|
|
|
|
// present to user.
|
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
|
|
|
$resource = new FractalCollection($piggyBanks, new RecurrenceTransformer($this->parameters), 'recurrences');
|
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
|
|
|
|
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
2018-06-28 15:15:22 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List single resource.
|
|
|
|
*
|
2018-06-29 12:27:07 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param Recurrence $recurrence
|
2018-06-28 15:15:22 -05:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2018-06-29 12:27:07 -05:00
|
|
|
public function show(Request $request, Recurrence $recurrence): JsonResponse
|
2018-06-28 15:15:22 -05:00
|
|
|
{
|
2018-06-29 12:27:07 -05:00
|
|
|
$manager = new Manager();
|
|
|
|
// add include parameter:
|
|
|
|
$include = $request->get('include') ?? '';
|
|
|
|
$manager->parseIncludes($include);
|
|
|
|
|
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
|
|
|
|
|
|
|
$resource = new Item($recurrence, new RecurrenceTransformer($this->parameters), 'recurrences');
|
|
|
|
|
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
|
|
|
|
2018-06-28 15:15:22 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store new object.
|
|
|
|
*
|
2018-06-29 12:27:07 -05:00
|
|
|
* @param RecurrenceRequest $request
|
2018-06-28 15:15:22 -05:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2018-06-29 12:27:07 -05:00
|
|
|
public function store(RecurrenceRequest $request): JsonResponse
|
2018-06-28 15:15:22 -05:00
|
|
|
{
|
2018-06-29 12:27:07 -05:00
|
|
|
$recurrence = $this->repository->store($request->getAll());
|
|
|
|
$manager = new Manager();
|
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
|
|
|
$resource = new Item($recurrence, new RecurrenceTransformer($this->parameters), 'recurrences');
|
2018-06-28 15:15:22 -05:00
|
|
|
|
2018-06-29 12:27:07 -05:00
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
2018-06-28 15:15:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-06 00:15:42 -05:00
|
|
|
* Update single recurrence.
|
|
|
|
*
|
2018-06-29 23:14:39 -05:00
|
|
|
* @param RecurrenceRequest $request
|
|
|
|
* @param Recurrence $recurrence
|
2018-06-28 15:15:22 -05:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2018-06-29 23:14:39 -05:00
|
|
|
public function update(RecurrenceRequest $request, Recurrence $recurrence): JsonResponse
|
2018-06-28 15:15:22 -05:00
|
|
|
{
|
2018-07-05 11:42:51 -05:00
|
|
|
$data = $request->getAll();
|
2018-06-29 23:14:39 -05:00
|
|
|
$category = $this->repository->update($recurrence, $data);
|
|
|
|
$manager = new Manager();
|
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
|
|
|
|
|
|
|
$resource = new Item($category, new RecurrenceTransformer($this->parameters), 'recurrences');
|
|
|
|
|
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
2018-06-28 15:15:22 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|