Files
firefly-iii/app/Api/V1/Controllers/BillController.php

164 lines
5.1 KiB
PHP
Raw Normal View History

2018-02-04 13:41:42 +01:00
<?php
declare(strict_types=1);
2018-02-04 13:41:42 +01:00
/**
* BillController.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/>.
*/
namespace FireflyIII\Api\V1\Controllers;
2018-02-10 09:57:31 +01:00
use FireflyIII\Api\V1\Requests\BillRequest;
2018-02-04 13:41:42 +01:00
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Transformers\BillTransformer;
2018-02-04 13:41:42 +01:00
use Illuminate\Http\Request;
2018-02-06 07:49:56 +01:00
use Illuminate\Support\Collection;
2018-02-04 15:57:35 +01:00
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
2018-02-04 15:57:35 +01:00
use League\Fractal\Serializer\JsonApiSerializer;
use Preferences;
2018-02-04 13:41:42 +01:00
/**
* Class BillController
*/
class BillController extends Controller
{
/** @var BillRepositoryInterface */
private $repository;
/**
* BillController constructor.
*
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function __construct()
{
parent::__construct();
2018-02-09 14:47:37 +01:00
$this->middleware(
function ($request, $next) {
/** @var BillRepositoryInterface repository */
$this->repository = app(BillRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
2018-02-04 13:41:42 +01:00
/**
* Remove the specified resource from storage.
*
* @param \FireflyIII\Models\Bill $bill
*
* @return \Illuminate\Http\Response
*/
2018-02-10 09:57:31 +01:00
public function delete(Bill $bill)
2018-02-04 13:41:42 +01:00
{
$this->repository->destroy($bill);
2018-02-09 19:11:55 +01:00
2018-02-10 09:57:31 +01:00
return response()->json([], 204);
2018-02-04 13:41:42 +01:00
}
/**
* Display a listing of the resource.
*
* @param Request $request
*
2018-02-04 15:57:35 +01:00
* @return \Illuminate\Http\JsonResponse
2018-02-04 13:41:42 +01:00
*/
2018-02-04 15:57:35 +01:00
public function index(Request $request)
2018-02-04 13:41:42 +01:00
{
2018-04-02 14:17:11 +02:00
$pageSize = (int)Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data;
$paginator = $this->repository->getPaginator($pageSize);
2018-02-06 07:49:56 +01:00
/** @var Collection $bills */
$bills = $paginator->getCollection();
2018-02-04 15:57:35 +01:00
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new FractalCollection($bills, new BillTransformer($this->parameters), 'bills');
2018-02-04 15:57:35 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2018-02-18 10:31:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-04 13:41:42 +01:00
}
2018-02-09 14:47:37 +01:00
2018-02-04 13:41:42 +01:00
/**
2018-02-09 14:47:37 +01:00
* @param Request $request
* @param Bill $bill
2018-02-04 13:41:42 +01:00
*
* @return \Illuminate\Http\JsonResponse
2018-02-04 13:41:42 +01:00
*/
public function show(Request $request, Bill $bill)
2018-02-04 13:41:42 +01:00
{
$manager = new Manager();
2018-02-13 21:04:15 +01:00
// add include parameter:
$include = $request->get('include') ?? '';
$manager->parseIncludes($include);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
2018-02-18 10:31:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-04 13:41:42 +01:00
}
/**
* @param BillRequest $request
2018-02-04 13:41:42 +01:00
*
* @return \Illuminate\Http\JsonResponse
2018-02-04 13:41:42 +01:00
*/
2018-02-10 09:57:31 +01:00
public function store(BillRequest $request)
2018-02-04 13:41:42 +01:00
{
2018-02-11 07:46:34 +01:00
$bill = $this->repository->store($request->getAll());
2018-02-10 09:57:31 +01:00
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
2018-02-10 09:57:31 +01:00
2018-02-18 10:31:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-10 09:57:31 +01:00
2018-02-04 13:41:42 +01:00
}
2018-02-11 07:46:34 +01:00
2018-02-04 13:41:42 +01:00
/**
* @param BillRequest $request
* @param Bill $bill
2018-02-04 13:41:42 +01:00
*
* @return \Illuminate\Http\JsonResponse
2018-02-04 13:41:42 +01:00
*/
2018-02-10 09:57:31 +01:00
public function update(BillRequest $request, Bill $bill)
2018-02-04 13:41:42 +01:00
{
$data = $request->getAll();
$bill = $this->repository->update($bill, $data);
2018-02-10 09:57:31 +01:00
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
2018-02-10 09:57:31 +01:00
2018-02-18 10:31:15 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2018-02-10 09:57:31 +01:00
2018-02-04 13:41:42 +01:00
}
}