sarga/packages/Webkul/API/Http/Controllers/Shop/ResourceController.php

119 lines
3.0 KiB
PHP
Raw Normal View History

2019-03-13 11:17:52 +00:00
<?php
namespace Webkul\API\Http\Controllers\Shop;
use Illuminate\Http\Request;
class ResourceController extends Controller
{
2019-05-07 11:36:21 +00:00
/**
* Contains current guard
*
* @var array
*/
protected $guard;
2019-03-13 11:17:52 +00:00
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* Repository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Eloquent\Repository
2019-03-13 11:17:52 +00:00
*/
protected $repository;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
2019-05-07 11:36:21 +00:00
$this->guard = request()->has('token') ? 'api' : 'customer';
2019-03-13 11:17:52 +00:00
$this->_config = request('_config');
2019-05-07 11:36:21 +00:00
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
auth()->setDefaultDriver($this->guard);
$this->middleware('auth:' . $this->guard);
}
if ($this->_config) {
$this->repository = app($this->_config['repository']);
}
2019-03-13 11:17:52 +00:00
}
/**
* Returns a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$query = $this->repository->scopeQuery(function($query) {
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
$query = $query->where('customer_id', auth()->user()->id );
}
2019-05-07 11:36:21 +00:00
foreach (request()->except(['page', 'limit', 'pagination', 'sort', 'order', 'token']) as $input => $value) {
$query = $query->whereIn($input, array_map('trim', explode(',', $value)));
2019-03-13 11:17:52 +00:00
}
2019-03-15 05:06:27 +00:00
if ($sort = request()->input('sort')) {
$query = $query->orderBy($sort, request()->input('order') ?? 'desc');
2019-05-07 11:36:21 +00:00
} else {
$query = $query->orderBy('id', 'desc');
2019-03-15 05:06:27 +00:00
}
2019-03-13 11:17:52 +00:00
return $query;
});
2019-03-15 13:30:44 +00:00
if (is_null(request()->input('pagination')) || request()->input('pagination')) {
$results = $query->paginate(request()->input('limit') ?? 10);
} else {
$results = $query->get();
}
2019-03-13 11:17:52 +00:00
return $this->_config['resource']::collection($results);
}
/**
* Returns a individual resource.
*
2020-03-05 05:34:57 +00:00
* @param int $id
2019-03-13 11:17:52 +00:00
* @return \Illuminate\Http\Response
*/
public function get($id)
{
$query = isset($this->_config['authorization_required']) && $this->_config['authorization_required'] ?
$this->repository->where('customer_id', auth()->user()->id)->findOrFail($id) :
$this->repository->findOrFail($id);
return new $this->_config['resource']($query);
2019-03-13 11:17:52 +00:00
}
2019-05-07 11:36:21 +00:00
/**
* Delete's a individual resource.
*
2020-03-05 05:34:57 +00:00
* @param int $id
2019-05-07 11:36:21 +00:00
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$wishlistProduct = $this->repository->findOrFail($id);
$this->repository->delete($id);
2019-05-07 11:36:21 +00:00
return response()->json([
2020-03-04 06:32:53 +00:00
'message' => 'Item removed successfully.',
2020-02-27 08:03:03 +00:00
]);
2019-05-07 11:36:21 +00:00
}
2019-03-13 11:17:52 +00:00
}