Consistent use of links in transformers.

This commit is contained in:
James Cole 2018-02-07 11:20:24 +01:00
parent 909dc212fb
commit f445a95c26
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
4 changed files with 58 additions and 9 deletions

View File

@ -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());
}
/**

View File

@ -49,6 +49,12 @@ class AttachmentTransformer extends TransformerAbstract
'notes' => $attachment->notes,
'mime' => $attachment->mime,
'size' => $attachment->size,
'links' => [
[
'rel' => 'self',
'uri' => '/attachment/' . $attachment->id,
],
]
];
}

View File

@ -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,
],
],
];

View File

@ -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,
],
]
];
}