v2 first commit
This commit is contained in:
@ -2,15 +2,19 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Common;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use App\Http\Requests\Common\Company as Request;
|
||||
use App\Jobs\Common\CreateCompany;
|
||||
use App\Jobs\Common\DeleteCompany;
|
||||
use App\Jobs\Common\UpdateCompany;
|
||||
use App\Models\Common\Company;
|
||||
use App\Transformers\Common\Company as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
use App\Traits\Users;
|
||||
use Dingo\Api\Http\Response;
|
||||
|
||||
class Companies extends ApiController
|
||||
{
|
||||
use Helpers;
|
||||
use Users;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
@ -19,13 +23,9 @@ class Companies extends ApiController
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$companies = app('Dingo\Api\Auth\Auth')->user()->companies()->get()->sortBy('name');
|
||||
$companies = user()->companies()->collect();
|
||||
|
||||
foreach ($companies as $company) {
|
||||
$company->setSettings();
|
||||
}
|
||||
|
||||
return $this->response->collection($companies, new Transformer());
|
||||
return $this->response->paginator($companies, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,15 +36,14 @@ class Companies extends ApiController
|
||||
*/
|
||||
public function show(Company $company)
|
||||
{
|
||||
// Check if user can access company
|
||||
$companies = app('Dingo\Api\Auth\Auth')->user()->companies()->pluck('id')->toArray();
|
||||
if (!in_array($company->id, $companies)) {
|
||||
$this->response->errorUnauthorized();
|
||||
try {
|
||||
// Check if user can access company
|
||||
$this->owner($company);
|
||||
|
||||
return $this->response->item($company, new Transformer());
|
||||
} catch (\HttpException $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
|
||||
$company->setSettings();
|
||||
|
||||
return $this->response->item($company, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,22 +54,7 @@ class Companies extends ApiController
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$company = Company::create($request->all());
|
||||
|
||||
// Clear settings
|
||||
setting()->forgetAll();
|
||||
setting()->setExtraColumns(['company_id' => $company->id]);
|
||||
|
||||
// Create settings
|
||||
setting()->set([
|
||||
'general.company_name' => $request->get('company_name'),
|
||||
'general.company_email' => $request->get('company_email'),
|
||||
'general.company_address' => $request->get('company_address'),
|
||||
'general.default_currency' => $request->get('default_currency'),
|
||||
'general.default_locale' => $request->get('default_locale', 'en-GB'),
|
||||
]);
|
||||
|
||||
setting()->save();
|
||||
$company = $this->dispatch(new CreateCompany($request));
|
||||
|
||||
return $this->response->created(url('api/companies/' . $company->id));
|
||||
}
|
||||
@ -84,31 +68,47 @@ class Companies extends ApiController
|
||||
*/
|
||||
public function update(Company $company, Request $request)
|
||||
{
|
||||
// Check if user can access company
|
||||
$companies = app('Dingo\Api\Auth\Auth')->user()->companies()->pluck('id')->toArray();
|
||||
if (!in_array($company->id, $companies)) {
|
||||
$this->response->errorUnauthorized();
|
||||
try {
|
||||
$company = $this->dispatch(new UpdateCompany($company, $request));
|
||||
|
||||
return $this->item($company->fresh(), new Transformer());
|
||||
} catch (\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Update company
|
||||
$company->update(['domain' => $request->get('domain')]);
|
||||
/**
|
||||
* Enable the specified resource in storage.
|
||||
*
|
||||
* @param Company $company
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function enable(Company $company)
|
||||
{
|
||||
try {
|
||||
$company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 1])));
|
||||
|
||||
// Update settings
|
||||
setting()->forgetAll();
|
||||
setting()->setExtraColumns(['company_id' => $company->id]);
|
||||
setting()->load(true);
|
||||
return $this->item($company->fresh(), new Transformer());
|
||||
} catch (\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
setting()->set([
|
||||
'general.company_name' => $request->get('company_name'),
|
||||
'general.company_email' => $request->get('company_email'),
|
||||
'general.company_address' => $request->get('company_address'),
|
||||
'general.default_currency' => $request->get('default_currency'),
|
||||
'general.default_locale' => $request->get('default_locale', 'en-GB'),
|
||||
]);
|
||||
/**
|
||||
* Disable the specified resource in storage.
|
||||
*
|
||||
* @param Company $company
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function disable(Company $company)
|
||||
{
|
||||
try {
|
||||
$company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 0])));
|
||||
|
||||
setting()->save();
|
||||
|
||||
return $this->response->item($company->fresh(), new Transformer());
|
||||
return $this->item($company->fresh(), new Transformer());
|
||||
} catch (\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -119,14 +119,30 @@ class Companies extends ApiController
|
||||
*/
|
||||
public function destroy(Company $company)
|
||||
{
|
||||
// Check if user can access company
|
||||
$companies = app('Dingo\Api\Auth\Auth')->user()->companies()->pluck('id')->toArray();
|
||||
if (!in_array($company->id, $companies)) {
|
||||
$this->response->errorUnauthorized();
|
||||
try {
|
||||
$this->dispatch(new DeleteCompany($company));
|
||||
|
||||
return $this->response->noContent();
|
||||
} catch (\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check user company assignment
|
||||
*
|
||||
* @param Company $company
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function owner(Company $company)
|
||||
{
|
||||
if ($this->isUserCompany($company->id)) {
|
||||
return new Response('');
|
||||
}
|
||||
|
||||
$company->delete();
|
||||
$message = trans('companies.error.not_user_company');
|
||||
|
||||
return $this->response->noContent();
|
||||
$this->response->errorUnauthorized($message);
|
||||
}
|
||||
}
|
||||
|
133
app/Http/Controllers/Api/Common/Contacts.php
Normal file
133
app/Http/Controllers/Api/Common/Contacts.php
Normal file
@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Common;
|
||||
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use App\Http\Requests\Common\Contact as Request;
|
||||
use App\Jobs\Common\CreateContact;
|
||||
use App\Jobs\Common\DeleteContact;
|
||||
use App\Jobs\Common\UpdateContact;
|
||||
use App\Models\Common\Contact;
|
||||
use App\Traits\Uploads;
|
||||
use App\Transformers\Common\Contact as Transformer;
|
||||
|
||||
class Contacts extends ApiController
|
||||
{
|
||||
use Uploads;
|
||||
|
||||
/**
|
||||
* Instantiate a new controller instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// Add CRUD permission check
|
||||
$this->middleware('permission:create-incomes-customers')->only(['create', 'store', 'duplicate', 'import']);
|
||||
$this->middleware('permission:read-incomes-customers')->only(['index', 'show', 'edit', 'export']);
|
||||
$this->middleware('permission:update-incomes-customers')->only(['update', 'enable', 'disable']);
|
||||
$this->middleware('permission:delete-incomes-customers')->only('destroy');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$contacts = Contact::collect();
|
||||
|
||||
return $this->response->paginator($contacts, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int|string $id
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
// Check if we're querying by id or email
|
||||
if (is_numeric($id)) {
|
||||
$contact = Contact::find($id);
|
||||
} else {
|
||||
$contact = Contact::where('email', $id)->first();
|
||||
}
|
||||
|
||||
return $this->response->item($contact, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$contact = $this->dispatch(new CreateContact($request));
|
||||
|
||||
return $this->response->created(url('api/contacts/' . $contact->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param $contact
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function update(Contact $contact, Request $request)
|
||||
{
|
||||
$contact = $this->dispatch(new UpdateContact($contact, $request));
|
||||
|
||||
return $this->item($contact->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the specified resource in storage.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function enable(Contact $contact)
|
||||
{
|
||||
$contact = $this->dispatch(new UpdateContact($contact, request()->merge(['enabled' => 1])));
|
||||
|
||||
return $this->item($contact->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the specified resource in storage.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function disable(Contact $contact)
|
||||
{
|
||||
try {
|
||||
$contact = $this->dispatch(new UpdateContact($contact, request()->merge(['enabled' => 0])));
|
||||
|
||||
return $this->item($contact->fresh(), new Transformer());
|
||||
} catch(\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function destroy(Contact $contact)
|
||||
{
|
||||
try {
|
||||
$this->dispatch(new DeleteContact($contact));
|
||||
|
||||
return $this->response->noContent();
|
||||
} catch(\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -2,15 +2,18 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Common;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use App\Http\Requests\Common\Item as Request;
|
||||
use App\Jobs\Common\CreateItem;
|
||||
use App\Jobs\Common\DeleteItem;
|
||||
use App\Jobs\Common\UpdateItem;
|
||||
use App\Models\Common\Item;
|
||||
use App\Transformers\Common\Item as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
use App\Traits\Uploads;
|
||||
|
||||
class Items extends ApiController
|
||||
{
|
||||
use Helpers;
|
||||
use Uploads;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
@ -32,12 +35,7 @@ class Items extends ApiController
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
// Check if we're querying by id or sku
|
||||
if (is_numeric($id)) {
|
||||
$item = Item::with(['category', 'tax'])->find($id);
|
||||
} else {
|
||||
$item = Item::with(['category', 'tax'])->where('sku', $id)->first();
|
||||
}
|
||||
$item = Item::with(['category', 'tax'])->find($id);
|
||||
|
||||
return $this->response->item($item, new Transformer());
|
||||
}
|
||||
@ -50,9 +48,9 @@ class Items extends ApiController
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$item = Item::create($request->all());
|
||||
$item = $this->dispatch(new CreateItem($request));
|
||||
|
||||
return $this->response->created(url('api/items/'.$item->id));
|
||||
return $this->response->created(url('api/items/' . $item->id));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,9 +62,35 @@ class Items extends ApiController
|
||||
*/
|
||||
public function update(Item $item, Request $request)
|
||||
{
|
||||
$item->update($request->all());
|
||||
$item = $this->dispatch(new UpdateItem($item, $request));
|
||||
|
||||
return $this->response->item($item->fresh(), new Transformer());
|
||||
return $this->item($item->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the specified resource in storage.
|
||||
*
|
||||
* @param Item $item
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function enable(Item $item)
|
||||
{
|
||||
$item = $this->dispatch(new UpdateItem($item, request()->merge(['enabled' => 1])));
|
||||
|
||||
return $this->item($item->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the specified resource in storage.
|
||||
*
|
||||
* @param Item $item
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function disable(Item $item)
|
||||
{
|
||||
$item = $this->dispatch(new UpdateItem($item, request()->merge(['enabled' => 0])));
|
||||
|
||||
return $this->item($item->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,8 +101,12 @@ class Items extends ApiController
|
||||
*/
|
||||
public function destroy(Item $item)
|
||||
{
|
||||
$item->delete();
|
||||
try {
|
||||
$this->dispatch(new DeleteItem($item));
|
||||
|
||||
return $this->response->noContent();
|
||||
return $this->response->noContent();
|
||||
} catch(\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Common;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use Date;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
|
||||
|
107
app/Http/Controllers/Api/Common/Reports.php
Normal file
107
app/Http/Controllers/Api/Common/Reports.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Common;
|
||||
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use App\Http\Requests\Common\Report as Request;
|
||||
use App\Jobs\Common\CreateReport;
|
||||
use App\Jobs\Common\DeleteReport;
|
||||
use App\Jobs\Common\UpdateReport;
|
||||
use App\Models\Common\Report;
|
||||
use App\Transformers\Common\Report as Transformer;
|
||||
|
||||
class Reports extends ApiController
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$reports = Report::collect();
|
||||
|
||||
return $this->response->paginator($reports, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param Report $report
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function show(Report $report)
|
||||
{
|
||||
return $this->response->item($report, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$report = $this->dispatch(new CreateReport($request));
|
||||
|
||||
return $this->response->created(url('api/reports/' . $report->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param $report
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function update(Report $report, Request $request)
|
||||
{
|
||||
$report = $this->dispatch(new UpdateReport($report, $request));
|
||||
|
||||
return $this->item($report->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the specified resource in storage.
|
||||
*
|
||||
* @param Report $report
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function enable(Report $report)
|
||||
{
|
||||
$report = $this->dispatch(new UpdateReport($report, request()->merge(['enabled' => 1])));
|
||||
|
||||
return $this->item($report->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the specified resource in storage.
|
||||
*
|
||||
* @param Report $report
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function disable(Report $report)
|
||||
{
|
||||
$report = $this->dispatch(new UpdateReport($report, request()->merge(['enabled' => 0])));
|
||||
|
||||
return $this->item($report->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Report $report
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function destroy(Report $report)
|
||||
{
|
||||
try {
|
||||
$this->dispatch(new DeleteReport($report));
|
||||
|
||||
return $this->response->noContent();
|
||||
} catch(\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user