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

update coding convention

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