Commit 9f0a561c authored by lmethuku's avatar lmethuku

custom messages changes

parent ee2a18b3
...@@ -82,7 +82,7 @@ public class ProfileController { ...@@ -82,7 +82,7 @@ public class ProfileController {
@PutMapping @PutMapping
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public Mono<Profile> updateProfile(@Valid @RequestBody ProfileDTO profileUpdateDTO) { public Mono<Profile> updateProfile(@RequestBody ProfileDTO profileUpdateDTO) {
return profileService.updateProfile(profileUpdateDTO) return profileService.updateProfile(profileUpdateDTO)
.doOnSuccess(updatedProfile -> log.info("Profile updated successfully: {}", updatedProfile)) .doOnSuccess(updatedProfile -> log.info("Profile updated successfully: {}", updatedProfile))
.doOnError(error -> log.error("Error updating profile", error)); .doOnError(error -> log.error("Error updating profile", error));
......
...@@ -6,12 +6,18 @@ import org.springframework.web.bind.annotation.ControllerAdvice; ...@@ -6,12 +6,18 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.support.WebExchangeBindException; import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.server.ResponseStatusException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ControllerAdvice @ControllerAdvice
public class GlobalExceptionHandler { 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 // Handle validation errors
@ExceptionHandler(WebExchangeBindException.class) @ExceptionHandler(WebExchangeBindException.class)
...@@ -44,4 +50,14 @@ public class GlobalExceptionHandler { ...@@ -44,4 +50,14 @@ public class GlobalExceptionHandler {
public ResponseEntity<String> handleProfileNotFound(ProfileNotFoundException ex) { public ResponseEntity<String> handleProfileNotFound(ProfileNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage()); 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 { ...@@ -65,7 +65,7 @@ public class ProfileService {
// Check if at least one search criteria is provided // Check if at least one search criteria is provided
if (firstName == null && lastName == null && pan == null && adhar == null && mobile == null && dob == null) { 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."); 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 // Build the query based on provided criteria
...@@ -98,53 +98,73 @@ public class ProfileService { ...@@ -98,53 +98,73 @@ public class ProfileService {
.switchIfEmpty(Flux.error(new IllegalStateException("No profiles found matching the given criteria."))); .switchIfEmpty(Flux.error(new IllegalStateException("No profiles found matching the given criteria.")));
} }
public Mono<Profile> updateProfile(ProfileDTO updateDTO) { public Mono<Profile> updateProfile(ProfileDTO profileDTO) {
log.info("Received request to update profile with ID: {}", updateDTO.getProfileId()); // Check if profileId is provided
if (profileDTO.getProfileId() == null || profileDTO.getProfileId().trim().isEmpty()) {
return profileRepository.findById(updateDTO.getProfileId()) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Profile ID is mandatory");
}
return profileRepository.findById(profileDTO.getProfileId())
.flatMap(existingProfile -> { .flatMap(existingProfile -> {
boolean updated = false;
if (updateDTO.getFirstName() != null) { // Validate at least one field to update
existingProfile.setFirstName(updateDTO.getFirstName()); if (profileDTO.getFirstName() == null &&
updated = true; 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(profileDTO.getLastName());
existingProfile.setLastName(updateDTO.getLastName());
updated = true;
} }
if (updateDTO.getDob() != null) {
if (profileDTO.getDob() != null) {
try { try {
existingProfile.setDateOfBirth(LocalDate.parse(updateDTO.getDob(), DateTimeFormatter.ofPattern("MM-dd-yyyy"))); LocalDate dob = LocalDate.parse(profileDTO.getDob(), DateTimeFormatter.ofPattern("MM-dd-yyyy"));
updated = true; existingProfile.setDateOfBirth(dob);
} catch (DateTimeParseException e) { } catch (DateTimeParseException e) {
log.error("Invalid date format: {}", updateDTO.getDob()); return Mono.error(new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid date format. Expected MM-DD-YYYY."));
return Mono.error(new IllegalArgumentException("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(profileDTO.getPan());
existingProfile.setPermanentAccountNumber(updateDTO.getPan());
updated = true;
} }
if (updateDTO.getAdhar() != null) {
existingProfile.setAdharNumber(updateDTO.getAdhar()); if (profileDTO.getAdhar() != null) {
updated = true; 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.setAdharNumber(profileDTO.getAdhar());
existingProfile.setMobileNumber(updateDTO.getMobile());
updated = true;
} }
if (!updated) { if (profileDTO.getMobile() != null) {
log.warn("No fields to update for profile with ID: {}", updateDTO.getProfileId()); if (!profileDTO.getMobile().matches("^[6-9][0-9]{9}$")) {
return Mono.error(new IllegalArgumentException("At least one field must be provided to update the profile.")); return Mono.error(new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid mobile number format"));
}
existingProfile.setMobileNumber(profileDTO.getMobile());
} }
return profileRepository.save(existingProfile) // Save the updated profile
.doOnSuccess(updatedProfile -> log.info("Profile updated successfully: {}", updatedProfile)) return profileRepository.save(existingProfile);
.doOnError(error -> log.error("Error updating profile", error));
}) })
.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) { 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