Skip to content

Commit

Permalink
New Features
Browse files Browse the repository at this point in the history
Can now delete many items in a single request
Added method rollback for each of Create, Update and Delete
Updated doc
Added contribution information
  • Loading branch information
ezra-obiwale committed Nov 3, 2017
1 parent 1d5b2ff commit dc128c3
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 11 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ trait for its `Web` counterpart, and you're good.
## Documentation

[Get a full walk-through](http://laraquick.ezraobiwale.com)

## Contribution

Contributions are absolutely welcome. Create a PR and I'll as swiftly as possible
merge it up.
7 changes: 6 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ development.

## Helpers

- [Http](/helpers/http)
- [Http](/helpers/http)

## Contribution

Contributions are absolutely welcome. Create a PR and I'll as swiftly as possible
merge it up.
11 changes: 10 additions & 1 deletion docs/controllers/traits/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@ Returns `$data` with response code 202
protected function deleteResponse ( `Illuminate\Database\Eloquent\Model` $data ) : `Illuminate\Http\Response`
</p>

Returns `$data` with response code 202
Returns `$data` with response code 202

### deleteManyResponse ( `...` )

<p class="tip no-bg">
protected function deleteManyResponse ( `integer` $deletedCount ) : `Illuminate\Http\Response`
</p>

Returns a message bearing the number of successfully deleted item.
The response code is 202.
45 changes: 45 additions & 0 deletions docs/controllers/traits/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ can be manipulated as it is passed by reference.

If the method returns anything not equivalent to `null`, it becomes the sent response.

### rollbackCreate ( )

<p class="tip no-bg">
protected function rollbackCreate( ) : `null`
</p>

Called when there's an issue with creating a resource.

### beforeCreateResponse ( `...` )

<p class="tip no-bg">
Expand All @@ -318,6 +326,14 @@ manipulated as it is passed by reference.

If the method returns anything not equivalent to `null`, it becomes the sent response.

### rollbackUpdate ( )

<p class="tip no-bg">
protected function rollbackUpdate( ) : `null`
</p>

Called when there's an issue with updating a resource.

### beforeUpdateResponse ( `...` )

<p class="tip no-bg">
Expand All @@ -339,6 +355,24 @@ Called before a delete/destory operation. Parameter `$data` is the model to be d

If the method returns anything not equivalent to `null`, it becomes the sent response.

### beforeDeleteMany ( `...` )

<p class="tip no-bg">
protected function beforeDeleteMany( `array` &$data) : `Illuminate\Http\Response | null`
</p>

Called before a delete/destory many operation. Parameter `$data` request data.

If the method returns anything not equivalent to `null`, it becomes the sent response.

### rollbackDelete ( )

<p class="tip no-bg">
protected function rollbackDelete( ) : `null`
</p>

Called when there's an issue with deleting a resource.

### beforeDeleteResponse ( `...` )

<p class="tip no-bg">
Expand All @@ -350,6 +384,17 @@ is the delete model.

If the method returns anything not equivalent to `null`, it becomes the sent response.

### beforeDeleteManyResponse ( `...` )

<p class="tip no-bg">
protected function beforeDeleteManyResponse(`integer` $deletedCount) : `Illuminate\Http\Response | null`
</p>

Called before the success response on the delete many operation is sent. Parameter `$deletedCount`
is the number of items successfully deleted.

If the method returns anything not equivalent to `null`, it becomes the sent response.

### defaultPaginationLength ( )

<p class="tip no-bg">
Expand Down
8 changes: 8 additions & 0 deletions docs/controllers/traits/web.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ Redirects to the previous page with a status message.

Redirects to the previous page with a status message.

### deleteManyResponse ( `...` )

<p class="tip no-bg">
protected function deleteManyResponse ( `integer` $data ) : `Illuminate\Http\RedirectResponse`
</p>

Redirects to the previous page with a status message bearing the number of successfully delete items.

## Overriden Methods

### error ( `...` )
Expand Down
9 changes: 8 additions & 1 deletion src/Controllers/Traits/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use DB;

/**
* A colletion of methods to assist in quick controller generation
* A collection of methods to assist in quick controller generation
*
*/
trait Api
Expand Down Expand Up @@ -37,4 +37,11 @@ protected function deleteResponse(Model $data)
{
return response()->json($data, 202);
}

protected function deleteManyResponse($deletedCount)
{
return response()->json([
"message" => "$deletedCount item(s) deleted successfully"
], 202);
}
}
120 changes: 112 additions & 8 deletions src/Controllers/Traits/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait Crud
abstract protected function model();

/**
* Should return the validation rules
* Should return the validation rules for when using @see store() and @see update().
*
* @param array $data The data being validated
* @param mixed $id Id of the model being updated, if such were the case
Expand Down Expand Up @@ -81,6 +81,14 @@ abstract protected function updateResponse(Model $data);
*/
abstract protected function deleteResponse(Model $data);

