Commit d5f42690 authored by 20194288-huannh's avatar 20194288-huannh

update index function in OrderController

parent 9891271d
......@@ -24,9 +24,10 @@ public function __construct(OrderRepository $orderRepository)
}
public function index(GetOrderRequest $request)
{
$user = auth('api')->user()->id;
$order = $user->role == User::ROLE_ADMIN ? $this->orderRepository->getListOrderOfAdmin($request->all())
: $this->orderRepository->getListOrderOfUser($user);
$user = auth('api')->user();
$order = $user->role == User::ROLE_ADMIN ?
$this->orderRepository->getListOrder($request->all())->paginate()
: $this->orderRepository->getListOrderOfUser($user->id, $request->all());
return response()->json([
'success' => true,
'meta' => [
......
......@@ -24,17 +24,10 @@ public function __construct(OrderRepository $orderRepository)
}
public function index(GetOrderRequest $request)
{
$user = auth('api')->user()->id;
switch ($user->role) {
case User::ROLE_ADMIN:
$order = $this->orderRepository->getListOrderOfAdmin($request->all());
break;
case User::ROLE_EDITOR:
$order = $this->orderRepository->getListOrderOfUser($user);
break;
default:
$order = collect([]);
}
$user = auth('api')->user();
$order = $user->role == User::ROLE_ADMIN ?
$this->orderRepository->getListOrder($request->all())->paginate()
: $this->orderRepository->getListOrderOfUser($user->id, $request->all());
return response()->json([
'success' => true,
'meta' => [
......@@ -110,8 +103,9 @@ public function update(Request $request, $id)
$request->only(['id', 'details', 'client', 'is_fulfilled'])
);
return response()->json([
'success' => $is_update ? true : false,
'message' => ''
'success' => true,
'message' => '',
'data' => $is_update
]);
}
......@@ -125,8 +119,9 @@ public function destroy(Request $request)
{
$is_deleted = $this->orderRepository->deleteById($request->id);
return response()->json([
'success' => $is_deleted ? true : false,
'message' => ''
'success' => true,
'message' => '',
'data' => $is_deleted
]);
}
}
......@@ -4,6 +4,7 @@
use App\Models\Order;
use App\Repositories\BaseRepository;
use Log;
class OrderRepository extends BaseRepository
{
......@@ -23,16 +24,28 @@ public function getFulfilledOrder()
$this->model->where('is_fulfilled', true);
}
public function getListOrderOfAdmin(array $params)
public function getListOrder(array $params)
{
$query = $this->model;
foreach ($params as $key => $value) {
$query = $query->where($key, $value);
if (isset($params['status'])) {
$query = $query->where('status', $params['status']);
}
return $query->paginate();
if (isset($params['keyword'])) {
$keyword = $params['keyword'];
$query = $query->where(function ($query) use ($keyword) {
$query->where('details', 'LIKE', '%' . $keyword . '%')
->orWhere('client', 'LIKE', '%' . $keyword . '%')
->orWhere(function ($query) use ($keyword) {
$query->whereHas('user', function ($query) use ($keyword) {
$query->where('name', 'LIKE', '%' . $keyword . '%');
});
});
});
}
return $query;
}
public function getListOrderOfUser($id)
public function getListOrderOfUser($id, array $params = [])
{
return $this->model->where('user_id', $id)->with('user')->paginate();
return $this->getListOrder($params)->where('user_id', $id)->paginate();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment