Commit 9f0a561c authored by lmethuku's avatar lmethuku

custom messages changes

parent ee2a18b3
......@@ -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));
......
......@@ -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());
}
}
......@@ -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 IllegalArgumentException("At least one search criteria must be provided."));
return Flux.error(new IllegalStateException("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."));
}
// 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"));
}
if (updateDTO.getLastName() != null) {
existingProfile.setLastName(updateDTO.getLastName());
updated = true;
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 (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"));
}
if (updateDTO.getPan() != null) {
existingProfile.setPermanentAccountNumber(updateDTO.getPan());
updated = true;
existingProfile.setPermanentAccountNumber(profileDTO.getPan());
}
if (updateDTO.getAdhar() != null) {
existingProfile.setAdharNumber(updateDTO.getAdhar());
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"));
}
if (updateDTO.getMobile() != null) {
existingProfile.setMobileNumber(updateDTO.getMobile());
updated = true;
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) {
......
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