Commit 2057d743 authored by 20194288-huannh's avatar 20194288-huannh

update OrderController

parent b51f5c0a
...@@ -26,9 +26,9 @@ public function __construct(OrderRepository $orderRepository, UserRepository $us ...@@ -26,9 +26,9 @@ public function __construct(OrderRepository $orderRepository, UserRepository $us
public function index(Request $request) public function index(Request $request)
{ {
$id = $request->id; $id = $request->id;
$user = $this->userRepository->getByid($id); $user = auth('api')->user()->id;
if ($user->role == User::ROLE_ADMIN) { if ($user->role == User::ROLE_ADMIN) {
$order = $this->orderRepository->paginate($request->page); $order = $this->orderRepository->getListOrderOfAdmin($request);
} elseif ($user->role == User::ROLE_EDITOR) { } elseif ($user->role == User::ROLE_EDITOR) {
$order = $this->orderRepository->getListOrderOfUser($id); $order = $this->orderRepository->getListOrderOfUser($id);
} }
...@@ -129,16 +129,4 @@ public function destroy(Request $request) ...@@ -129,16 +129,4 @@ public function destroy(Request $request)
]); ]);
} }
public function search(Request $request)
{
$order = $this->orderRepository->getListOrderByStatus($request->status);
return response()->json([
'success' => true,
'meta' => [
'total' => $order->total(),
'pages' => $order->lastPage()
],
'data' => OrderResource::collection($order)
]);
}
} }
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use App\Models\User; use App\Models\User;
use Validator; use Validator;
......
...@@ -128,17 +128,4 @@ public function destroy(Request $request) ...@@ -128,17 +128,4 @@ public function destroy(Request $request)
'data' => $order 'data' => $order
]); ]);
} }
public function search(Request $request)
{
$order = $this->orderRepository->getListOrderByStatus($request->status);
return response()->json([
'success' => true,
'meta' => [
'total' => $order->total(),
'pages' => $order->lastPage()
],
'data' => OrderResource::collection($order)
]);
}
} }
...@@ -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 Illuminate\Http\Request;
class OrderRepository extends BaseRepository class OrderRepository extends BaseRepository
{ {
...@@ -23,15 +24,12 @@ public function getFulfilledOrder() ...@@ -23,15 +24,12 @@ public function getFulfilledOrder()
$this->model->where('is_fulfilled', true); $this->model->where('is_fulfilled', true);
} }
public function getListOrderOfUser($id) public function getListOrderOfAdmin(Request $request)
{ {
return $order = $this->model->with('user')->whereHas('user', function ($query) use ($id) { return $this->model->latest()->where('status', $request->status)->paginate();
$query->where('id', $id);
})->paginate();
} }
public function getListOrderOfUser($id)
public function getListOrderByStatus($status)
{ {
return $order = $this->model->where('status', $status)->paginate(); return $this->model->where('user_id', $id)->with('user')->paginate();
} }
} }
...@@ -35,6 +35,5 @@ ...@@ -35,6 +35,5 @@
'middleware' => 'api', 'middleware' => 'api',
], function ($router) { ], function ($router) {
Route::get('orders/search', [App\Http\Controllers\Api\OrderController::class, 'search']);
Route::resource('orders', App\Http\Controllers\Api\OrderController::class); Route::resource('orders', App\Http\Controllers\Api\OrderController::class);
}); });
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