117 lines
2.7 KiB
PHP
117 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Webkul\API\Http\Controllers\Shop;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* Resource Controller
|
|
*
|
|
* @author Jitendra Singh <jitendra@webkul.com>
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
*/
|
|
class ResourceController extends Controller
|
|
{
|
|
/**
|
|
* Contains current guard
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $guard;
|
|
|
|
/**
|
|
* Contains route related configuration
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $_config;
|
|
|
|
/**
|
|
* Repository object
|
|
*
|
|
* @var \Webkul\Core\Eloquent\Repository
|
|
*/
|
|
protected $repository;
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->guard = request()->has('token') ? 'api' : 'customer';
|
|
|
|
$this->_config = request('_config');
|
|
|
|
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
|
|
|
|
auth()->setDefaultDriver($this->guard);
|
|
|
|
$this->middleware('auth:' . $this->guard);
|
|
}
|
|
|
|
$this->repository = app($this->_config['repository']);
|
|
}
|
|
|
|
/**
|
|
* Returns a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$query = $this->repository->scopeQuery(function($query) {
|
|
foreach (request()->except(['page', 'limit', 'pagination', 'sort', 'order', 'token']) as $input => $value) {
|
|
$query = $query->whereIn($input, array_map('trim', explode(',', $value)));
|
|
}
|
|
|
|
if ($sort = request()->input('sort')) {
|
|
$query = $query->orderBy($sort, request()->input('order') ?? 'desc');
|
|
} else {
|
|
$query = $query->orderBy('id', 'desc');
|
|
}
|
|
|
|
return $query;
|
|
});
|
|
|
|
if (is_null(request()->input('pagination')) || request()->input('pagination')) {
|
|
$results = $query->paginate(request()->input('limit') ?? 10);
|
|
} else {
|
|
$results = $query->get();
|
|
}
|
|
|
|
return $this->_config['resource']::collection($results);
|
|
}
|
|
|
|
/**
|
|
* Returns a individual resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function get($id)
|
|
{
|
|
return new $this->_config['resource'](
|
|
$this->repository->findOrFail($id)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Delete's a individual resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$wishlistProduct = $this->repository->findOrFail($id);
|
|
|
|
$this->repository->delete($id);
|
|
|
|
return response()->json([
|
|
'message' => 'Item removed successfully.',
|
|
]);
|
|
}
|
|
}
|