Refactor bulk actions

This commit is contained in:
David Bomba 2019-04-23 16:19:45 +10:00
parent 706625e83f
commit f496949008
3 changed files with 44 additions and 9 deletions

View File

@ -7,6 +7,7 @@ use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException; use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Symfony\Component\Debug\Exception\FatalThrowableError;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
{ {
@ -61,7 +62,11 @@ class Handler extends ExceptionHandler
} }
else if($exception instanceof ThrottleRequestsException) else if($exception instanceof ThrottleRequestsException)
{ {
return response()->json(['message'=>'Too many requests'],429); return response()->json(['message'=>'Too many requests'],429);
}
else if($exception instanceof FatalThrowableError)
{
return response()->json(['message'=>'Fatal error', 500]);
} }
return parent::render($request, $exception); return parent::render($request, $exception);

View File

@ -19,6 +19,7 @@ use App\Models\ClientContact;
use App\Models\Country; use App\Models\Country;
use App\Models\Currency; use App\Models\Currency;
use App\Models\Size; use App\Models\Size;
use App\Repositories\BaseRepository;
use App\Repositories\ClientRepository; use App\Repositories\ClientRepository;
use App\Transformers\ClientTransformer; use App\Transformers\ClientTransformer;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
@ -41,17 +42,17 @@ class ClientController extends BaseController
/** /**
* @var ClientRepository * @var ClientRepository
*/ */
protected $clientRepo; protected $client_repo;
/** /**
* ClientController constructor. * ClientController constructor.
* @param ClientRepository $clientRepo * @param ClientRepository $clientRepo
*/ */
public function __construct(ClientRepository $clientRepo) public function __construct(ClientRepository $client_repo)
{ {
parent::__construct(); parent::__construct();
$this->clientRepo = $clientRepo; $this->client_repo = $client_repo;
} }
@ -101,7 +102,7 @@ class ClientController extends BaseController
*/ */
public function update(UpdateClientRequest $request, Client $client) public function update(UpdateClientRequest $request, Client $client)
{ {
$client = $this->clientRepo->save($request, $client); $client = $this->client_repo->save($request, $client);
return $this->itemResponse($client); return $this->itemResponse($client);
@ -129,7 +130,7 @@ class ClientController extends BaseController
public function store(StoreClientRequest $request) public function store(StoreClientRequest $request)
{ {
$client = $this->clientRepo->save($request, ClientFactory::create(auth()->user()->company()->id, auth()->user()->id)); $client = $this->client_repo->save($request, ClientFactory::create(auth()->user()->company()->id, auth()->user()->id));
$client->load('contacts', 'primary_contact'); $client->load('contacts', 'primary_contact');
@ -168,12 +169,11 @@ class ClientController extends BaseController
$clients->each(function ($client, $key) use($action){ $clients->each(function ($client, $key) use($action){
if(auth()->user()->can('edit', $client)) if(auth()->user()->can('edit', $client))
ActionEntity::dispatchNow($client, $action); $this->client_repo->{$action}($invoice);
}); });
//todo need to return the updated dataset return $this->listResponse(Client::withTrashed()->whereIn('id', $ids));
return response()->json([], 200);
} }

View File

@ -12,7 +12,9 @@ use App\Http\Requests\Invoice\EditInvoiceRequest;
use App\Http\Requests\Invoice\ShowInvoiceRequest; use App\Http\Requests\Invoice\ShowInvoiceRequest;
use App\Http\Requests\Invoice\StoreInvoiceRequest; use App\Http\Requests\Invoice\StoreInvoiceRequest;
use App\Http\Requests\Invoice\UpdateInvoiceRequest; use App\Http\Requests\Invoice\UpdateInvoiceRequest;
use App\Jobs\Entity\ActionEntity;
use App\Models\Invoice; use App\Models\Invoice;
use App\Repositories\BaseRepository;
use App\Repositories\InvoiceRepository; use App\Repositories\InvoiceRepository;
use App\Transformers\InvoiceTransformer; use App\Transformers\InvoiceTransformer;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
@ -38,6 +40,8 @@ class InvoiceController extends BaseController
*/ */
protected $invoice_repo; protected $invoice_repo;
protected $base_repo;
/** /**
* InvoiceController constructor. * InvoiceController constructor.
* *
@ -165,6 +169,32 @@ class InvoiceController extends BaseController
} }
/**
* Perform bulk actions on the list view
*
* @return Collection
*/
public function bulk()
{
$action = request()->input('action');
$ids = request()->input('ids');
$invoices = Invoice::withTrashed()->find($ids);
$invoices->each(function ($invoice, $key) use($action){
if(auth()->user()->can('edit', $invoice))
$this->invoice_repo->{$action}($invoice);
});
//todo need to return the updated dataset
return $this->listResponse(Invoice::withTrashed()->whereIn('id', $ids));
}
public function action(ActionInvoiceRequest $request, Invoice $invoice, $action) public function action(ActionInvoiceRequest $request, Invoice $invoice, $action)
{ {