Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
java-jwt
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
Phùng Quốc Toàn
java-jwt
Commits
11fb58bf
Commit
11fb58bf
authored
Mar 02, 2024
by
devteria
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
c92a7378
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
128 additions
and
10 deletions
+128
-10
UserController.java
...m/devteria/identityservice/controller/UserController.java
+7
-2
ApiResponse.java
...com/devteria/identityservice/dto/request/ApiResponse.java
+34
-0
UserCreationRequest.java
...eria/identityservice/dto/request/UserCreationRequest.java
+2
-2
AppException.java
.../com/devteria/identityservice/exception/AppException.java
+19
-0
ErrorCode.java
...ava/com/devteria/identityservice/exception/ErrorCode.java
+26
-0
GlobalExceptionHandler.java
...ria/identityservice/exception/GlobalExceptionHandler.java
+37
-5
UserService.java
...ava/com/devteria/identityservice/service/UserService.java
+3
-1
No files found.
src/main/java/com/devteria/identityservice/controller/UserController.java
View file @
11fb58bf
package
com
.
devteria
.
identityservice
.
controller
;
import
com.devteria.identityservice.dto.request.ApiResponse
;
import
com.devteria.identityservice.dto.request.UserCreationRequest
;
import
com.devteria.identityservice.dto.request.UserUpdateRequest
;
import
com.devteria.identityservice.entity.User
;
...
...
@@ -17,8 +18,12 @@ public class UserController {
private
UserService
userService
;
@PostMapping
User
createUser
(
@RequestBody
@Valid
UserCreationRequest
request
){
return
userService
.
createUser
(
request
);
ApiResponse
<
User
>
createUser
(
@RequestBody
@Valid
UserCreationRequest
request
){
ApiResponse
<
User
>
apiResponse
=
new
ApiResponse
<>();
apiResponse
.
setResult
(
userService
.
createUser
(
request
));
return
apiResponse
;
}
@GetMapping
...
...
src/main/java/com/devteria/identityservice/dto/request/ApiResponse.java
0 → 100644
View file @
11fb58bf
package
com
.
devteria
.
identityservice
.
dto
.
request
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
@JsonInclude
(
JsonInclude
.
Include
.
NON_NULL
)
public
class
ApiResponse
<
T
>
{
private
int
code
=
1000
;
private
String
message
;
private
T
result
;
public
int
getCode
()
{
return
code
;
}
public
void
setCode
(
int
code
)
{
this
.
code
=
code
;
}
public
String
getMessage
()
{
return
message
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
public
T
getResult
()
{
return
result
;
}
public
void
setResult
(
T
result
)
{
this
.
result
=
result
;
}
}
src/main/java/com/devteria/identityservice/dto/request/UserCreationRequest.java
View file @
11fb58bf
...
...
@@ -5,10 +5,10 @@ import jakarta.validation.constraints.Size;
import
java.time.LocalDate
;
public
class
UserCreationRequest
{
@Size
(
min
=
3
,
message
=
"U
sername must be at least 3 characters
"
)
@Size
(
min
=
3
,
message
=
"U
SERNAME_INVALID
"
)
private
String
username
;
@Size
(
min
=
8
,
message
=
"
Password must be at least 8 characters
"
)
@Size
(
min
=
8
,
message
=
"
INVALID_PASSWORD
"
)
private
String
password
;
private
String
firstName
;
private
String
lastName
;
...
...
src/main/java/com/devteria/identityservice/exception/AppException.java
0 → 100644
View file @
11fb58bf
package
com
.
devteria
.
identityservice
.
exception
;
public
class
AppException
extends
RuntimeException
{
public
AppException
(
ErrorCode
errorCode
)
{
super
(
errorCode
.
getMessage
());
this
.
errorCode
=
errorCode
;
}
private
ErrorCode
errorCode
;
public
ErrorCode
getErrorCode
()
{
return
errorCode
;
}
public
void
setErrorCode
(
ErrorCode
errorCode
)
{
this
.
errorCode
=
errorCode
;
}
}
src/main/java/com/devteria/identityservice/exception/ErrorCode.java
0 → 100644
View file @
11fb58bf
package
com
.
devteria
.
identityservice
.
exception
;
public
enum
ErrorCode
{
UNCATEGORIZED_EXCEPTION
(
9999
,
"Uncategorized error"
),
INVALID_KEY
(
1001
,
"Uncategorized error"
),
USER_EXISTED
(
1002
,
"User existed"
),
USERNAME_INVALID
(
1003
,
"Username must be at least 3 characters"
),
INVALID_PASSWORD
(
1004
,
"Password must be at least 8 characters"
)
;
ErrorCode
(
int
code
,
String
message
)
{
this
.
code
=
code
;
this
.
message
=
message
;
}
private
int
code
;
private
String
message
;
public
int
getCode
()
{
return
code
;
}
public
String
getMessage
()
{
return
message
;
}
}
src/main/java/com/devteria/identityservice/exception/GlobalExceptionHandler.java
View file @
11fb58bf
package
com
.
devteria
.
identityservice
.
exception
;
import
com.devteria.identityservice.dto.request.ApiResponse
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
...
...
@@ -8,13 +9,44 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public
class
GlobalExceptionHandler
{
@ExceptionHandler
(
value
=
RuntimeException
.
class
)
ResponseEntity
<
String
>
handlingRuntimeException
(
RuntimeException
exception
){
return
ResponseEntity
.
badRequest
().
body
(
exception
.
getMessage
());
@ExceptionHandler
(
value
=
Exception
.
class
)
ResponseEntity
<
ApiResponse
>
handlingRuntimeException
(
RuntimeException
exception
){
ApiResponse
apiResponse
=
new
ApiResponse
();
apiResponse
.
setCode
(
ErrorCode
.
UNCATEGORIZED_EXCEPTION
.
getCode
());
apiResponse
.
setMessage
(
ErrorCode
.
UNCATEGORIZED_EXCEPTION
.
getMessage
());
return
ResponseEntity
.
badRequest
().
body
(
apiResponse
);
}
@ExceptionHandler
(
value
=
AppException
.
class
)
ResponseEntity
<
ApiResponse
>
handlingAppException
(
AppException
exception
){
ErrorCode
errorCode
=
exception
.
getErrorCode
();
ApiResponse
apiResponse
=
new
ApiResponse
();
apiResponse
.
setCode
(
errorCode
.
getCode
());
apiResponse
.
setMessage
(
errorCode
.
getMessage
());
return
ResponseEntity
.
badRequest
().
body
(
apiResponse
);
}
@ExceptionHandler
(
value
=
MethodArgumentNotValidException
.
class
)
ResponseEntity
<
String
>
handlingValidation
(
MethodArgumentNotValidException
exception
){
return
ResponseEntity
.
badRequest
().
body
(
exception
.
getFieldError
().
getDefaultMessage
());
ResponseEntity
<
ApiResponse
>
handlingValidation
(
MethodArgumentNotValidException
exception
){
String
enumKey
=
exception
.
getFieldError
().
getDefaultMessage
();
ErrorCode
errorCode
=
ErrorCode
.
INVALID_KEY
;
try
{
errorCode
=
ErrorCode
.
valueOf
(
enumKey
);
}
catch
(
IllegalArgumentException
e
){
}
ApiResponse
apiResponse
=
new
ApiResponse
();
apiResponse
.
setCode
(
errorCode
.
getCode
());
apiResponse
.
setMessage
(
errorCode
.
getMessage
());
return
ResponseEntity
.
badRequest
().
body
(
apiResponse
);
}
}
src/main/java/com/devteria/identityservice/service/UserService.java
View file @
11fb58bf
...
...
@@ -3,6 +3,8 @@ package com.devteria.identityservice.service;
import
com.devteria.identityservice.dto.request.UserCreationRequest
;
import
com.devteria.identityservice.dto.request.UserUpdateRequest
;
import
com.devteria.identityservice.entity.User
;
import
com.devteria.identityservice.exception.AppException
;
import
com.devteria.identityservice.exception.ErrorCode
;
import
com.devteria.identityservice.repository.UserRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
...
...
@@ -18,7 +20,7 @@ public class UserService {
User
user
=
new
User
();
if
(
userRepository
.
existsByUsername
(
request
.
getUsername
()))
throw
new
RuntimeException
(
"User existed."
);
throw
new
AppException
(
ErrorCode
.
USER_EXISTED
);
user
.
setUsername
(
request
.
getUsername
());
user
.
setPassword
(
request
.
getPassword
());
...
...
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