I am trying to save list of admins and I need to validate unique username and email address. First, I need to validate that condition within the list of my request DTOs and then check it one at a time again with the list that is already saved in my database.
I checked RequestDTO using distinct once for username and email address. Then again, I checked with the database. Doing this way, multiple loops are executed.
I referred following ways already :
Java stream that is distinct by more than one property
java 8 how to get distinct list on more than one property
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "This is admin request class")
public class AdminRequestDTO implements Serializable {
@ApiModelProperty(value = "This is the id of admin")
private Long id;
@ApiModelProperty(value = "This is the username of admin")
private String username;
@ApiModelProperty(value = "This is full name of admin")
private String fullName;
@ApiModelProperty(value = "This is the email address of admin")
private String emailAddress;
@ApiModelProperty(value = "This is the status of admin")
private Character status;
}
@Entity
@Getter
@Setter
@Table(name = "admin")
public class Admin implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "fullname")
private String fullName;
@Column(name = "password")
private String password;
@Column(name = "email_address", length = 200)
private String emailAddress;
}
public void saveAdmin(List<AdminRequestDTO> adminRequestDTO) {
validateAdminRequestDTO.accept(adminRequestDTO);
List<Admin> admin = adminRequestDTO.stream().map(requestDTO -> {
/*CHECK IF USERNAME AND EMAIL ADDRESS IN REQUESTDTO EXISTS IN DATABASE, ID IT DOES, THEN THROW EXCEPTION*/
/*ELSE CONVERT */
Admin admin1 = new Admin();
admin1.setUsername(requestDTO.getUsername());
/*SET OTHER REQUEST DTOS*/
return admin1;
}).collect(Collectors.toList());
/*SAVE ADMIN*/
adminRepository.saveAll(admin);
}
public Consumer<List<AdminRequestDTO>> validateAdminRequestDTO = (requestDTO) -> {
boolean checkIfRequestHaveSameUsername = requestDTO.stream().map(AdminRequestDTO::getUsername)
.distinct().count() < requestDTO.size();
boolean checkIfRequestHaveSameEmailAddress = requestDTO.stream().map(AdminRequestDTO::getEmailAddress)
.distinct().count() < requestDTO.size();
if (checkIfRequestHaveSameUsername)
throw new DataDuplicationException(AdminMessageConstants.DUPLICATE_USERNAME_MESSAGE,
AdminMessageConstants.DUPLICATE_USERNAME_DEVELOPER_MESSAGE);
if (checkIfRequestHaveSameEmailAddress)
throw new DataDuplicationException(AdminMessageConstants.DUPLICATE_EMAILADDRESS_MESSAGE,
AdminMessageConstants.DUPLICATE_EMAILADDRESS_DEVELOPER_MESSAGE);
};
public Consumer<AdminRequestDTO> validateAdmin = (requestDTO) -> {
if (!Objects.isNull(adminRepository.getAdminByUsername(requestDTO.getUsername()))) ;
throw new DataDuplicationException("Admin username already exits", "Admin username already exits");
/*SIMILARLY FOR EMAIL ADDRESS*/
};
I want to refactor the existing code so that the multiple loops could be reduced.
abc abc@f1soft.com , xyz xyz@f1soft.com -> allowed
abc abc@f1soft.com, abc xyz@f1soft.com -> not allowed
abc abc@f1soft.com, xyz abc@f1soft.com -> not allowed
(first is username and second is email address)