Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
P
Project_Laravel
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
TTS Nguyen Huu Huan
Project_Laravel
Commits
b51f5c0a
Commit
b51f5c0a
authored
Sep 19, 2022
by
20194288-huannh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update coding convention
parent
d66b8a0f
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
86 additions
and
47 deletions
+86
-47
OrderController.php
app/Http/Controllers/Api/OrderController.php
+8
-7
AuthController.php
app/Http/Controllers/AuthController.php
+26
-17
OrderController.php
app/Http/Controllers/OrderController.php
+35
-8
OrderDetail.php
app/Models/OrderDetail.php
+3
-2
Post.php
app/Models/Post.php
+4
-2
User.php
app/Models/User.php
+3
-2
BaseRepository.php
app/Repositories/BaseRepository.php
+3
-3
OrderRepository.php
app/Repositories/OrderRepository.php
+2
-2
OrderFactory.php
database/factories/OrderFactory.php
+2
-2
api.php
routes/api.php
+0
-2
No files found.
app/Http/Controllers/Api/OrderController.php
View file @
b51f5c0a
...
...
@@ -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
,
...
...
app/Http/Controllers/AuthController.php
View file @
b51f5c0a
<?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'
,
...
...
app/Http/Controllers/OrderController.php
View file @
b51f5c0a
...
...
@@ -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
)
]);
}
}
app/Models/OrderDetail.php
View file @
b51f5c0a
...
...
@@ -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
;
}
}
app/Models/Post.php
View file @
b51f5c0a
...
...
@@ -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
()
...
...
app/Models/User.php
View file @
b51f5c0a
...
...
@@ -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
);
...
...
app/Repositories/BaseRepository.php
View file @
b51f5c0a
...
...
@@ -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
}
app/Repositories/OrderRepository.php
View file @
b51f5c0a
...
...
@@ -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
();
}
}
database/factories/OrderFactory.php
View file @
b51f5c0a
...
...
@@ -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
(),
];
}
...
...
routes/api.php
View file @
b51f5c0a
...
...
@@ -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
);
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment