Skip to content

Firefly III: a personal finances manager

Consistent use of links in transformers.

James Colef445a95c26ac
diff --git a/app/Api/V1/Controllers/BillController.php b/app/Api/V1/Controllers/BillController.php
index 25b4055e9b..f7a2df3d63 100644
--- a/app/Api/V1/Controllers/BillController.php
+++ b/app/Api/V1/Controllers/BillController.php
@@ -25,12 +25,14 @@ namespace FireflyIII\Api\V1\Controllers;
use Auth;
use Carbon\Carbon;
use FireflyIII\Models\Bill;
+use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Transformers\BillTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
+use League\Fractal\Resource\Item;
use League\Fractal\Serializer\JsonApiSerializer;
use Preferences;
use Response;
@@ -40,6 +42,24 @@ use Response;
*/
class BillController extends Controller
{
+ /** @var BillRepositoryInterface */
+ private $repository;
+
+ /**
+ * BillController constructor.
+ *
+ * @throws \FireflyIII\Exceptions\FireflyException
+ */
+ public function __construct()
+ {
+
+ /** @var BillRepositoryInterface repository */
+ $this->repository = app(BillRepositoryInterface::class);
+ $user = Auth::guard('api')->user();
+ $this->repository->setUser($user);
+ parent::__construct();
+ }
+
/**
* Remove the specified resource from storage.
*
@@ -49,7 +69,8 @@ class BillController extends Controller
*/
public function destroy(Bill $bill)
{
- //
+ $this->repository->destroy($bill);
+ return response()->json(null, 204);
}
/**
@@ -69,7 +90,7 @@ class BillController extends Controller
if (null !== $request->get('end')) {
$end = new Carbon($request->get('end'));
}
- $paginator = $user->bills()->paginate($pageSize);
+ $paginator = $this->repository->getPaginator($pageSize);
/** @var Collection $bills */
$bills = $paginator->getCollection();
@@ -80,7 +101,6 @@ class BillController extends Controller
$resource = new FractalCollection($bills, new BillTransformer($start, $end), 'bills');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
-
return Response::json($manager->createData($resource)->toArray());
}
@@ -89,11 +109,28 @@ class BillController extends Controller
*
* @param \FireflyIII\Models\Bill $bill
*
- * @return \Illuminate\Http\Response
+ * @return \Illuminate\Http\JsonResponse
*/
- public function show(Bill $bill)
+ public function show(Request $request, Bill $bill)
{
- //
+ $start = null;
+ $end = null;
+ if (null !== $request->get('start')) {
+ $start = new Carbon($request->get('start'));
+ }
+ if (null !== $request->get('end')) {
+ $end = new Carbon($request->get('end'));
+ }
+
+
+ $manager = new Manager();
+ $manager->parseIncludes(['attachments']);
+ $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
+ $manager->setSerializer(new JsonApiSerializer($baseUrl));
+
+ $resource = new Item($bill, new BillTransformer($start, $end), 'bill');
+
+ return Response::json($manager->createData($resource)->toArray());
}
/**
diff --git a/app/Transformers/AttachmentTransformer.php b/app/Transformers/AttachmentTransformer.php
index 81c03f4b7e..740b06ffbf 100644
--- a/app/Transformers/AttachmentTransformer.php
+++ b/app/Transformers/AttachmentTransformer.php
@@ -49,6 +49,12 @@ class AttachmentTransformer extends TransformerAbstract
'notes' => $attachment->notes,
'mime' => $attachment->mime,
'size' => $attachment->size,
+ 'links' => [
+ [
+ 'rel' => 'self',
+ 'uri' => '/attachment/' . $attachment->id,
+ ],
+ ]
];
}
diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php
index bb3ff233df..86b9244d21 100644
--- a/app/Transformers/BillTransformer.php
+++ b/app/Transformers/BillTransformer.php
@@ -72,7 +72,7 @@ class BillTransformer extends TransformerAbstract
{
$attachments = $bill->attachments()->get();
- return $this->collection($attachments, new AttachmentTransformer);
+ return $this->collection($attachments, new AttachmentTransformer,'attachment');
}
/**
@@ -84,7 +84,7 @@ class BillTransformer extends TransformerAbstract
{
$notes = $bill->notes()->get();
- return $this->collection($notes, new NoteTransformer);
+ return $this->collection($notes, new NoteTransformer,'note');
}
/**
@@ -114,7 +114,7 @@ class BillTransformer extends TransformerAbstract
'links' => [
[
'rel' => 'self',
- 'uri' => '/bills/' . $bill->id,
+ 'uri' => '/bill/' . $bill->id,
],
],
];
diff --git a/app/Transformers/NoteTransformer.php b/app/Transformers/NoteTransformer.php
index e46a13d710..41c621215a 100644
--- a/app/Transformers/NoteTransformer.php
+++ b/app/Transformers/NoteTransformer.php
@@ -48,6 +48,12 @@ class NoteTransformer extends TransformerAbstract
'title' => $note->title,
'text' => $note->text,
'markdown' => $converter->convertToHtml($note->text),
+ 'links' => [
+ [
+ 'rel' => 'self',
+ 'uri' => '/note/' . $note->id,
+ ],
+ ]
];
}