-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
this edit handle all controller functions #6
Open
habib-eg
wants to merge
2
commits into
Mahmoud-Italy:2.x
Choose a base branch
from
habib-eg:2.x
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,103 @@ | ||
<?php | ||
|
||
namespace DummyNamespace; | ||
|
||
use DummyFullModelClass; | ||
use Illuminate\Http\Request; | ||
use App\Http\Requests\DummyModelClassRequest; | ||
use App\Http\Resources\DummyModelClassResource; | ||
//use Illuminate\Http\Request; | ||
use App\Http\Requests\DummyModelClass\{DummyModelClassStoreRequest,DummyModelClassUpdateRequest}; | ||
use App\Http\Resources\DummyModelClass\{DummyModelClassResource,DummyModelClassCollection}; | ||
use DummyRootNamespaceHttp\Controllers\Controller; | ||
use Spatie\QueryBuilder\QueryBuilder; | ||
use Larafast\Fastapi\Controller\Actions\{Destroy, Index, Show, Store, Update}; | ||
use Exception; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class DummyClass extends Controller | ||
{ | ||
use Index,Update,Destroy,Store,Show; | ||
function __construct() | ||
{ | ||
// $this->middleware('permission:view_'.$DummyModelVariable, ['only' => ['index', 'show', 'export']]); | ||
// $this->middleware('permission:add_'.$DummyModelVariable, ['only' => ['store']]); | ||
// $this->middleware('permission:edit_'.$DummyModelVariable, | ||
// $this->middleware('permission:edit_'.$DummyModelVariable, | ||
// ['only' => ['update', 'active', 'inactive', 'trash', 'restore']]); | ||
// $this->middleware('permission:delete_'.$DummyModelVariable, ['only' => ['destroy']]); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
* @return JsonResponse | ||
*/ | ||
public function index() | ||
{ | ||
$cars = QueryBuilder::for(DummyModelClass::where('active', 1)) | ||
->allowedFilters('') | ||
->defaultSort('') | ||
->allowedSorts('') | ||
->paginate($request->perPage ?? 10); | ||
$allowedFilters = ['id']; | ||
$defaultSort = ['id']; | ||
$allowedSorts = ['id']; | ||
|
||
return response()->json(DummyModelClassResource::collection($cars)->response()->getData(true), 200); | ||
return $this->ApiIndex(DummyModelClass::query(), function ($collection) { | ||
if (request()->wantsJson()) { | ||
return response()->json(DummyModelClassResource::collection($collection)->response()->getData(true), 200); | ||
} | ||
return view('index', compact('collection')); | ||
}, false, $allowedFilters, $defaultSort, $allowedSorts); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param DummyModelClassRequest $request | ||
* @return \Illuminate\Http\JsonResponse | ||
* @param DummyModelClassStoreRequest $request | ||
* @return JsonResponse|mixed | ||
*/ | ||
public function store(DummyModelClassRequest $request) | ||
public function store(DummyModelClassStoreRequest $request) | ||
{ | ||
try { | ||
DummyModelClass::create($request->all()); | ||
return response()->json(['message' => ''], 201); | ||
} catch (\Exception $e) { | ||
return response()->json(['message' => 'Unable to create entry, ' . $e->getMessage()], 500); | ||
} | ||
$validated = $request->validated(); | ||
// handel Upload Image Here Or in Request | ||
return $this->ApiStore(DummyModelClass::class, $validated, function ($model) use ($request) { | ||
if ($request->wantsJson()) { | ||
//handel attached data or anything | ||
return response()->json(new DummyModelClassResource($model), 200); | ||
} | ||
// handel your back response | ||
return back()->with(["message" => "Created Successfully"]); | ||
}); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
* | ||
* @param \DummyFullModelClass $id | ||
* @return \Illuminate\Http\JsonResponse | ||
* @param $DummyModelVariable | ||
* @return DummyModelClassResource|JsonResponse | ||
*/ | ||
public function show($id) | ||
public function show($DummyModelVariable) | ||
{ | ||
$data = new DummyModelClassResource(DummyModelClass::find($id)); | ||
return response()->json(['data' => $row], 200); | ||
try { | ||
return new DummyModelClassResource($this->ApiShow(DummyModelClass::class, $DummyModelVariable)); | ||
} catch (Exception $e) { | ||
return response()->json(['message' => 'Unable to show entry, ' . (config('app.debug', true) ? $e->getMessage() : ' Debug is False ')], 500); | ||
} | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param DummyModelClassRequest $request | ||
* @param \DummyFullModelClass $id | ||
* @return \Illuminate\Http\JsonResponse | ||
* @param DummyModelClassUpdateRequest $request | ||
* @param $DummyModelVariable | ||
* @return JsonResponse|mixed | ||
*/ | ||
public function update(DummyModelClassRequest $request, $id) | ||
public function update(DummyModelClassUpdateRequest $request, $DummyModelVariable) | ||
{ | ||
try { | ||
DummyModelClass::find($id)->update($request->all()); | ||
return response()->json(['message' => ''], 200); | ||
} catch (\Exception $e) { | ||
return response()->json(['message' => 'Unable to update entry, ' . $e->getMessage()], 500); | ||
} | ||
$model = $this->ApiShow(DummyModelClass::class, $DummyModelVariable); | ||
$validated = $request->validated(); | ||
// handel Upload Image Here Or in Request | ||
return $this->ApiUpdate($model, $validated, function ($model, $updatedResponse) use ($request) { | ||
if ($request->wantsJson()) { | ||
return response()->json(new DummyModelClassResource($model), 200); | ||
} | ||
// handel your back response | ||
return back()->with(["message" => "Updated Successfully"]); | ||
}); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param \DummyFullModelClass $id | ||
* @return \Illuminate\Http\JsonResponse | ||
* @param $DummyModelVariable | ||
* @return JsonResponse | ||
*/ | ||
public function destroy($id) | ||
public function destroy($DummyModelVariable) | ||
{ | ||
try { | ||
$DummyModelVariable->find($id)->delete(); | ||
return response()->json(['message' => ''], 200); | ||
} catch (\Exception $e) { | ||
return response()->json(['message' => 'Unable to delete entry, ' . $e->getMessage()], 500); | ||
} | ||
$model = $this->ApiShow(DummyModelClass::class, $DummyModelVariable); | ||
|
||
return $this->ApiDestroy($model); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
namespace Larafast\Fastapi\Controller\Actions; | ||
|
||
use Closure; | ||
|
||
trait Destroy | ||
{ | ||
|
||
/** | ||
* @param $model | ||
* @param null $reponseSuccess | ||
* @param null $reponseError | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function ApiDestroy($model, $reponseSuccess = null, $reponseError = null) | ||
{ | ||
try { | ||
$deleted = $model->delete(); | ||
if ($reponseSuccess instanceof Closure) { | ||
return $reponseSuccess($deleted); | ||
} | ||
return response()->json(['message' => 'deleted successfully'], 200); | ||
} catch (\Exception $e) { | ||
if ($reponseError instanceof Closure) { | ||
return $reponseError($e); | ||
} | ||
return response()->json(['message' => 'Unable to delete entry, ' . (config('app.debug',true)?$e->getMessage():' Debug is False ')], 500); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Larafast\Fastapi\Controller\Actions; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Http\JsonResponse; | ||
use Spatie\QueryBuilder\QueryBuilder; | ||
|
||
trait Index | ||
{ | ||
/** | ||
* @param string $model | ||
* @param string $collection | ||
* @param bool $getAll | ||
* @param array|string[] $allowedFilters | ||
* @param array|string[] $defaultSort | ||
* @param array|string[] $allowedSorts | ||
* @return JsonResponse | ||
*/ | ||
public function ApiIndex(Builder $model, $callback, bool $getAll = false, array $allowedFilters = [], array $defaultSort = [], array $allowedSorts = []) | ||
{ | ||
$rows = QueryBuilder::for($model) | ||
->allowedFilters($allowedFilters) | ||
->defaultSort($defaultSort) | ||
->allowedSorts($allowedSorts); | ||
|
||
$data = ($getAll) ? $rows->get() : $rows->paginate(request()->get('perPage', 15)); | ||
|
||
if ($callback instanceof Closure) { | ||
return $callback($data); | ||
} | ||
|
||
return response()->json(['data'=>$data], 200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
|
||
namespace Larafast\Fastapi\Controller\Actions; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
|
||
trait Show | ||
{ | ||
public function ApiShow($model, $parameter, $reponseSuccess = null, $reponseError = null) | ||
{ | ||
if ($data = (new $model)->resolveRouteBinding($parameter)) { | ||
return $reponseSuccess instanceof Closure ? $reponseSuccess($data) : $data; | ||
} | ||
|
||
if ($reponseError instanceof Closure) { | ||
return $reponseError(new ModelNotFoundException()); | ||
} | ||
|
||
return abort(404); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good one, you can also make it shorter by using only the
request
method: