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) ...@@ -24,9 +24,10 @@ public function __construct(OrderRepository $orderRepository)
} }
public function index(GetOrderRequest $request) public function index(GetOrderRequest $request)
{ {
$user = auth('api')->user()->id; $user = auth('api')->user();
$order = $user->role == User::ROLE_ADMIN ? $this->orderRepository->getListOrderOfAdmin($request->all()) $order = $user->role == User::ROLE_ADMIN ?
: $this->orderRepository->getListOrderOfUser($user); $this->orderRepository->getListOrder($request->all())->paginate()
: $this->orderRepository->getListOrderOfUser($user->id, $request->all());
return response()->json([ return response()->json([
'success' => true, 'success' => true,
'meta' => [ 'meta' => [
......
...@@ -24,17 +24,10 @@ public function __construct(OrderRepository $orderRepository) ...@@ -24,17 +24,10 @@ public function __construct(OrderRepository $orderRepository)
} }
public function index(GetOrderRequest $request) public function index(GetOrderRequest $request)
{ {
$user = auth('api')->user()->id; $user = auth('api')->user();
switch ($user->role) { $order = $user->role == User::ROLE_ADMIN ?
case User::ROLE_ADMIN: $this->orderRepository->getListOrder($request->all())->paginate()
$order = $this->orderRepository->getListOrderOfAdmin($request->all()); : $this->orderRepository->getListOrderOfUser($user->id, $request->all());
break;
case User::ROLE_EDITOR:
$order = $this->orderRepository->getListOrderOfUser($user);
break;
default:
$order = collect([]);
}
return response()->json([ return response()->json([
'success' => true, 'success' => true,
'meta' => [ 'meta' => [
...@@ -110,8 +103,9 @@ public function update(Request $request, $id) ...@@ -110,8 +103,9 @@ public function update(Request $request, $id)
$request->only(['id', 'details', 'client', 'is_fulfilled']) $request->only(['id', 'details', 'client', 'is_fulfilled'])
); );
return response()->json([ return response()->json([
'success' => $is_update ? true : false, 'success' => true,
'message' => '' 'message' => '',
'data' => $is_update
]); ]);
} }
...@@ -125,8 +119,9 @@ public function destroy(Request $request) ...@@ -125,8 +119,9 @@ public function destroy(Request $request)
{ {
$is_deleted = $this->orderRepository->deleteById($request->id); $is_deleted = $this->orderRepository->deleteById($request->id);
return response()->json([ return response()->json([
'success' => $is_deleted ? true : false, 'success' => true,
'message' => '' 'message' => '',
'data' => $is_deleted
]); ]);
} }
} }
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
use App\Models\Order; use App\Models\Order;
use App\Repositories\BaseRepository; use App\Repositories\BaseRepository;
use Log;
class OrderRepository extends BaseRepository class OrderRepository extends BaseRepository
{ {
...@@ -23,16 +24,28 @@ public function getFulfilledOrder() ...@@ -23,16 +24,28 @@ public function getFulfilledOrder()
$this->model->where('is_fulfilled', true); $this->model->where('is_fulfilled', true);
} }
public function getListOrderOfAdmin(array $params) public function getListOrder(array $params)
{ {
$query = $this->model; $query = $this->model;
foreach ($params as $key => $value) { if (isset($params['status'])) {
$query = $query->where($key, $value); $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