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

update code OrderController after review

parent 2057d743
...@@ -2,35 +2,38 @@ ...@@ -2,35 +2,38 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Resources\OrderResource; use App\Http\Resources\OrderResource;
use App\Repositories\OrderRepository; use App\Repositories\OrderRepository;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Repositories\UserRepository; use App\Repositories\UserRepository;
use App\Http\Requests\GetOrderRequest;
use Illuminate\Http\Request;
use App\Models\User; use App\Models\User;
class OrderController extends Controller class OrderController extends Controller
{ {
private OrderRepository $orderRepository; private OrderRepository $orderRepository;
private UserRepository $userRepository;
/** /**
* Display a listing of the resource. * Display a listing of the resource.
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function __construct(OrderRepository $orderRepository, UserRepository $userRepository) public function __construct(OrderRepository $orderRepository)
{ {
$this->orderRepository = $orderRepository; $this->orderRepository = $orderRepository;
$this->userRepository = $userRepository;
} }
public function index(Request $request) public function index(GetOrderRequest $request)
{ {
$id = $request->id;
$user = auth('api')->user()->id; $user = auth('api')->user()->id;
if ($user->role == User::ROLE_ADMIN) { switch ($user->role) {
$order = $this->orderRepository->getListOrderOfAdmin($request); case User::ROLE_ADMIN:
} elseif ($user->role == User::ROLE_EDITOR) { $order = $this->orderRepository->getListOrderOfAdmin($request->status);
$order = $this->orderRepository->getListOrderOfUser($id); break;
case User::ROLE_EDITOR:
$order = $this->orderRepository->getListOrderOfUser($user);
break;
default:
$order = collect([]);
} }
return response()->json([ return response()->json([
'success' => true, 'success' => true,
...@@ -102,14 +105,13 @@ public function edit(Request $request) ...@@ -102,14 +105,13 @@ public function edit(Request $request)
*/ */
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
$order = $this->orderRepository->update( $is_update = $this->orderRepository->update(
$request->id, $request->id,
$request->only(['id', 'details', 'client', 'is_fulfilled']) $request->only(['id', 'details', 'client', 'is_fulfilled'])
); );
return response()->json([ return response()->json([
'success' => true, 'success' => $is_update ? true : false,
'message' => '', 'message' => ''
'data' => $order
]); ]);
} }
...@@ -121,12 +123,10 @@ public function update(Request $request, $id) ...@@ -121,12 +123,10 @@ public function update(Request $request, $id)
*/ */
public function destroy(Request $request) public function destroy(Request $request)
{ {
$order = $this->orderRepository->deleteById($request->id); $is_deleted = $this->orderRepository->deleteById($request->id);
return response()->json([ return response()->json([
'success' => true, 'success' => $is_deleted ? true : false,
'message' => '', 'message' => ''
'data' => $order
]); ]);
} }
} }
<?php <?php
namespace App\Http\Controllers; namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Resources\OrderResource; use App\Http\Resources\OrderResource;
use App\Repositories\OrderRepository; use App\Repositories\OrderRepository;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Repositories\UserRepository; use App\Repositories\UserRepository;
use App\Http\Requests\GetOrderRequest;
use Illuminate\Http\Request;
use App\Models\User; use App\Models\User;
class OrderController extends Controller class OrderController extends Controller
{ {
private OrderRepository $orderRepository; private OrderRepository $orderRepository;
private UserRepository $userRepository;
/** /**
* Display a listing of the resource. * Display a listing of the resource.
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function __construct(OrderRepository $orderRepository, UserRepository $userRepository) public function __construct(OrderRepository $orderRepository)
{ {
$this->orderRepository = $orderRepository; $this->orderRepository = $orderRepository;
$this->userRepository = $userRepository;
} }
public function index(Request $request) public function index(GetOrderRequest $request)
{ {
$id = $request->id; $user = auth('api')->user()->id;
$user = $this->userRepository->getByid($id); switch ($user->role) {
if ($user->role == User::ROLE_ADMIN) { case User::ROLE_ADMIN:
$order = $this->orderRepository->paginate($request->page); $order = $this->orderRepository->getListOrderOfAdmin($request->status);
} elseif ($user->role == User::ROLE_EDITOR) { break;
$order = $this->orderRepository->getListOrderOfUser($id); case User::ROLE_EDITOR:
$order = $this->orderRepository->getListOrderOfUser($user);
break;
default:
$order = collect([]);
} }
return response()->json([ return response()->json([
'success' => true, 'success' => true,
...@@ -102,14 +105,13 @@ public function edit(Request $request) ...@@ -102,14 +105,13 @@ public function edit(Request $request)
*/ */
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
$order = $this->orderRepository->update( $is_update = $this->orderRepository->update(
$request->id, $request->id,
$request->only(['id', 'details', 'client', 'is_fulfilled']) $request->only(['id', 'details', 'client', 'is_fulfilled'])
); );
return response()->json([ return response()->json([
'success' => true, 'success' => $is_update ? true : false,
'message' => '', 'message' => ''
'data' => $order
]); ]);
} }
...@@ -121,11 +123,10 @@ public function update(Request $request, $id) ...@@ -121,11 +123,10 @@ public function update(Request $request, $id)
*/ */
public function destroy(Request $request) public function destroy(Request $request)
{ {
$order = $this->orderRepository->deleteById($request->id); $is_deleted = $this->orderRepository->deleteById($request->id);
return response()->json([ return response()->json([
'success' => true, 'success' => $is_deleted ? true : false,
'message' => '', 'message' => ''
'data' => $order
]); ]);
} }
} }
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class OrderDetailController extends Controller
{
//
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class GetOrderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'status' => 'required|integer'
];
}
}
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
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
{ {
...@@ -24,9 +23,9 @@ public function getFulfilledOrder() ...@@ -24,9 +23,9 @@ public function getFulfilledOrder()
$this->model->where('is_fulfilled', true); $this->model->where('is_fulfilled', true);
} }
public function getListOrderOfAdmin(Request $request) public function getListOrderOfAdmin($status)
{ {
return $this->model->latest()->where('status', $request->status)->paginate(); return $this->model->latest()->where('status', $status)->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