Commit 5bf73d9f authored by Syed Asif's avatar Syed Asif

validation changes done

parent 1ef37f1f
package com.example.demo;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
public class DemoApplication {
@GetMapping("/")
String home() {
return "Spring is here!";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
\ No newline at end of file
......@@ -24,29 +24,22 @@ public class ProfileDto implements Serializable {
private String profileId;
@NotNull(message = "First Name is Mandatory")
@Pattern(regexp = "^[a-zA-Z]+$", message = "First Name should only contain alphabets and spaces")
private String firstName;
@NotNull(message = "Last Name is Mandatory")
@Pattern(regexp = "^[a-zA-Z]+$", message = "Last Name should only contain alphabets and spaces")
private String lastName;
@NotNull(message = "Date of Birth is Mandatory")
@Pattern(regexp = "^\\d{2}-\\d{2}-\\d{4}$", message = "Date of Birth should be in MM-DD-YYYY format")
private String dateOfBirth;
// @Pattern(regexp = "^[A-Z]{5}\\d{4}[A-Z]$", message = "Invalid PAN format")
@Pattern(regexp = "[A-Z]{5}[0-9]{4}[A-Z]{1}", message = "Invalid PAN format")
private String permanentAccountNumber;
@Size(min = 12, max = 12, message = "Aadhar Number should be a 12-digit number")
// @Pattern(regexp = "^[1-9]\\d{11}$", message = "Aadhar Number should not start with 0")
@Pattern(regexp = "^[2-9]\\d{1}[0-9]\\d{10}$", message = "Aadhar Number should not start with 0")
private String adharNumber;
@NotBlank(message = "Mobile Number is mandatory")
@Pattern(regexp = "^[6-9]\\d{9}$", message = "Mobile Number should be 10-digit number starting with 6/7/8/9")
private String mobileNumber;
private String status;
public interface ProfileCreate{}
......
......@@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import com.timtech.profile.dto.ProfileDto;
......@@ -26,17 +25,13 @@ public class ProfileServiceImpl implements ProfileService {
@Autowired
private ProfileRepository profileRepository;
@Transactional
public Mono<ProfileDto> save(ProfileDto profileDto) {
log.debug("Request to save Profile " + profileDto);
if (profileDto == null) {
return Mono.error(new RuntimeException("Profile Should Not be NULL"));
}
boolean isValidPan = Utils.builder().build().isValidPanCard(profileDto.getPermanentAccountNumber());
boolean isValidAadhar = Utils.builder().build().isValidAadhar(profileDto.getAdharNumber());
if (isValidPan || isValidAadhar) {
if (Utils.builder().build().isValidDataFormats(profileDto)) {
return profileRepository.existsByPermanentAccountNumber(profileDto.getPermanentAccountNumber())
.zipWith(profileRepository.existsByAdharNumber(profileDto.getAdharNumber())).flatMap(tuple -> {
if (tuple.getT1() || tuple.getT2()) {
......@@ -55,56 +50,50 @@ public class ProfileServiceImpl implements ProfileService {
.onErrorResume(ex -> Mono.error(
new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error !!!")));
} else {
return Mono.error(new BusinessException(HttpStatus.BAD_REQUEST, "Invalid PAN or Aadhar Number"));
return Mono.error(new BusinessException(HttpStatus.BAD_REQUEST,
"Invalid PAN or Aadhar Number or First Name or Last Name or Date of Birth or Mobile Number"));
}
}
@Transactional
public Mono<ProfileDto> update(ProfileDto profileDto) {
log.debug("Request to update profile " + profileDto);
if (profileDto == null) {
return Mono.error(new RuntimeException("Profile Should Not be NULL"));
}
boolean isValidPan = Utils.builder().build().isValidPanCard(profileDto.getPermanentAccountNumber());
boolean isValidAadhar = Utils.builder().build().isValidAadhar(profileDto.getAdharNumber());
if (isValidPan || isValidAadhar) {
return findOne(profileDto.getProfileId()).flatMap(existingProfileDto -> {
if ("ACTIVE".equals(existingProfileDto.getStatus())) {
Profile profile = convertDtoToProfile(existingProfileDto);
profile.setStatus("IN-ACTIVE");
return profileRepository.save(profile).map(this::convertProfileToDto)
.onErrorMap(DuplicateKeyException.class, ex -> {
throw new RuntimeException("Duplicate PAN or Aadhar Number");
});
} else {
return Mono.error(new RuntimeException("Profile has been Deleted !!!"));
}
});
if (Utils.builder().build().isValidDataFormats(profileDto)) {
return findOne(profileDto.getProfileId()).flatMap(existingProfileDto -> {
if ("ACTIVE".equals(existingProfileDto.getStatus())) {
Profile profile = convertDtoToProfile(existingProfileDto);
profile.setStatus("IN-ACTIVE");
return profileRepository.save(profile).map(this::convertProfileToDto)
.onErrorMap(DuplicateKeyException.class, ex -> {
throw new RuntimeException("Duplicate PAN or Aadhar Number");
});
} else {
return Mono.error(new RuntimeException("Profile has been Deleted !!!"));
}
});
} else {
return Mono.error(new BusinessException(HttpStatus.BAD_REQUEST, "Invalid PAN or Aadhar Number"));
return Mono.error(new BusinessException(HttpStatus.BAD_REQUEST,
"Invalid PAN or Aadhar Number or First Name or Last Name or Date of Birth or Mobile Number"));
}
}
@Transactional(readOnly = true)
public Flux<ProfileDto> findAll() {
log.debug("Request to find all profiles ");
return profileRepository.findByStatus("ACTIVE").map(this::convertProfileToDto);
}
@Transactional(readOnly = true)
public Mono<ProfileDto> findOne(String profileId) {
log.debug("Request to find one profile ");
return profileRepository.findById(profileId).map(this::convertProfileToDto);
}
@Transactional
public Mono<String> deleteOne(String profileId) {
log.debug("Request to delete one profile ");
......
package com.timtech.profile.util;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.http.HttpStatus;
import com.timtech.profile.dto.ProfileDto;
import com.timtech.profile.exception.BusinessException;
import lombok.Builder;
@Builder
public class Utils {
public boolean isValidPanCard(String panCardNo) {
String regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
Pattern p = Pattern.compile(regex);
if (panCardNo == null) {
return false;
}
Matcher m = p.matcher(panCardNo);
return m.matches();
}
public boolean isValidAadhar(String adhar) {
String regex = "[1-9]{12}";
Pattern p = Pattern.compile(regex);
if (adhar == null) {
return false;
}
Matcher m = p.matcher(adhar);
return m.matches();
}
public boolean isMobileValidFormat(String mobile) {
String regex = "[6-9]{1}[0-9]{9}";
Pattern p = Pattern.compile(regex);
if (mobile == null) {
return false;
}
Matcher m = p.matcher(mobile);
return m.matches();
}
public boolean isValidPanCard(String panCardNo)
{
String regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}";
Pattern p = Pattern.compile(regex);
if (panCardNo == null)
{
return false;
}
Matcher m = p.matcher(panCardNo);
return m.matches();
}
public boolean isNameValidFormat(String name) {
String regex = "^[A-Za-z]+( [A-Za-z]+)?$";
Pattern p = Pattern.compile(regex);
if (name == null) {
return false;
}
Matcher m = p.matcher(name);
return m.matches();
}
public boolean isValidAadhar(String adhar)
{
String regex = "[1-9]{12}";
public boolean isDOBValidFormat(String dob) {
String regex = "^(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])-(\\d{4})$";
Pattern p = Pattern.compile(regex);
if (adhar == null)
{
if (dob == null) {
return false;
}
Matcher m = p.matcher(adhar);
Matcher m = p.matcher(dob);
return m.matches();
}
// public static void main(String[] args) {
//
// boolean validAadhar = Utils.builder().build().isValidAadhar("011234567891");
// System.out.println(validAadhar);
//
// }
public boolean isValidDataFormats(ProfileDto profileDto) {
List<String> validationErrors = new ArrayList<>();
if (!isValidPanCard(profileDto.getPermanentAccountNumber())) {
validationErrors.add("Invalid PAN");
}
if (!isValidAadhar(profileDto.getAdharNumber())) {
validationErrors.add("Invalid Aadhar Number");
}
if (!isDOBValidFormat(profileDto.getDateOfBirth())) {
validationErrors.add("Invalid Date of Birth");
}
if (!isNameValidFormat(profileDto.getFirstName())) {
validationErrors.add("Invalid First Name");
}
if (!isNameValidFormat(profileDto.getLastName())) {
validationErrors.add("Invalid Last Name");
}
if (!isMobileValidFormat(profileDto.getMobileNumber())) {
validationErrors.add("Invalid Mobile Number");
}
if (validationErrors.isEmpty()) {
return true;
} else {
throw new BusinessException(HttpStatus.BAD_REQUEST,
"Validation errors: " + String.join(", ", validationErrors));
}
}
}
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