. */ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers; use FireflyIII\Api\V1\Requests\RecurrenceRequest; use FireflyIII\Models\Recurrence; use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; use FireflyIII\Transformers\RecurrenceTransformer; use FireflyIII\User; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; 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; /** * * Class RecurrenceController */ class RecurrenceController extends Controller { /** @var RecurringRepositoryInterface */ private $repository; public function __construct() { parent::__construct(); $this->middleware( function ($request, $next) { /** @var User $user */ $user = auth()->user(); /** @var RecurringRepositoryInterface repository */ $this->repository = app(RecurringRepositoryInterface::class); $this->repository->setUser($user); return $next($request); } ); } /** * Delete the resource. * * @param Recurrence $recurrence * * @return JsonResponse */ public function delete(Recurrence $recurrence): JsonResponse { $this->repository->destroy($recurrence); return response()->json([], 204); } /** * List all of them. * * @param Request $request * * @return JsonResponse] */ public function index(Request $request): JsonResponse { // create some objects: $manager = new Manager; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; // types to get, page size: $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; // get list of budgets. Count it and split it. $collection = $this->repository->getAll(); $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.recurrences.index') . $this->buildParams()); // present to user. $manager->setSerializer(new JsonApiSerializer($baseUrl)); $resource = new FractalCollection($piggyBanks, new RecurrenceTransformer($this->parameters), 'recurrences'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } /** * List single resource. * * @param Request $request * @param Recurrence $recurrence * * @return JsonResponse */ public function show(Request $request, Recurrence $recurrence): JsonResponse { $manager = new Manager(); // add include parameter: $include = $request->get('include') ?? ''; $manager->parseIncludes($include); $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); $resource = new Item($recurrence, new RecurrenceTransformer($this->parameters), 'recurrences'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } /** * Store new object. * * @param RecurrenceRequest $request * * @return JsonResponse */ public function store(RecurrenceRequest $request): JsonResponse { $recurrence = $this->repository->store($request->getAll()); $manager = new Manager(); $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); $resource = new Item($recurrence, new RecurrenceTransformer($this->parameters), 'recurrences'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } /** * @param RecurrenceRequest $request * @param Recurrence $recurrence * * @return JsonResponse */ public function update(RecurrenceRequest $request, Recurrence $recurrence): JsonResponse { $data = $request->getAll(); // $category = $this->repository->update($recurrence, $data); $manager = new Manager(); $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); $resource = new Item($category, new RecurrenceTransformer($this->parameters), 'recurrences'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } }