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
public function index(Request $request)
{
$id = $request->id;
$user = $this->userRepository->getByid($id);
$user = auth('api')->user()->id;
if ($user->role == User::ROLE_ADMIN) {
$order = $this->orderRepository->paginate($request->page);
$order = $this->orderRepository->getListOrderOfAdmin($request);
} elseif ($user->role == User::ROLE_EDITOR) {
$order = $this->orderRepository->getListOrderOfUser($id);
}
......@@ -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 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Validator;
......
......@@ -128,17 +128,4 @@ public function destroy(Request $request)
'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 @@
use App\Models\Order;
use App\Repositories\BaseRepository;
use Illuminate\Http\Request;
class OrderRepository extends BaseRepository
{
......@@ -23,15 +24,12 @@ public function getFulfilledOrder()
$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) {
$query->where('id', $id);
})->paginate();
return $this->model->latest()->where('status', $request->status)->paginate();
}
public function getListOrderByStatus($status)
public function getListOrderOfUser($id)
{
return $order = $this->model->where('status', $status)->paginate();
return $this->model->where('user_id', $id)->with('user')->paginate();
}
}
......@@ -35,6 +35,5 @@
'middleware' => 'api',
], function ($router) {
Route::get('orders/search', [App\Http\Controllers\Api\OrderController::class, 'search']);
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