149 lines
3.9 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2018-06-10 02:48:51 +03:00
namespace App\Http\Controllers\Api\Common;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\ApiController;
2018-06-10 02:48:51 +03:00
use App\Http\Requests\Common\Company as Request;
2019-11-16 10:21:14 +03:00
use App\Jobs\Common\CreateCompany;
use App\Jobs\Common\DeleteCompany;
use App\Jobs\Common\UpdateCompany;
2018-06-10 02:48:51 +03:00
use App\Models\Common\Company;
use App\Transformers\Common\Company as Transformer;
2019-11-16 10:21:14 +03:00
use App\Traits\Users;
use Dingo\Api\Http\Response;
2017-09-14 22:21:00 +03:00
class Companies extends ApiController
{
2019-11-16 10:21:14 +03:00
use Users;
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return \Dingo\Api\Http\Response
*/
public function index()
{
2019-11-16 10:21:14 +03:00
$companies = user()->companies()->collect();
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $this->response->paginator($companies, new Transformer());
2017-09-14 22:21:00 +03:00
}
/**
* Display the specified resource.
*
* @param Company $company
* @return \Dingo\Api\Http\Response
*/
public function show(Company $company)
{
2019-11-16 10:21:14 +03:00
try {
// Check if user can access company
2021-01-29 21:38:10 +03:00
$this->canAccess($company);
2017-10-12 16:28:48 +03:00
2019-11-16 10:21:14 +03:00
return $this->response->item($company, new Transformer());
2019-12-22 15:58:48 +03:00
} catch (\Exception $e) {
2019-11-16 10:21:14 +03:00
$this->response->errorUnauthorized($e->getMessage());
}
2017-09-14 22:21:00 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return \Dingo\Api\Http\Response
*/
public function store(Request $request)
{
2019-11-16 10:21:14 +03:00
$company = $this->dispatch(new CreateCompany($request));
2017-09-19 20:04:30 +03:00
2020-08-11 16:52:10 +03:00
return $this->response->created(route('api.companies.show', $company->id));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $company
* @param $request
* @return \Dingo\Api\Http\Response
*/
public function update(Company $company, Request $request)
{
2019-11-16 10:21:14 +03:00
try {
2020-12-25 12:08:15 +03:00
$company = $this->dispatch(new UpdateCompany($company, $request, session('company_id')));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $this->item($company->fresh(), new Transformer());
} catch (\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
}
2017-09-19 20:04:30 +03:00
2019-11-16 10:21:14 +03:00
/**
* Enable the specified resource in storage.
*
* @param Company $company
* @return \Dingo\Api\Http\Response
*/
public function enable(Company $company)
{
try {
2020-12-25 12:08:15 +03:00
$company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]), session('company_id')));
2017-09-19 20:04:30 +03:00
2019-11-16 10:21:14 +03:00
return $this->item($company->fresh(), new Transformer());
} catch (\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
}
2017-09-19 20:04:30 +03:00
2019-11-16 10:21:14 +03:00
/**
* Disable the specified resource in storage.
*
* @param Company $company
* @return \Dingo\Api\Http\Response
*/
public function disable(Company $company)
{
try {
2020-12-25 12:08:15 +03:00
$company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]), session('company_id')));
2017-09-19 20:04:30 +03:00
2019-11-16 10:21:14 +03:00
return $this->item($company->fresh(), new Transformer());
} catch (\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
2017-09-14 22:21:00 +03:00
}
/**
* Remove the specified resource from storage.
*
* @param Company $company
* @return \Dingo\Api\Http\Response
*/
public function destroy(Company $company)
{
2019-11-16 10:21:14 +03:00
try {
2020-12-25 12:08:15 +03:00
$this->dispatch(new DeleteCompany($company, session('company_id')));
2019-11-16 10:21:14 +03:00
return $this->response->noContent();
} catch (\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
}
/**
* Check user company assignment
*
* @param Company $company
*
* @return \Dingo\Api\Http\Response
*/
2021-04-09 12:56:45 +03:00
public function canAccess(Company $company)
2019-11-16 10:21:14 +03:00
{
2021-01-29 21:38:10 +03:00
if (!empty($company) && $this->isUserCompany($company->id)) {
2019-11-16 10:21:14 +03:00
return new Response('');
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
$message = trans('companies.error.not_user_company');
2017-10-12 16:28:48 +03:00
2019-11-16 10:21:14 +03:00
$this->response->errorUnauthorized($message);
2017-09-14 22:21:00 +03:00
}
}