Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
DomPoc
Project
Project
Details
Activity
Releases
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
Laxman Methuku
DomPoc
Commits
9f0a561c
Commit
9f0a561c
authored
Sep 12, 2024
by
lmethuku
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
custom messages changes
parent
ee2a18b3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
35 deletions
+71
-35
ProfileController.java
...a/com/nisum/application/controller/ProfileController.java
+1
-1
GlobalExceptionHandler.java
...m/nisum/application/exception/GlobalExceptionHandler.java
+16
-0
ProfileService.java
...in/java/com/nisum/application/service/ProfileService.java
+54
-34
No files found.
src/main/java/com/nisum/application/controller/ProfileController.java
View file @
9f0a561c
...
...
@@ -82,7 +82,7 @@ public class ProfileController {
@PutMapping
@ResponseStatus
(
HttpStatus
.
OK
)
public
Mono
<
Profile
>
updateProfile
(
@
Valid
@
RequestBody
ProfileDTO
profileUpdateDTO
)
{
public
Mono
<
Profile
>
updateProfile
(
@RequestBody
ProfileDTO
profileUpdateDTO
)
{
return
profileService
.
updateProfile
(
profileUpdateDTO
)
.
doOnSuccess
(
updatedProfile
->
log
.
info
(
"Profile updated successfully: {}"
,
updatedProfile
))
.
doOnError
(
error
->
log
.
error
(
"Error updating profile"
,
error
));
...
...
src/main/java/com/nisum/application/exception/GlobalExceptionHandler.java
View file @
9f0a561c
...
...
@@ -6,12 +6,18 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
org.springframework.web.bind.support.WebExchangeBindException
;
import
org.springframework.web.server.ResponseStatusException
;
import
java.util.List
;
import
java.util.stream.Collectors
;
@ControllerAdvice
public
class
GlobalExceptionHandler
{
@ExceptionHandler
(
ResponseStatusException
.
class
)
public
ResponseEntity
<
String
>
handleResponseStatusException
(
ResponseStatusException
ex
)
{
// Return the status code and the message as response
return
ResponseEntity
.
status
(
ex
.
getStatus
().
value
()).
body
(
ex
.
getReason
());
}
// Handle validation errors
@ExceptionHandler
(
WebExchangeBindException
.
class
)
...
...
@@ -44,4 +50,14 @@ public class GlobalExceptionHandler {
public
ResponseEntity
<
String
>
handleProfileNotFound
(
ProfileNotFoundException
ex
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
body
(
ex
.
getMessage
());
}
@ExceptionHandler
(
IllegalArgumentException
.
class
)
public
ResponseEntity
<
String
>
handleIllegalArgumentException
(
IllegalArgumentException
ex
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
BAD_REQUEST
).
body
(
ex
.
getMessage
());
}
@ExceptionHandler
(
IllegalStateException
.
class
)
public
ResponseEntity
<
String
>
handleIllegalStateException
(
IllegalStateException
ex
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
body
(
ex
.
getMessage
());
}
}
src/main/java/com/nisum/application/service/ProfileService.java
View file @
9f0a561c
...
...
@@ -65,7 +65,7 @@ public class ProfileService {
// Check if at least one search criteria is provided
if
(
firstName
==
null
&&
lastName
==
null
&&
pan
==
null
&&
adhar
==
null
&&
mobile
==
null
&&
dob
==
null
)
{
log
.
error
(
"Search criteria are missing. At least one criteria must be provided."
);
return
Flux
.
error
(
new
Illegal
Argument
Exception
(
"At least one search criteria must be provided."
));
return
Flux
.
error
(
new
Illegal
State
Exception
(
"At least one search criteria must be provided."
));
}
// Build the query based on provided criteria
...
...
@@ -98,53 +98,73 @@ public class ProfileService {
.
switchIfEmpty
(
Flux
.
error
(
new
IllegalStateException
(
"No profiles found matching the given criteria."
)));
}
public
Mono
<
Profile
>
updateProfile
(
ProfileDTO
updateDTO
)
{
log
.
info
(
"Received request to update profile with ID: {}"
,
updateDTO
.
getProfileId
());
return
profileRepository
.
findById
(
updateDTO
.
getProfileId
())
public
Mono
<
Profile
>
updateProfile
(
ProfileDTO
profileDTO
)
{
// Check if profileId is provided
if
(
profileDTO
.
getProfileId
()
==
null
||
profileDTO
.
getProfileId
().
trim
().
isEmpty
())
{
throw
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"Profile ID is mandatory"
);
}
return
profileRepository
.
findById
(
profileDTO
.
getProfileId
())
.
flatMap
(
existingProfile
->
{
boolean
updated
=
false
;
if
(
updateDTO
.
getFirstName
()
!=
null
)
{
existingProfile
.
setFirstName
(
updateDTO
.
getFirstName
());
updated
=
true
;
// Validate at least one field to update
if
(
profileDTO
.
getFirstName
()
==
null
&&
profileDTO
.
getLastName
()
==
null
&&
profileDTO
.
getDob
()
==
null
&&
profileDTO
.
getPan
()
==
null
&&
profileDTO
.
getAdhar
()
==
null
&&
profileDTO
.
getMobile
()
==
null
)
{
return
Mono
.
error
(
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"At least one field must be provided to update."
));
}
if
(
updateDTO
.
getLastName
()
!=
null
)
{
existingProfile
.
setLastName
(
updateDTO
.
getLastName
());
updated
=
true
;
// Set new values only if they are provided
if
(
profileDTO
.
getFirstName
()
!=
null
)
{
if
(!
profileDTO
.
getFirstName
().
matches
(
"^[a-zA-Z ]+$"
))
{
return
Mono
.
error
(
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"First name should only contain letters and spaces"
));
}
existingProfile
.
setFirstName
(
profileDTO
.
getFirstName
());
}
if
(
profileDTO
.
getLastName
()
!=
null
)
{
if
(!
profileDTO
.
getLastName
().
matches
(
"^[a-zA-Z ]+$"
))
{
return
Mono
.
error
(
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"Last name should only contain letters and spaces"
));
}
existingProfile
.
setLastName
(
profileDTO
.
getLastName
());
}
if
(
updateDTO
.
getDob
()
!=
null
)
{
if
(
profileDTO
.
getDob
()
!=
null
)
{
try
{
existingProfile
.
setDateOfBirth
(
LocalDate
.
parse
(
updateDTO
.
getDob
(),
DateTimeFormatter
.
ofPattern
(
"MM-dd-yyyy"
)
));
updated
=
true
;
LocalDate
dob
=
LocalDate
.
parse
(
profileDTO
.
getDob
(),
DateTimeFormatter
.
ofPattern
(
"MM-dd-yyyy"
));
existingProfile
.
setDateOfBirth
(
dob
)
;
}
catch
(
DateTimeParseException
e
)
{
log
.
error
(
"Invalid date format: {}"
,
updateDTO
.
getDob
());
return
Mono
.
error
(
new
IllegalArgumentException
(
"Invalid date format. Expected MM-DD-YYYY"
));
return
Mono
.
error
(
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"Invalid date format. Expected MM-DD-YYYY."
));
}
}
if
(
updateDTO
.
getPan
()
!=
null
)
{
existingProfile
.
setPermanentAccountNumber
(
updateDTO
.
getPan
());
updated
=
true
;
}
if
(
updateDTO
.
getAdhar
()
!=
null
)
{
existingProfile
.
setAdharNumber
(
updateDTO
.
getAdhar
());
updated
=
true
;
if
(
profileDTO
.
getPan
()
!=
null
)
{
if
(!
profileDTO
.
getPan
().
matches
(
"^[A-Z]{5}[0-9]{4}[A-Z]$"
))
{
return
Mono
.
error
(
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"Invalid PAN format"
));
}
existingProfile
.
setPermanentAccountNumber
(
profileDTO
.
getPan
());
}
if
(
updateDTO
.
getMobile
()
!=
null
)
{
existingProfile
.
setMobileNumber
(
updateDTO
.
getMobile
());
updated
=
true
;
if
(
profileDTO
.
getAdhar
()
!=
null
)
{
if
(!
profileDTO
.
getAdhar
().
matches
(
"^[1-9][0-9]{11}$"
))
{
return
Mono
.
error
(
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"Invalid Aadhar format"
));
}
existingProfile
.
setAdharNumber
(
profileDTO
.
getAdhar
());
}
if
(!
updated
)
{
log
.
warn
(
"No fields to update for profile with ID: {}"
,
updateDTO
.
getProfileId
());
return
Mono
.
error
(
new
IllegalArgumentException
(
"At least one field must be provided to update the profile."
));
if
(
profileDTO
.
getMobile
()
!=
null
)
{
if
(!
profileDTO
.
getMobile
().
matches
(
"^[6-9][0-9]{9}$"
))
{
return
Mono
.
error
(
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"Invalid mobile number format"
));
}
existingProfile
.
setMobileNumber
(
profileDTO
.
getMobile
());
}
return
profileRepository
.
save
(
existingProfile
)
.
doOnSuccess
(
updatedProfile
->
log
.
info
(
"Profile updated successfully: {}"
,
updatedProfile
))
.
doOnError
(
error
->
log
.
error
(
"Error updating profile"
,
error
));
// Save the updated profile
return
profileRepository
.
save
(
existingProfile
);
})
.
switchIfEmpty
(
Mono
.
error
(
new
IllegalArgumentException
(
"Profile not found with the given ID"
)));
.
switchIfEmpty
(
Mono
.
error
(
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Profile not found with the given ID"
)));
}
public
Mono
<
Void
>
deleteProfileById
(
String
profileId
)
{
...
...
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