. */ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers\Models\Account; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\Account; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Transformers\AttachmentTransformer; use FireflyIII\Transformers\PiggyBankTransformer; use FireflyIII\Transformers\TransactionGroupTransformer; use FireflyIII\User; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use League\Fractal\Pagination\IlluminatePaginatorAdapter; use League\Fractal\Resource\Collection as FractalCollection; /** * Class ListController */ class ListController extends Controller { use TransactionFilter; public const string RESOURCE_KEY = 'accounts'; private AccountRepositoryInterface $repository; /** * AccountController constructor. */ public function __construct() { parent::__construct(); $this->middleware( function ($request, $next) { $this->repository = app(AccountRepositoryInterface::class); $this->repository->setUser(auth()->user()); return $next($request); } ); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/accounts/listAttachmentByAccount * * @throws FireflyException */ public function attachments(Account $account): JsonResponse { $manager = $this->getManager(); $pageSize = $this->parameters->get('limit'); $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', self::CONTENT_TYPE); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/accounts/listPiggyBankByAccount * * @throws FireflyException */ public function piggyBanks(Account $account): JsonResponse { // create some objects: $manager = $this->getManager(); // types to get, page size: $pageSize = $this->parameters->get('limit'); // get list of budgets. Count it and split it. $collection = $this->repository->getPiggyBanks($account); $count = $collection->count(); $piggyBanks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); // make paginator: $paginator = new LengthAwarePaginator($piggyBanks, $count, $pageSize, $this->parameters->get('page')); $paginator->setPath(route('api.v1.accounts.piggy-banks', [$account->id]).$this->buildParams()); /** @var PiggyBankTransformer $transformer */ $transformer = app(PiggyBankTransformer::class); $transformer->setParameters($this->parameters); $resource = new FractalCollection($piggyBanks, $transformer, 'piggy_banks'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/accounts/listTransactionByAccount * * Show all transaction groups related to the account. * * @throws FireflyException */ public function transactions(Request $request, Account $account): JsonResponse { $pageSize = $this->parameters->get('limit'); $type = $request->get('type') ?? 'default'; $this->parameters->set('type', $type); $types = $this->mapTransactionTypes($this->parameters->get('type')); $manager = $this->getManager(); /** @var User $admin */ $admin = auth()->user(); // use new group collector: /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector->setUser($admin)->setAccounts(new Collection([$account])) ->withAPIInformation()->setLimit($pageSize)->setPage($this->parameters->get('page'))->setTypes($types) ; if (null !== $this->parameters->get('start')) { $collector->setStart($this->parameters->get('start')); } if (null !== $this->parameters->get('end')) { $collector->setEnd($this->parameters->get('end')); } $paginator = $collector->getPaginatedGroups(); $paginator->setPath(route('api.v1.accounts.transactions', [$account->id]).$this->buildParams()); $groups = $paginator->getCollection(); /** @var TransactionGroupTransformer $transformer */ $transformer = app(TransactionGroupTransformer::class); $transformer->setParameters($this->parameters); $resource = new FractalCollection($groups, $transformer, 'transactions'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } }