repository = $repository; } /** * Return an array of all eggs on a given nest. * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function index(GetEggsRequest $request, Nest $nest): array { $perPage = $request->query('per_page', 10); if ($perPage > 100) { throw new QueryValueOutOfRangeHttpException('per_page', 1, 100); } $eggs = QueryBuilder::for(Egg::query()) ->where('nest_id', '=', $nest->id) ->allowedFilters(['id', 'name', 'author']) ->allowedSorts(['id', 'name', 'author']); if ($perPage > 0) { $eggs = $eggs->paginate($perPage); } return $this->fractal->collection($eggs) ->transformWith($this->getTransformer(EggTransformer::class)) ->toArray(); } /** * Returns a single egg. * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function view(GetEggRequest $request, Egg $egg): array { return $this->fractal->item($egg) ->transformWith($this->getTransformer(EggTransformer::class)) ->toArray(); } /** * Creates a new egg. * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function store(StoreEggRequest $request): JsonResponse { $egg = Egg::query()->create($request->validated()); return $this->fractal->item($egg) ->transformWith($this->getTransformer(EggTransformer::class)) ->respond(JsonResponse::HTTP_CREATED); } /** * Updates an egg. * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function update(UpdateEggRequest $request, Egg $egg): array { $egg->update($request->validated()); return $this->fractal->item($egg) ->transformWith($this->getTransformer(EggTransformer::class)) ->toArray(); } /** * Deletes an egg. * * @throws \Exception */ public function delete(DeleteEggRequest $request, Egg $egg): Response { $egg->delete(); return $this->returnNoContent(); } }