From a728a5fc7f121d954746d9ddb85e6cb5d565a939 Mon Sep 17 00:00:00 2001 From: Tyler Date: Tue, 26 Jun 2018 16:08:55 -0700 Subject: [PATCH] Fixing unique user name login bug. --- .../main/java/com/simplevote/db/Actions.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/service/src/main/java/com/simplevote/db/Actions.java b/service/src/main/java/com/simplevote/db/Actions.java index f216e87..8fe5c91 100755 --- a/service/src/main/java/com/simplevote/db/Actions.java +++ b/service/src/main/java/com/simplevote/db/Actions.java @@ -10,6 +10,8 @@ import com.simplevote.types.User; import org.javalite.activejdbc.LazyList; +import org.javalite.activejdbc.Model; + import ch.qos.logback.classic.Logger; import org.slf4j.LoggerFactory; @@ -60,11 +62,21 @@ public static User signup(Long loggedInUserId, String userName, String password, } // Find the user, then create a login for them + + LazyList users; + if (email != null) { + users = Tables.User.find("name = ? or email = ?", userName, email); + } else { + users = Tables.User.find("name = ?", userName); + } + Tables.User uv; - if (email != null) { - uv = Tables.User.findFirst("name = ? or email = ?", userName, email); + if (users.size() > 1) { + throw new NoSuchElementException("Username/email already exists"); + } else if (users.size() == 1) { + uv = users.get(0); } else { - uv = Tables.User.findFirst("name = ?", userName); + uv = null; } if (uv == null) {