Skip to content

Firefly III: a personal finances manager

First basic routes and code for bills.

James Colef488bbde025c
diff --git a/app/Api/V1/Controllers/BillController.php b/app/Api/V1/Controllers/BillController.php
index a0f793353e..2b43bee73c 100644
--- a/app/Api/V1/Controllers/BillController.php
+++ b/app/Api/V1/Controllers/BillController.php
@@ -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());
}
/**
diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php
new file mode 100644
index 0000000000..e5cf812658
--- /dev/null
+++ b/app/Transformers/BillTransformer.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * BillTransformer.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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,
+ ],
+ ],
+ ];
+ }
+}
\ No newline at end of file
diff --git a/routes/api.php b/routes/api.php
index 61d0d30274..5828119d12 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -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;
+//}
+//);
\ No newline at end of file