Commit 6a01560c authored by Syed Asif's avatar Syed Asif

Merge branch 'future-changes' into 'master'

validation changes done

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