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

update coding convention

parent d66b8a0f
...@@ -27,9 +27,9 @@ public function index(Request $request) ...@@ -27,9 +27,9 @@ public function index(Request $request)
{ {
$id = $request->id; $id = $request->id;
$user = $this->userRepository->getByid($id); $user = $this->userRepository->getByid($id);
if ($user->role == User::ROLE_ADMIN){ if ($user->role == User::ROLE_ADMIN) {
$order = $this->orderRepository->paginate($request->page); $order = $this->orderRepository->paginate($request->page);
} elseif ($user->role == User::ROLE_EDITOR){ } elseif ($user->role == User::ROLE_EDITOR) {
$order = $this->orderRepository->getListOrderOfUser($id); $order = $this->orderRepository->getListOrderOfUser($id);
} }
return response()->json([ return response()->json([
...@@ -49,7 +49,6 @@ public function index(Request $request) ...@@ -49,7 +49,6 @@ public function index(Request $request)
*/ */
public function create(Request $request) public function create(Request $request)
{ {
} }
/** /**
...@@ -92,7 +91,6 @@ public function show(Request $request) ...@@ -92,7 +91,6 @@ public function show(Request $request)
*/ */
public function edit(Request $request) public function edit(Request $request)
{ {
} }
/** /**
...@@ -104,8 +102,10 @@ public function edit(Request $request) ...@@ -104,8 +102,10 @@ public function edit(Request $request)
*/ */
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
$order = $this->orderRepository->update($request->id, $order = $this->orderRepository->update(
$request->only(['id', 'details', 'client', 'is_fulfilled'])); $request->id,
$request->only(['id', 'details', 'client', 'is_fulfilled'])
);
return response()->json([ return response()->json([
'success' => true, 'success' => true,
'message' => '', 'message' => '',
...@@ -129,7 +129,8 @@ public function destroy(Request $request) ...@@ -129,7 +129,8 @@ public function destroy(Request $request)
]); ]);
} }
public function search(Request $request){ public function search(Request $request)
{
$order = $this->orderRepository->getListOrderByStatus($request->status); $order = $this->orderRepository->getListOrderByStatus($request->status);
return response()->json([ return response()->json([
'success' => true, 'success' => true,
......
<?php <?php
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;
...@@ -15,7 +16,8 @@ class AuthController extends Controller ...@@ -15,7 +16,8 @@ class AuthController extends Controller
* *
* @return void * @return void
*/ */
public function __construct() { public function __construct()
{
$this->middleware('auth:api', ['except' => ['login', 'register']]); $this->middleware('auth:api', ['except' => ['login', 'register']]);
} }
...@@ -24,8 +26,9 @@ public function __construct() { ...@@ -24,8 +26,9 @@ public function __construct() {
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function login(Request $request){ public function login(Request $request)
$validator = Validator::make($request->all(), [ {
$validator = Validator::make($request->all(), [
'email' => 'required|email', 'email' => 'required|email',
'password' => 'required|string|min:6', 'password' => 'required|string|min:6',
]); ]);
...@@ -34,7 +37,7 @@ public function login(Request $request){ ...@@ -34,7 +37,7 @@ public function login(Request $request){
return response()->json($validator->errors(), 422); return response()->json($validator->errors(), 422);
} }
if (! $token = auth('api')->attempt($validator->validated())) { if (!$token = auth('api')->attempt($validator->validated())) {
return response()->json(['error' => 'Unauthorized'], 401); return response()->json(['error' => 'Unauthorized'], 401);
} }
...@@ -46,21 +49,22 @@ public function login(Request $request){ ...@@ -46,21 +49,22 @@ public function login(Request $request){
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function register(Request $request) { public function register(Request $request)
{
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'name' => 'required|string|between:2,100', 'name' => 'required|string|between:2,100',
'email' => 'required|string|email|max:100|unique:users', 'email' => 'required|string|email|max:100|unique:users',
'password' => 'required|string|confirmed|min:6', 'password' => 'required|string|confirmed|min:6',
]); ]);
if($validator->fails()){ if ($validator->fails()) {
return response()->json($validator->errors()->toJson(), 400); return response()->json($validator->errors()->toJson(), 400);
} }
$user = User::create(array_merge( $user = User::create(array_merge(
$validator->validated(), $validator->validated(),
['password' => bcrypt($request->password)] ['password' => bcrypt($request->password)]
)); ));
return response()->json([ return response()->json([
'message' => 'User successfully registered', 'message' => 'User successfully registered',
...@@ -74,7 +78,8 @@ public function register(Request $request) { ...@@ -74,7 +78,8 @@ public function register(Request $request) {
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function logout() { public function logout()
{
auth('api')->logout(); auth('api')->logout();
return response()->json(['message' => 'User successfully signed out']); return response()->json(['message' => 'User successfully signed out']);
...@@ -85,7 +90,8 @@ public function logout() { ...@@ -85,7 +90,8 @@ public function logout() {
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function refresh() { public function refresh()
{
return $this->createNewToken(auth('api')->refresh()); return $this->createNewToken(auth('api')->refresh());
} }
...@@ -94,7 +100,8 @@ public function refresh() { ...@@ -94,7 +100,8 @@ public function refresh() {
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function userProfile() { public function userProfile()
{
return response()->json(auth('api')->user()); return response()->json(auth('api')->user());
} }
...@@ -105,7 +112,8 @@ public function userProfile() { ...@@ -105,7 +112,8 @@ public function userProfile() {
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
protected function createNewToken($token){ protected function createNewToken($token)
{
return response()->json([ return response()->json([
'access_token' => $token, 'access_token' => $token,
'token_type' => 'bearer', 'token_type' => 'bearer',
...@@ -114,20 +122,21 @@ protected function createNewToken($token){ ...@@ -114,20 +122,21 @@ protected function createNewToken($token){
]); ]);
} }
public function changePassWord(Request $request) { public function changePassWord(Request $request)
{
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'old_password' => 'required|string|min:6', 'old_password' => 'required|string|min:6',
'new_password' => 'required|string|confirmed|min:6', 'new_password' => 'required|string|confirmed|min:6',
]); ]);
if($validator->fails()){ if ($validator->fails()) {
return response()->json($validator->errors()->toJson(), 400); return response()->json($validator->errors()->toJson(), 400);
} }
$userId = auth('api')->user()->id; $userId = auth('api')->user()->id;
$user = User::where('id', $userId)->update( $user = User::where('id', $userId)->update(
['password' => bcrypt($request->new_password)] ['password' => bcrypt($request->new_password)]
); );
return response()->json([ return response()->json([
'message' => 'User successfully changed password', 'message' => 'User successfully changed password',
......
...@@ -2,28 +2,42 @@ ...@@ -2,28 +2,42 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Resources\OrderResource;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Resources\OrderResource;
use App\Repositories\OrderRepository; use App\Repositories\OrderRepository;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
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) public function __construct(OrderRepository $orderRepository, UserRepository $userRepository)
{ {
$this->orderRepository = $orderRepository; $this->orderRepository = $orderRepository;
$this->userRepository = $userRepository;
} }
public function index(Request $request) public function index(Request $request)
{ {
$order = $this->orderRepository->paginate($request->page); $id = $request->id;
$user = $this->userRepository->getByid($id);
if ($user->role == User::ROLE_ADMIN) {
$order = $this->orderRepository->paginate($request->page);
} elseif ($user->role == User::ROLE_EDITOR) {
$order = $this->orderRepository->getListOrderOfUser($id);
}
return response()->json([ return response()->json([
'success' => true, 'success' => true,
'message' => '', 'meta' => [
'total' => $order->total(),
'pages' => $order->lastPage()
],
'data' => OrderResource::collection($order) 'data' => OrderResource::collection($order)
]); ]);
} }
...@@ -35,7 +49,6 @@ public function index(Request $request) ...@@ -35,7 +49,6 @@ public function index(Request $request)
*/ */
public function create(Request $request) public function create(Request $request)
{ {
} }
/** /**
...@@ -78,7 +91,6 @@ public function show(Request $request) ...@@ -78,7 +91,6 @@ public function show(Request $request)
*/ */
public function edit(Request $request) public function edit(Request $request)
{ {
} }
/** /**
...@@ -90,8 +102,10 @@ public function edit(Request $request) ...@@ -90,8 +102,10 @@ public function edit(Request $request)
*/ */
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
$order = $this->orderRepository->update($request->id, $order = $this->orderRepository->update(
$request->only(['id', 'details', 'client', 'is_fulfilled'])); $request->id,
$request->only(['id', 'details', 'client', 'is_fulfilled'])
);
return response()->json([ return response()->json([
'success' => true, 'success' => true,
'message' => '', 'message' => '',
...@@ -114,4 +128,17 @@ public function destroy(Request $request) ...@@ -114,4 +128,17 @@ 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)
]);
}
} }
...@@ -13,8 +13,9 @@ public function order() ...@@ -13,8 +13,9 @@ public function order()
{ {
return $this->belongsTo(Order::class); return $this->belongsTo(Order::class);
} }
public function getPayMoneyAttribute(){ public function getPayMoneyAttribute()
{
return $this->unit_price * $this->quantity * (100 - $this->discount) / 100; return $this->unit_price * $this->quantity * (100 - $this->discount) / 100;
} }
} }
...@@ -18,10 +18,12 @@ class Post extends Model ...@@ -18,10 +18,12 @@ class Post extends Model
protected $attributes = [ protected $attributes = [
'status' => true, 'status' => true,
]; ];
public function user() { public function user()
{
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
public function categories() { public function categories()
{
return $this->belongsToMany(Category::class); return $this->belongsToMany(Category::class);
} }
public function tags() public function tags()
......
...@@ -41,9 +41,10 @@ class User extends Authenticatable implements JWTSubject ...@@ -41,9 +41,10 @@ class User extends Authenticatable implements JWTSubject
'email_verified_at' => 'datetime', 'email_verified_at' => 'datetime',
]; ];
public function posts() { public function posts()
{
return $this->hasMany(Post::class); return $this->hasMany(Post::class);
} }
public function comments() public function comments()
{ {
return $this->hasMany(Comment::class); return $this->hasMany(Comment::class);
......
...@@ -20,12 +20,12 @@ public function deleteById($id) ...@@ -20,12 +20,12 @@ public function deleteById($id)
return $this->getById($id)->delete(); return $this->getById($id)->delete();
} }
public function create(array $details) public function create(array $details)
{ {
return $this->model->create($details); return $this->model->create($details);
} }
public function update($id, array $newDetails) public function update($id, array $newDetails)
{ {
return $this->getById($id)->update($newDetails); return $this->getById($id)->update($newDetails);
} }
...@@ -34,4 +34,4 @@ public function paginate($size = 15) ...@@ -34,4 +34,4 @@ public function paginate($size = 15)
{ {
return $this->model->latest()->paginate($size); return $this->model->latest()->paginate($size);
} }
} }
\ No newline at end of file
...@@ -25,13 +25,13 @@ public function getFulfilledOrder() ...@@ -25,13 +25,13 @@ public function getFulfilledOrder()
public function getListOrderOfUser($id) public function getListOrderOfUser($id)
{ {
return $order = $this->model->with('user')->whereHas('user', function($query) use ($id) { return $order = $this->model->with('user')->whereHas('user', function ($query) use ($id) {
$query->where('id', $id); $query->where('id', $id);
})->paginate(); })->paginate();
} }
public function getListOrderByStatus($status) public function getListOrderByStatus($status)
{ {
return $order = $this->model->where('status', $status)->paginate(); return $order = $this->model->where('status', $status)->paginate();
} }
} }
...@@ -15,8 +15,8 @@ public function definition() ...@@ -15,8 +15,8 @@ public function definition()
{ {
return [ return [
'user_id' => rand(1, 10), 'user_id' => rand(1, 10),
'details' => $this->faker->sentences(4, true), 'details' => $this->faker->sentences(4, true),
'client' => $this->faker->name(), 'client' => $this->faker->name(),
'is_fulfilled' => $this->faker->boolean(), 'is_fulfilled' => $this->faker->boolean(),
]; ];
} }
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
Route::post('refresh', [App\Http\Controllers\AuthController::class, 'refresh']); Route::post('refresh', [App\Http\Controllers\AuthController::class, 'refresh']);
Route::get('user-profile', [App\Http\Controllers\AuthController::class, 'userProfile']); Route::get('user-profile', [App\Http\Controllers\AuthController::class, 'userProfile']);
Route::post('change-password', [App\Http\Controllers\AuthController::class, 'changePassword']); Route::post('change-password', [App\Http\Controllers\AuthController::class, 'changePassword']);
}); });
Route::group([ Route::group([
...@@ -39,4 +38,3 @@ ...@@ -39,4 +38,3 @@
Route::get('orders/search', [App\Http\Controllers\Api\OrderController::class, 'search']); 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