/**
* Called for the response to method deleteMany()
*
* @param integer $deletedCount
* @return Response|array
*/
abstract protected function deleteManyResponse($deleteCount);

/**
* Indicates whether validation should be strict and throw errors if unwanted
* values exists
Expand Down Expand Up @@ -131,7 +139,8 @@ protected function indexModel()
*
* @return mixed
*/
protected function searchModel($query) {
protected function searchModel($query)
{
return $this->indexModel();
}

Expand All @@ -140,7 +149,8 @@ protected function searchModel($query) {
*
* @return string
*/
protected function searchQueryParam() {
protected function searchQueryParam()
{
return 'query';
}

Expand All @@ -149,7 +159,8 @@ protected function searchQueryParam() {
*
* @return string
*/
protected function sortParam() {
protected function sortParam()
{
return 'sort';
}

Expand Down Expand Up @@ -223,6 +234,15 @@ protected function beforeCreate(array &$data)
{
}

/**
* Called when an error occurs in a create operation
*
* @return void
*/
protected function rollbackCreate()
{
}

/**
* Called on success but before sending the response
*
Expand All @@ -233,6 +253,16 @@ protected function beforeCreateResponse(Model &$data)
{
}

/**
* Called on success but before sending the response
*
* @param mixed $data
* @return mixed The response to send or null
*/
protected function beforeCreateManyResponse(Model &$data)
{
}

/**
* Called when the model is found but before sending the response
*
Expand All @@ -253,6 +283,15 @@ protected function beforeUpdate(array &$data)
{
}

/**
* Called when an error occurs in a update operation
*
* @return void
*/
protected function rollbackUpdate()
{
}

/**
* Called on success but before sending the response
*
Expand All @@ -273,6 +312,15 @@ protected function beforeDelete(Model &$data)
{
}

/**
* Called when an error occurs in a delete operation
*
* @return void
*/
protected function rollbackDelete()
{
}

/**
* Called on success but before sending the response
*
Expand All @@ -283,22 +331,43 @@ protected function beforeDeleteResponse(Model &$data)
{
}

/**
* Called when the model has been found but before deleting
*
* @param mixed $data
* @return void
*/
protected function beforeDeleteMany(array &$data)
{
}

/**
* Called on success but before sending the response
*
* @param integer $deleteCount
* @return mixed The response to send or null
*/
protected function beforeDeleteManyResponse($deletedCount)
{
}

/**
* Applies sort to the given model based on the given string
*
* @param string $string Format is column:direction,column:direction
* @param string $model
* @return string
*/
private function sort($string, $model) {
private function sort($string, $model)
{
$isObject = is_object($model);
foreach (explode(',', $string) as $sorter) {
$sorter = trim($sorter);
if (!$sorter) continue;

$parts = explode(':', $sorter);
$col = trim($parts[0]);
$dir = count($parts) > 1
$dir = count($parts) > 1
? trim($parts[1])
: 'asc';

Expand Down Expand Up @@ -352,14 +421,15 @@ public function store(Request $request)

$model = $this->storeModel();

DB::beginTransaction();
if ($resp = $this->beforeCreate($data)) return $resp;

DB::beginTransaction();
$data = is_object($model)
? $model->create($data)
: $model::create($data);

if (!$data) {
$this->rollbackCreate();
DB::rollback();
return $this->createFailedError();
}
Expand Down Expand Up @@ -409,12 +479,13 @@ public function update(Request $request, $id)
: $model::find($id);
if (!$item) return $this->notFoundError();

DB::beginTransaction();
if ($resp = $this->beforeUpdate($data)) return $resp;

DB::beginTransaction();
$result = $item->update($data);

if (!$result) {
$this->rollbackUpdate();
DB::rollback();
return $this->updateFailedError();
}
Expand All @@ -440,10 +511,12 @@ public function destroy($id)

if (!$item) return $this->notFoundError();

DB::beginTransaction();
$this->beforeDelete($item);
$result = $item->delete();

if (!$result) {
$this->rollbackDelete();
DB::rollback();
return $this->deleteFailedError();
}
Expand All @@ -454,4 +527,35 @@ public function destroy($id)
DB::commit();
return $this->deleteResponse($item);
}

/**
* Remove the specified resource from storage.
* @param int $id
* @return Response
*/
public function destroyMany(Request $request)
{
$model = $this->model();
$data = $request->all();
if (!array_key_exists('ids', $data)) {
throw new \Exception('Ids not found');
}
DB::beginTransaction();
$this->beforeDeleteMany($data);
$result = is_object($model)
? $model->whereIn('id', $data['ids'])->delete()
: $model::whereIn('id', $data['ids'])->delete();

if (!$result) {
$this->rollbackDelete();
DB::rollback();
return $this->deleteFailedError();
}

if ($resp = $this->beforeDeleteManyResponse($result)) {
return $resp;
}
DB::commit();
return $this->deleteManyResponse($result);
}
}
Loading

0 comments on commit dc128c3

Please sign in to comment.