Skip to content

Commit

Permalink
Merge pull request #76 from Jhsysng/feat/user
Browse files Browse the repository at this point in the history
Feat/user
  • Loading branch information
Jhsysng authored May 2, 2024
2 parents e539f15 + fc1e537 commit 98d02d1
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package goorm.reinput.global.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class GlobalControllerAdvice extends ResponseEntityExceptionHandler {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package goorm.reinput.global.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@Builder
@NoArgsConstructor
@Getter
public class ErrorResponseDto {

private String code;
private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package goorm.reinput.user.exception;

public class CustomUserException extends RuntimeException {
private final UserErrorCode errorCode;

public CustomUserException(UserErrorCode errorCode) {
this.errorCode = errorCode;
}
}
16 changes: 16 additions & 0 deletions src/main/java/goorm/reinput/user/exception/UserErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package goorm.reinput.user.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@AllArgsConstructor
@Getter
public enum UserErrorCode {
USER_NOT_FOUND("UserEx001", "์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.", HttpStatus.NOT_FOUND),
USER_PASSWORD_NOT_MATCH("UserEx002", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.", HttpStatus.BAD_REQUEST);

private final String code;
private final String message;
private final HttpStatus httpStatus;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package goorm.reinput.user.repository;

import goorm.reinput.user.domain.User;
import goorm.reinput.user.repository.support.UserRepositoryCustom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long>{
public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {

Optional<User> findByUserId(Long userId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package goorm.reinput.user.repository.support;

import goorm.reinput.user.domain.User;

import java.util.Optional;

public interface UserRepositoryCustom {
Optional<User> findByUserEmail(String userEmail);
void deactivateUserByUserId(Long userId);
void deleteByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
package goorm.reinput.user.repository;
package goorm.reinput.user.repository.support;

import com.querydsl.jpa.impl.JPAQueryFactory;
import goorm.reinput.user.domain.User;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Optional;

import static goorm.reinput.user.domain.QUser.user;

@Slf4j
@Repository
@RequiredArgsConstructor
public class CustomUserRepository {
private final EntityManager entityManager;
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
private final JPAQueryFactory queryFactory;
@Autowired
public CustomUserRepository(EntityManager entityManager) {
this.entityManager = entityManager;
this.queryFactory = new JPAQueryFactory(this.entityManager);
}

@Override
public Optional<User> findByUserEmail(String userEmail) {
return Optional.ofNullable(queryFactory.selectFrom(user)
.where(user.userEmail.eq(userEmail))
.fetchOne());
}
@Modifying(clearAutomatically = true)

@Override
public void deactivateUserByUserId(Long userId) {
queryFactory.update(user)
.where(user.userId.eq(userId))
.set(user.isEnable, false)
.execute();
}

@Modifying(clearAutomatically = true)
@Override
public void deleteByUserId(Long userId) {
queryFactory.delete(user)
.where(user.userId.eq(userId))
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/goorm/reinput/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import goorm.reinput.user.domain.dto.TokenResponseDto;
import goorm.reinput.user.domain.dto.UserLoginDto;
import goorm.reinput.user.domain.dto.UserSignupDto;
import goorm.reinput.user.repository.CustomUserRepository;
import goorm.reinput.user.exception.CustomUserException;
import goorm.reinput.user.exception.UserErrorCode;
import goorm.reinput.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,11 +19,11 @@

@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
private final CustomUserRepository customUserRepository;
private final FolderRepository folderRepository;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final TokenProvider tokenProvider;
Expand Down Expand Up @@ -53,12 +54,12 @@ public void signUp(UserSignupDto userSignUpDto) {
public TokenResponseDto login(UserLoginDto userLoginDto){
log.info("[UserService] login userEmail : {}", userLoginDto.getUserEmail());
//todo : login useremail custom exception
User user = customUserRepository.findByUserEmail(userLoginDto.getUserEmail())
.orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. : " + userLoginDto.getUserEmail()));
User user = userRepository.findByUserEmail(userLoginDto.getUserEmail())
.orElseThrow(() -> new CustomUserException(UserErrorCode.USER_NOT_FOUND));

if (!bCryptPasswordEncoder.matches(userLoginDto.getUserPassword(), user.getUserPassword())) {
//todo : login password custom exception
throw new IllegalArgumentException("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
log.warn("[UserService] login password not match userEmail : {}", userLoginDto.getUserEmail());
throw new CustomUserException(UserErrorCode.USER_PASSWORD_NOT_MATCH);
}

return TokenResponseDto.builder()
Expand All @@ -78,7 +79,7 @@ public TokenResponseDto reissueAccessToken(String refreshToken) {

public void deactivate(Long userId) {
log.info("[UserService] delete userId : {}", userId);
customUserRepository.deactivateUserByUserId(userId);
userRepository.deactivateUserByUserId(userId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CustomUserRepositoryTest {
private EntityManager em;
private JPAQueryFactory queryFactory;
@Autowired
private CustomUserRepository customUserRepository;
private UserRepository userRepository;


@BeforeEach
Expand All @@ -47,14 +47,14 @@ public void testEntity(){
}
@Test
public void findByUserEmail() {
User user = customUserRepository.findByUserEmail("user1").get();
User user = userRepository.findByUserEmail("user1").get();
assertEquals("user1", user.getUserEmail());
}

@Test
public void deactivateUserByUserId() {
customUserRepository.deactivateUserByUserId(1L);
User user = customUserRepository.findByUserEmail("user1").get();
userRepository.deactivateUserByUserId(1L);
User user = userRepository.findByUserEmail("user1").get();
Assertions.assertThat(user.isEnable()).isFalse();
}

Expand Down

0 comments on commit 98d02d1

Please sign in to comment.