From 81dd18ac70b57d9189245b011ece0dc11a990df5 Mon Sep 17 00:00:00 2001 From: yooonwodyd Date: Fri, 22 Dec 2023 17:57:36 +0900 Subject: [PATCH] =?UTF-8?q?service=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HappyNewHere/service/AccountService.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/com/example/HappyNewHere/service/AccountService.java b/src/main/java/com/example/HappyNewHere/service/AccountService.java index 81223e3..f9c6e74 100644 --- a/src/main/java/com/example/HappyNewHere/service/AccountService.java +++ b/src/main/java/com/example/HappyNewHere/service/AccountService.java @@ -49,6 +49,10 @@ public AccountDto addUserId(Long accountId, String userId) { Optional account = accountRepository.findById(accountId); if(!account.isPresent()) throw new HappyException(ErrorCode.USER_NOT_FOUND); + + if(!checkUserId(userId)) + throw new HappyException(ErrorCode.DUPLICATED_USER_NAME); + account.get().setUserId(userId); accountRepository.save(account.get()); return AccountDto.from(account.get()); @@ -91,10 +95,23 @@ public AccountDto updateAccount(Long accountId, AccountRequestDto accountRequest Optional account = accountRepository.findById(accountId); if(!account.isPresent()) throw new HappyException(ErrorCode.USER_NOT_FOUND); + if(!checkUserId(accountRequestDto.userId())) + throw new HappyException(ErrorCode.DUPLICATED_USER_NAME); // 있다면 userId가 동일한지 확인하고 다르다면 예외 반환 if(!account.get().getUserId().equals(accountRequestDto.userId())) throw new HappyException(ErrorCode.INVALID_PERMISSION); // 이미 존재하는 유저이며, userId가 동일하다면 해당 유저의 정보를 수정한다. return AccountDto.from(accountRepository.save(Account.of(accountId, accountRequestDto))); } + + //userId 중복체크 + public boolean checkUserId(String userId){ + if(userId.equals(null) || userId.equals("")){ + return true; + } + if (accountRepository.findByUserId(userId).isPresent()) { + return false; + } + return true; + } }