user()->account->companies() ->orderBy($this->sort, $this->sortDirection) ->paginate($this->getLimitPerPage()); } catch (QueryException $e) { return $this->respondInvalidQuery(); } return CompanyResource::collection($companies); } /** * Get the detail of a given company. * * @param Request $request * @return \Illuminate\Http\Response */ public function show(Request $request, $companyId) { try { $company = Company::where('account_id', auth()->user()->account_id) ->where('id', $companyId) ->firstOrFail(); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } return new CompanyResource($company); } /** * Store the company. * * @param Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { try { $company = app(CreateCompany::class)->execute( $request->all() + [ 'account_id' => auth()->user()->account->id, ] ); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } catch (ValidationException $e) { return $this->respondValidatorFailed($e->validator); } catch (QueryException $e) { return $this->respondInvalidQuery(); } return new CompanyResource($company); } /** * Update a company. * * @param Request $request * @param int $companyId * @return \Illuminate\Http\Response */ public function update(Request $request, $companyId) { try { $company = app(UpdateCompany::class)->execute( $request->all() + [ 'account_id' => auth()->user()->account->id, 'company_id' => $companyId, ] ); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } catch (ValidationException $e) { return $this->respondValidatorFailed($e->validator); } catch (QueryException $e) { return $this->respondInvalidQuery(); } return new CompanyResource($company); } /** * Delete a company. * * @param Request $request * @return \Illuminate\Http\Response */ public function destroy(Request $request, $companyId) { try { app(DestroyCompany::class)->execute([ 'account_id' => auth()->user()->account->id, 'company_id' => $companyId, ]); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } catch (ValidationException $e) { return $this->respondValidatorFailed($e->validator); } catch (QueryException $e) { return $this->respondInvalidQuery(); } return $this->respondObjectDeleted((int) $companyId); } }