From 75afe35e98d59a5c4bbac325f9a17486a57396f0 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 19 Mar 2020 08:32:42 +0100 Subject: [PATCH] API can deal with attachments for more models. #2828 --- app/Api/V1/Controllers/AccountController.php | 30 ++++++++++++++++++ app/Api/V1/Controllers/BudgetController.php | 30 ++++++++++++++++++ app/Api/V1/Controllers/CategoryController.php | 30 ++++++++++++++++++ .../V1/Controllers/PiggyBankController.php | 31 +++++++++++++++++++ app/Api/V1/Controllers/TagController.php | 31 +++++++++++++++++++ app/Models/Account.php | 10 ++++++ app/Models/Bill.php | 2 -- app/Models/Budget.php | 11 +++++++ app/Models/Category.php | 12 ++++++- app/Models/PiggyBank.php | 10 +++++- app/Models/Tag.php | 9 ++++++ .../Account/AccountRepository.php | 8 +++++ .../Account/AccountRepositoryInterface.php | 7 +++++ app/Repositories/Budget/BudgetRepository.php | 8 +++++ .../Budget/BudgetRepositoryInterface.php | 7 +++++ .../Category/CategoryRepository.php | 8 +++++ .../Category/CategoryRepositoryInterface.php | 7 +++++ .../PiggyBank/PiggyBankRepository.php | 8 +++++ .../PiggyBankRepositoryInterface.php | 7 +++++ app/Repositories/Tag/TagRepository.php | 8 +++++ .../Tag/TagRepositoryInterface.php | 7 +++++ 21 files changed, 277 insertions(+), 4 deletions(-) diff --git a/app/Api/V1/Controllers/AccountController.php b/app/Api/V1/Controllers/AccountController.php index dae6bec18b..f3b555b7bf 100644 --- a/app/Api/V1/Controllers/AccountController.php +++ b/app/Api/V1/Controllers/AccountController.php @@ -31,6 +31,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Support\Http\Api\AccountFilter; use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Transformers\AccountTransformer; +use FireflyIII\Transformers\AttachmentTransformer; use FireflyIII\Transformers\PiggyBankTransformer; use FireflyIII\Transformers\TransactionGroupTransformer; use FireflyIII\User; @@ -73,6 +74,35 @@ class AccountController extends Controller ); } + /** + * @param Account $account + * + * @return JsonResponse + * @codeCoverageIgnore + */ + public function attachments(Account $account): JsonResponse + { + $manager = $this->getManager(); + $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; + $collection = $this->repository->getAttachments($account); + + $count = $collection->count(); + $attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); + + // make paginator: + $paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); + $paginator->setPath(route('api.v1.accounts.attachments', [$account->id]) . $this->buildParams()); + + /** @var AttachmentTransformer $transformer */ + $transformer = app(AttachmentTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new FractalCollection($attachments, $transformer, 'attachments'); + $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + } + /** * Remove the specified resource from storage. * diff --git a/app/Api/V1/Controllers/BudgetController.php b/app/Api/V1/Controllers/BudgetController.php index ee94b68f31..592f01a725 100644 --- a/app/Api/V1/Controllers/BudgetController.php +++ b/app/Api/V1/Controllers/BudgetController.php @@ -33,6 +33,7 @@ use FireflyIII\Models\Budget; use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Support\Http\Api\TransactionFilter; +use FireflyIII\Transformers\AttachmentTransformer; use FireflyIII\Transformers\BudgetLimitTransformer; use FireflyIII\Transformers\BudgetTransformer; use FireflyIII\Transformers\TransactionGroupTransformer; @@ -109,6 +110,35 @@ class BudgetController extends Controller return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } + /** + * @param Budget $budget + * + * @return JsonResponse + * @codeCoverageIgnore + */ + public function attachments(Budget $budget): JsonResponse + { + $manager = $this->getManager(); + $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; + $collection = $this->repository->getAttachments($budget); + + $count = $collection->count(); + $attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); + + // make paginator: + $paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); + $paginator->setPath(route('api.v1.budgets.attachments', [$budget->id]) . $this->buildParams()); + + /** @var AttachmentTransformer $transformer */ + $transformer = app(AttachmentTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new FractalCollection($attachments, $transformer, 'attachments'); + $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + } + /** * Remove the specified resource from storage. * diff --git a/app/Api/V1/Controllers/CategoryController.php b/app/Api/V1/Controllers/CategoryController.php index a228b6f49a..2f35fdbe9e 100644 --- a/app/Api/V1/Controllers/CategoryController.php +++ b/app/Api/V1/Controllers/CategoryController.php @@ -29,6 +29,7 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\Category; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Support\Http\Api\TransactionFilter; +use FireflyIII\Transformers\AttachmentTransformer; use FireflyIII\Transformers\CategoryTransformer; use FireflyIII\Transformers\TransactionGroupTransformer; use FireflyIII\User; @@ -71,6 +72,35 @@ class CategoryController extends Controller ); } + /** + * @param Category $category + * + * @return JsonResponse + * @codeCoverageIgnore + */ + public function attachments(Category $category): JsonResponse + { + $manager = $this->getManager(); + $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; + $collection = $this->repository->getAttachments($category); + + $count = $collection->count(); + $attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); + + // make paginator: + $paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); + $paginator->setPath(route('api.v1.categories.attachments', [$category->id]) . $this->buildParams()); + + /** @var AttachmentTransformer $transformer */ + $transformer = app(AttachmentTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new FractalCollection($attachments, $transformer, 'attachments'); + $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + } + /** * Remove the specified resource from storage. * diff --git a/app/Api/V1/Controllers/PiggyBankController.php b/app/Api/V1/Controllers/PiggyBankController.php index a9a153315e..ec61455628 100644 --- a/app/Api/V1/Controllers/PiggyBankController.php +++ b/app/Api/V1/Controllers/PiggyBankController.php @@ -27,6 +27,7 @@ use FireflyIII\Api\V1\Requests\PiggyBankRequest; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\PiggyBank; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; +use FireflyIII\Transformers\AttachmentTransformer; use FireflyIII\Transformers\PiggyBankEventTransformer; use FireflyIII\Transformers\PiggyBankTransformer; use FireflyIII\User; @@ -83,6 +84,36 @@ class PiggyBankController extends Controller return response()->json([], 204); } + + /** + * @param PiggyBank $piggyBank + * + * @return JsonResponse + * @codeCoverageIgnore + */ + public function attachments(PiggyBank $piggyBank): JsonResponse + { + $manager = $this->getManager(); + $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; + $collection = $this->repository->getAttachments($piggyBank); + + $count = $collection->count(); + $attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); + + // make paginator: + $paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); + $paginator->setPath(route('api.v1.piggy_banks.attachments', [$piggyBank->id]) . $this->buildParams()); + + /** @var AttachmentTransformer $transformer */ + $transformer = app(AttachmentTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new FractalCollection($attachments, $transformer, 'attachments'); + $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + } + /** * List all of them. * diff --git a/app/Api/V1/Controllers/TagController.php b/app/Api/V1/Controllers/TagController.php index eb60351a51..2ef58d6d1a 100644 --- a/app/Api/V1/Controllers/TagController.php +++ b/app/Api/V1/Controllers/TagController.php @@ -31,6 +31,7 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\Tag; use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Support\Http\Api\TransactionFilter; +use FireflyIII\Transformers\AttachmentTransformer; use FireflyIII\Transformers\TagTransformer; use FireflyIII\Transformers\TransactionGroupTransformer; use FireflyIII\User; @@ -138,6 +139,36 @@ class TagController extends Controller return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } + + /** + * @param Tag $tag + * + * @return JsonResponse + * @codeCoverageIgnore + */ + public function attachments(Tag $tag): JsonResponse + { + $manager = $this->getManager(); + $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; + $collection = $this->repository->getAttachments($tag); + + $count = $collection->count(); + $attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); + + // make paginator: + $paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); + $paginator->setPath(route('api.v1.tags.attachments', [$tag->id]) . $this->buildParams()); + + /** @var AttachmentTransformer $transformer */ + $transformer = app(AttachmentTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new FractalCollection($attachments, $transformer, 'attachments'); + $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + } + /** * List single resource. * diff --git a/app/Models/Account.php b/app/Models/Account.php index 118856a481..d3050b6a6e 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -134,6 +134,16 @@ class Account extends Model throw new NotFoundHttpException; } + + /** + * @codeCoverageIgnore + * @return MorphMany + */ + public function attachments(): MorphMany + { + return $this->morphMany(Attachment::class, 'attachable'); + } + /** * @return HasMany * @codeCoverageIgnore diff --git a/app/Models/Bill.php b/app/Models/Bill.php index a707ffcabc..d4d1c5bdca 100644 --- a/app/Models/Bill.php +++ b/app/Models/Bill.php @@ -58,8 +58,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @SuppressWarnings (PHPMD.CouplingBetweenObjects) * @property \Illuminate\Support\Carbon|null $deleted_at * @property int $user_id - * @property bool $name_encrypted - * @property bool $match_encrypted * @property-read \Illuminate\Database\Eloquent\Collection|Attachment[] $attachments * @property-read \Illuminate\Database\Eloquent\Collection|TransactionJournal[] $transactionJournals * @method static bool|null forceDelete() diff --git a/app/Models/Budget.php b/app/Models/Budget.php index ede495ec33..92a56c92ac 100644 --- a/app/Models/Budget.php +++ b/app/Models/Budget.php @@ -29,6 +29,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Query\Builder; use Illuminate\Support\Collection; @@ -133,6 +134,16 @@ class Budget extends Model return $this->hasMany(BudgetLimit::class); } + + /** + * @codeCoverageIgnore + * @return MorphMany + */ + public function attachments(): MorphMany + { + return $this->morphMany(Attachment::class, 'attachable'); + } + /** * @codeCoverageIgnore * @return BelongsToMany diff --git a/app/Models/Category.php b/app/Models/Category.php index ef570c20a3..c18e2396a6 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -30,6 +30,7 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Query\Builder; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -47,7 +48,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property Carbon $updated_at * @property \Illuminate\Support\Carbon|null $deleted_at * @property int $user_id - * @property bool $encrypted * @property-read Collection|TransactionJournal[] $transactionJournals * @property-read Collection|Transaction[] $transactions * @method static bool|null forceDelete() @@ -120,6 +120,16 @@ class Category extends Model return $this->belongsToMany(TransactionJournal::class, 'category_transaction_journal', 'category_id'); } + + /** + * @codeCoverageIgnore + * @return MorphMany + */ + public function attachments(): MorphMany + { + return $this->morphMany(Attachment::class, 'attachable'); + } + /** * @codeCoverageIgnore * @return BelongsToMany diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php index edbe3f4d2e..d658a51b22 100644 --- a/app/Models/PiggyBank.php +++ b/app/Models/PiggyBank.php @@ -49,7 +49,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property int $account_id * @property bool encrypted * @property \Illuminate\Support\Carbon|null $deleted_at - * @property bool $encrypted * @property-read Collection|Note[] $notes * @property-read Collection|PiggyBankEvent[] $piggyBankEvents * @property-read Collection|PiggyBankRepetition[] $piggyBankRepetitions @@ -122,6 +121,15 @@ class PiggyBank extends Model throw new NotFoundHttpException; } + /** + * @codeCoverageIgnore + * @return MorphMany + */ + public function attachments(): MorphMany + { + return $this->morphMany(Attachment::class, 'attachable'); + } + /** * @codeCoverageIgnore * @return BelongsTo diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 0b11ad319d..e24381a493 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -130,6 +130,15 @@ class Tag extends Model return $this->morphMany(Location::class, 'locatable'); } + /** + * @codeCoverageIgnore + * @return MorphMany + */ + public function attachments(): MorphMany + { + return $this->morphMany(Attachment::class, 'attachable'); + } + /** * @codeCoverageIgnore * @return BelongsToMany diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index c933e7c835..c3bb6a12f1 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -645,4 +645,12 @@ class AccountRepository implements AccountRepositoryInterface { return $account->locations()->first(); } + + /** + * @inheritDoc + */ + public function getAttachments(Account $account): Collection + { + return $account->attachments()->get(); + } } diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index 2a52bc4734..3c8e5847ad 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -47,6 +47,13 @@ interface AccountRepositoryInterface */ public function count(array $types): int; + /** + * @param Account $account + * + * @return Collection + */ + public function getAttachments(Account $account): Collection; + /** * Get account location, if any. * diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 84a7608399..07fdebbc86 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -479,4 +479,12 @@ class BudgetRepository implements BudgetRepositoryInterface $autoBudget->delete(); } } + + /** + * @inheritDoc + */ + public function getAttachments(Budget $budget): Collection + { + return $budget->attachments()->get(); + } } diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index c7e008f96f..a170900cef 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -39,6 +39,13 @@ interface BudgetRepositoryInterface */ public function destroyAll(): void; + /** + * @param Budget $budget + * + * @return Collection + */ + public function getAttachments(Budget $budget): Collection; + /** * @param Budget $budget * diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index a84c9f47ad..3cf609c37c 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -373,4 +373,12 @@ class CategoryRepository implements CategoryRepositoryInterface $category->delete(); } } + + /** + * @inheritDoc + */ + public function getAttachments(Category $category): Collection + { + return $category->attachments()->get(); + } } diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index 76bcb7bf8a..2b7dfddc5d 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -38,6 +38,13 @@ interface CategoryRepositoryInterface */ public function destroyAll(): void; + /** + * @param Category $category + * + * @return Collection + */ + public function getAttachments(Category $category): Collection; + /** * @param Category $category * diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php index c34fd2a9cb..d34da21654 100644 --- a/app/Repositories/PiggyBank/PiggyBankRepository.php +++ b/app/Repositories/PiggyBank/PiggyBankRepository.php @@ -680,4 +680,12 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface return true; } + + /** + * @inheritDoc + */ + public function getAttachments(PiggyBank $piggyBank): Collection + { + return $piggyBank->attachments()->get(); + } } diff --git a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php index 051214d205..aa07fdb025 100644 --- a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php +++ b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php @@ -44,6 +44,13 @@ interface PiggyBankRepositoryInterface */ public function setCurrentAmount(PiggyBank $piggyBank, string $amount): PiggyBank; + /** + * @param PiggyBank $piggyBank + * + * @return Collection + */ + public function getAttachments(PiggyBank $piggyBank): Collection; + /** * @param PiggyBank $piggyBank * @param string $amount diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 3204204a22..86f3f74475 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -539,4 +539,12 @@ class TagRepository implements TagRepositoryInterface return $min; } + + /** + * @inheritDoc + */ + public function getAttachments(Tag $tag): Collection + { + return $tag->attachments()->get(); + } } diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php index 4b1ca418b3..7b063968dd 100644 --- a/app/Repositories/Tag/TagRepositoryInterface.php +++ b/app/Repositories/Tag/TagRepositoryInterface.php @@ -39,6 +39,13 @@ interface TagRepositoryInterface */ public function count(): int; + /** + * @param Tag $tag + * + * @return Collection + */ + public function getAttachments(Tag $tag): Collection; + /** * This method destroys a tag. *