responseObj = new HashMap<>();
- responseObj.put("email", "Email taken");
+ if(emailAlreadyExists(member.getEmail())){
+ responseObj.put("email", "Email taken");
+ }
+ if(nameAlreadyExists(member.getName())){
+ responseObj.put("name", "Name taken");
+ }
builder = Response.status(Response.Status.CONFLICT).entity(responseObj);
} catch (Exception e) {
// Handle generic exceptions
@@ -145,6 +150,9 @@ private void validateMember(Member member) throws ConstraintViolationException,
if (emailAlreadyExists(member.getEmail())) {
throw new ValidationException("Unique Email Violation");
}
+ if (nameAlreadyExists(member.getName())) {
+ throw new ValidationException("Unique Name Violation");
+ }
}
/**
@@ -182,4 +190,20 @@ public boolean emailAlreadyExists(String email) {
}
return member != null;
}
+ /**
+ * Checks if a member with the same name address is already registered. This is the only way to easily capture the
+ * "@UniqueConstraint(columnNames = "name")" constraint from the Member class.
+ *
+ * @param name The email to check
+ * @return True if the name already exists, and false otherwise
+ */
+ public boolean nameAlreadyExists(String name) {
+ Member member = null;
+ try {
+ member = repository.findByName(name);
+ } catch (NoResultException e) {
+ // ignore
+ }
+ return member != null;
+ }
}
diff --git a/src/main/webapp/partials/home.html b/src/main/webapp/partials/home.html
index 5433edd..e1edecd 100644
--- a/src/main/webapp/partials/home.html
+++ b/src/main/webapp/partials/home.html
@@ -46,6 +46,11 @@ Member Registration
{{errors.phoneNumber}}
+
+
+
+ {{errors.address}}
+