First basic routes and code for bills.

This commit is contained in:
James Cole 2018-02-04 15:57:35 +01:00
parent e668b88fb5
commit f488bbde02
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 105 additions and 8 deletions

View File

@ -22,8 +22,16 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers;
use Auth;
use FireflyIII\Models\Bill;
use FireflyIII\Transformers\BillTransformer;
use Illuminate\Http\Request;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Serializer\JsonApiSerializer;
use Preferences;
use Response;
/**
* Class BillController
@ -45,11 +53,25 @@ class BillController extends Controller
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function index()
public function index(Request $request)
{
//
$user = Auth::guard('api')->user();
$pageSize = intval(Preferences::getForUser($user, 'listPageSize', 50)->data);
$paginator = $user->bills()->paginate($pageSize);
$bills = $paginator->getCollection();
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new FractalCollection($bills, new BillTransformer(), 'bills');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return Response::json($manager->createData($resource)->toArray());
}
/**

View File

@ -0,0 +1,61 @@
<?php
/**
* BillTransformer.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\Transformers;
use FireflyIII\Models\Bill;
use League\Fractal\TransformerAbstract;
/**
* Class BillTransformer
*/
class BillTransformer extends TransformerAbstract
{
/**
* @param Bill $bill
*
* @return array
*/
public function transform(Bill $bill): array
{
return [
'id' => (int)$bill->id,
'name' => $bill->name,
'match' => explode(',', $bill->match),
'amount_min' => round($bill->amount_min, 2),
'amount_max' => round($bill->amount_max, 2),
'date' => $bill->date->format('Y-m-d'),
'repeat_freq' => $bill->repeat_freq,
'skip' => (int)$bill->skip,
'automatch' => intval($bill->automatch) === 1,
'active' => intval($bill->active) === 1,
'links' => [
[
'rel' => 'self',
'uri' => '/bills/' . $bill->id,
],
],
];
}
}

View File

@ -19,8 +19,6 @@
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
@ -32,8 +30,24 @@ use Illuminate\Http\Request;
|
*/
Route::middleware('auth:api')->get(
'/user', function (Request $request) {
return $request->user();
//Route::get(
// '/user', function (Request $request) {
// return 'hello';
// return $request->user();
//}
//);
Route::group(
['middleware' => 'auth:api', 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'bills', 'as' => 'api.v1.bills.'], function () {
Route::get('', ['uses' => 'BillController@index', 'as' => 'index']);
Route::get('{bill}', ['uses' => 'BillController@show', 'as' => 'show']);
}
);
//Route::get(
// '/bills', function (Request $request) {
// //return 'hello';
// return $request->user()->bills;
//}
//);