From b550d376b0f62bbe914da26500524acf5be22494 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 21 May 2020 12:10:32 +0300 Subject: Show correct signup errors --- src/main/java/com/juick/service/UserService.java | 3 ++- src/main/java/com/juick/service/UserServiceImpl.java | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'src/main/java/com/juick/service') diff --git a/src/main/java/com/juick/service/UserService.java b/src/main/java/com/juick/service/UserService.java index fbbab0ad..16d76659 100644 --- a/src/main/java/com/juick/service/UserService.java +++ b/src/main/java/com/juick/service/UserService.java @@ -20,6 +20,7 @@ package com.juick.service; import com.juick.model.Message; import com.juick.model.User; import com.juick.model.AuthResponse; +import com.juick.util.UsernameTakenException; import javax.annotation.Nonnull; import java.util.Collection; @@ -39,7 +40,7 @@ public interface UserService { String getSignUpHashByTelegramID(Long telegramId, String username); - Optional createUser(String username, String password); + Optional createUser(String username, String password) throws UsernameTakenException; Optional getUserByUID(int uid); diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java index 84ff1ff5..cf1c838d 100644 --- a/src/main/java/com/juick/service/UserServiceImpl.java +++ b/src/main/java/com/juick/service/UserServiceImpl.java @@ -21,6 +21,7 @@ import com.juick.model.Message; import com.juick.model.User; import com.juick.model.AnonymousUser; import com.juick.model.AuthResponse; +import com.juick.util.UsernameTakenException; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; @@ -108,7 +109,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { @Transactional @Override - public Optional createUser(final String username, final String password) { + public Optional createUser(final String username, final String password) throws UsernameTakenException { KeyHolder holder = new GeneratedKeyHolder(); try { getJdbcTemplate().update( @@ -122,7 +123,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { }, holder); } catch (DuplicateKeyException e) { - return Optional.empty(); + throw new UsernameTakenException(); } int uid = holder.getKeys().size() > 1 ? (int)holder.getKeys().get("id") : holder.getKey().intValue(); -- cgit v1.2.3