Commit 9891271d authored by 20194288-huannh's avatar 20194288-huannh

update code OrderController after review part 2

parent fd057283
...@@ -25,16 +25,8 @@ public function __construct(OrderRepository $orderRepository) ...@@ -25,16 +25,8 @@ public function __construct(OrderRepository $orderRepository)
public function index(GetOrderRequest $request) public function index(GetOrderRequest $request)
{ {
$user = auth('api')->user()->id; $user = auth('api')->user()->id;
switch ($user->role) { $order = $user->role == User::ROLE_ADMIN ? $this->orderRepository->getListOrderOfAdmin($request->all())
case User::ROLE_ADMIN: : $this->orderRepository->getListOrderOfUser($user);
$order = $this->orderRepository->getListOrderOfAdmin($request->status);
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 +102,9 @@ public function update(Request $request, $id) ...@@ -110,8 +102,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 +118,9 @@ public function destroy(Request $request) ...@@ -125,8 +118,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
]); ]);
} }
} }
...@@ -27,7 +27,7 @@ public function index(GetOrderRequest $request) ...@@ -27,7 +27,7 @@ public function index(GetOrderRequest $request)
$user = auth('api')->user()->id; $user = auth('api')->user()->id;
switch ($user->role) { switch ($user->role) {
case User::ROLE_ADMIN: case User::ROLE_ADMIN:
$order = $this->orderRepository->getListOrderOfAdmin($request->status); $order = $this->orderRepository->getListOrderOfAdmin($request->all());
break; break;
case User::ROLE_EDITOR: case User::ROLE_EDITOR:
$order = $this->orderRepository->getListOrderOfUser($user); $order = $this->orderRepository->getListOrderOfUser($user);
......
...@@ -24,7 +24,7 @@ public function authorize() ...@@ -24,7 +24,7 @@ public function authorize()
public function rules() public function rules()
{ {
return [ return [
'status' => 'required|integer' 'status' => 'integer'
]; ];
} }
} }
...@@ -23,9 +23,13 @@ public function getFulfilledOrder() ...@@ -23,9 +23,13 @@ public function getFulfilledOrder()
$this->model->where('is_fulfilled', true); $this->model->where('is_fulfilled', true);
} }
public function getListOrderOfAdmin($status) public function getListOrderOfAdmin(array $params)
{ {
return $this->model->latest()->where('status', $status)->paginate(); $query = $this->model;
foreach ($params as $key => $value) {
$query = $query->where($key, $value);
}
return $query->paginate();
} }
public function getListOrderOfUser($id) public function getListOrderOfUser($id)
{ {
......
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