Skip to content
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
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class BlogController extends Controller
->allowedFilters('')
->defaultSort('')
->allowedSorts('')
->paginate($request->perPage ?? 10);
->paginate(request()->get('perPage',10);
Copy link
Contributor

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:

request('per_page', 10);


return response()->json(BlogResource::collection($rows)->response()->getData(true), 200);

Expand Down Expand Up @@ -113,10 +113,10 @@ class BlogController extends Controller
/**
* Display the specified resource.
*
* @param \App\Blog $blog
* @param $blog
* @return \Illuminate\Http\Response
*/
public function show(Blog $blog)
public function show($blog)
{
$row = new BlogResource(Blog::findOrFail($blog));
return response()->json(['row' => $row], 200);
Expand All @@ -125,7 +125,7 @@ class BlogController extends Controller
/**
* Update the specified resource in storage.
*
* @param OrderUpdateRequest $request
* @param BlogUpdateRequest $request
* @param \App\Blog $blog
* @return \Illuminate\Http\Response
*/
Expand Down
118 changes: 61 additions & 57 deletions resources/stubs/controller.api.stub
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);
}
}
44 changes: 37 additions & 7 deletions src/Console/Commands/Fastapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ public function handle()

$this->type = 'Request';

$this->createRequest();
$this->createRequest(true);

$this->createResource();
$this->createCollection();
}

/**
Expand Down Expand Up @@ -123,7 +124,7 @@ protected function createController()
$modelName = $this->qualifyClass($this->getNameInput());

$args = [
'name' => "{$controller}Controller",
'name' => "{$controller}\\{$controller}Controller",
'--model' => $modelName,
];

Expand All @@ -148,16 +149,33 @@ protected function createController()
*
* @return void
*/
protected function createRequest()
protected function createRequest(bool $createBoth=false)
{
$model = Str::studly(class_basename($this->argument('name')));
$name = "{$model}Request";
$this->call('fastApi:request', [
$basename="{$model}\\{$model}";
if ($createBoth) {

// Create Request
$create = "{$basename}StoreRequest";
$this->callRequest($create,$model);

// Update Request
$update = "{$basename}UpdateRequest";
$this->callRequest($update,$model);

}else{
$name = "{$basename}Request";
$this->callRequest($name,$model);
}
}

public function callRequest($name,$model)
{
return $this->call('fastApi:request', [
'name' => $name,
'--model' => $model,
]);
}

/**
* Get the stub file for the generator.
*
Expand Down Expand Up @@ -196,8 +214,20 @@ protected function createResource()
$resource = Str::studly(class_basename($this->argument('name')));

$this->call('fastApi:resource', [
'name' => "{$resource}Resource",
'name' => "$resource\\{$resource}Resource",
'--model' => $this->argument('name'),
]);
}
/**
* Create a model resource for the model.
*
* @return void
*/
protected function createCollection()
{
$collection = Str::studly(class_basename($this->argument('name')));
$this->call('make:resource', [
'name' => "{$collection}\\{$collection}Collection",
]);
}
}
7 changes: 3 additions & 4 deletions src/Console/Commands/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ public function handle()
return;
}
if ($this->option('all')) {
$this->input->setOption('factory', true);
$this->input->setOption('migration', true);
$this->input->setOption('controller', true);
$this->input->setOption('resource', true);
foreach (['factory','migration','controller','resource'] as $item) {
$this->input->setOption($item, true);
}
}

if ($this->option('factory')) {
Expand Down
31 changes: 31 additions & 0 deletions src/Controller/Actions/Destroy.php
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);
}
}

}
36 changes: 36 additions & 0 deletions src/Controller/Actions/Index.php
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);
}
}
24 changes: 24 additions & 0 deletions src/Controller/Actions/Show.php
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);
}

}
Loading