Skip to content

Commit

Permalink
Merge pull request #6 from ezra-obiwale/wip
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
ezra-obiwale authored Jan 3, 2018
2 parents 1ffe1e7 + b8ff393 commit bd5d520
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 deletions.
41 changes: 32 additions & 9 deletions src/Controllers/Traits/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,68 @@ trait Api
{
use Crud;

/**
* Called when an action is successfully processed.
*
* @param mixed $response
* @param integer $code
* @return Response
*/
protected function success($response, $code = 200) {
$resp = [
'status' => array_key_exists('status', $response) ?
$response['status'] : 'ok'
];
if (is_array($response)) {
$resp = array_merge($resp, $response);
}
else if (is_string($response)) {
$resp['message'] = $response;
}
else {
$resp['data'] = $response;
}

return response()->json($resp, $code);
}

protected function indexResponse($data)
{
return $this->paginatedList($data->toArray());
}

protected function storeResponse(Model $data)
{
return response()->json($data, 201);
return $this->success($data, 201);
}

protected function showResponse(Model $data)
{
return response()->json($data, 200);
return $this->success($data, 200);
}

protected function updateResponse(Model $data)
{
return response()->json($data, 202);
return $this->success($data, 202);
}

protected function destroyResponse(Model $data)
{
return response()->json($data, 202);
return $this->success($data, 202);
}

protected function forceDestroyResponse(Model $data)
{
return response()->json($data, 202);
return $this->success($data, 202);
}

protected function destroyManyResponse($deletedCount)
{
return response()->json([
"message" => "$deletedCount item(s) deleted successfully"
], 202);
return $this->success("$deletedCount item(s) deleted successfully", 202);
}

protected function restoreDestroyedResponse(Model $data)
{
return response()->json($data, 202);
return $this->success($data, 202);
}
}
3 changes: 2 additions & 1 deletion src/Controllers/Traits/Respond.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ trait Respond
protected function error($message, $errors = null, $code = 400)
{
$resp = [
"status" => "error",
"message" => $message
];
if ($errors) $resp["errors"] = $errors;
Expand All @@ -40,7 +41,7 @@ protected function paginatedList(array $items, $code = 200) {
$meta['pagination'] = $items;
$resp['meta']['pagination'] = $items;
}
return response()->json($resp, $code);
return $this->success($resp, $code);
}

/**
Expand Down
22 changes: 16 additions & 6 deletions src/Controllers/Traits/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
trait Web
{
use Crud;

/**
* Called when an action is successfully processed.
*
* @param string $message
* @return Response
*/
protected function success($status) {
return back()->withStatus($status);
}

protected function error($message, $errors = null, $code = 400)
{
Expand All @@ -20,31 +30,31 @@ protected function error($message, $errors = null, $code = 400)

protected function storeResponse(Model $data)
{
return back()->withStatus('Create successful');
return $this->success('Create successful');
}

protected function updateResponse(Model $data)
{
return back()->withStatus('Update successful');
return $this->success('Update successful');
}

protected function destroyResponse(Model $data)
{
return back()->withStatus('Delete successful');
return $this->success('Delete successful');
}

protected function forceDestroyResponse(Model $data)
{
return back()->withStatus('Permanent delete successful');
return $this->success('Permanent delete successful');
}

protected function destroyManyResponse($deletedCount)
{
return back()->withStatus("Deleted $deletedCount item(s) successfully");
return $this->success("Deleted $deletedCount item(s) successfully");
}

protected function restoreDestroyedResponse(Model $data)
{
return back()->withStatus('Restoration successful');
return $this->success('Restoration successful');
}
}
24 changes: 24 additions & 0 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Laraquick\Models;

use Illuminate\Database\Eloquent\Model as iModel;

abstract class Model extends iModel
{

public function toArray()
{
$fillable = $this->fillable;
$fillable[] = 'id';
// Show only fillables
$array = collect(parent::toArray())
->only($fillable)
->all();
// Add loaded relations
foreach (array_keys($this->relations) as $relation) {
$array[$relation] = $this->$relation;
}
return $array;
}

}
10 changes: 1 addition & 9 deletions src/Models/WithSoftDeletes.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Laraquick\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

abstract class WithSoftDeletes extends Model
Expand All @@ -10,12 +10,4 @@ abstract class WithSoftDeletes extends Model

protected $dates = ['deleted_at'];

public function toArray()
{
$fillable = $this->fillable;
$fillable[] = 'id';
return collect(parent::toArray())
->only($fillable)
->all();
}
}

0 comments on commit bd5d520

Please sign in to comment.