Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.halil.halil.domain.user.advice;

import com.halil.halil.global.response.CommonResponse;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class UserControllerAdvice {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<CommonResponse> invalidDtoException(MethodArgumentNotValidException e){
return new ResponseEntity<>(CommonResponse.createError("take a form plz"), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<CommonResponse> ExistUserNickNameException(DataIntegrityViolationException e){
return new ResponseEntity<>(CommonResponse.createError("Already Exist NickName"), HttpStatus.BAD_REQUEST);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import com.halil.halil.domain.user.service.UserService;
import com.halil.halil.global.response.CommonResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -31,16 +31,8 @@ public ResponseEntity<?> updateUserInfo(HttpServletRequest request, @RequestBody
}

@PostMapping("/create")
ResponseEntity<CommonResponse> CreateUser(@RequestBody @Valid UserCreateRequestDto userCreateRequestDto){
UserCreateResponseDto userCreateResponseDto = userService.CreateUser(userCreateRequestDto);
ResponseEntity<CommonResponse> createUser(@RequestBody @Valid UserCreateRequestDto userCreateRequestDto){
UserCreateResponseDto userCreateResponseDto = userService.createUser(userCreateRequestDto);
return new ResponseEntity<>(CommonResponse.createSuccess(userCreateResponseDto), HttpStatus.OK);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public String invalidUserException(MethodArgumentNotValidException e){
return "take a form";
}
@ExceptionHandler(DataIntegrityViolationException.class)
public String nullTypeUserException(DataIntegrityViolationException e){
return "Already Exist User";
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package com.halil.halil.domain.user.dto;

import com.halil.halil.domain.user.entity.User;
import lombok.*;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserCreateRequestDto {
@NotNull
@NotBlank
@Email
String email;
@NotNull
@NotBlank
String nickName;

public User toEntity(){
return User.builder()
.email(email)
.nickname(nickName)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
public class UserResponseDto {

private String email;
private String nickname;
private String nickName;

public UserResponseDto(User user){
this.email = user.getEmail();
this.nickname = user.getNickname();
this.nickName = user.getNickName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class UserUpdateRequestDto {
private String email;

@NotBlank(message = "닉네임은 필수 입력값입니다.")
private String nickname;
private String nickName;

}
10 changes: 6 additions & 4 deletions src/main/java/com/halil/halil/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import lombok.*;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;

@Entity
@Getter
@Table(name = "USERS")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class User {
@Id
Expand All @@ -15,7 +17,7 @@ public class User {
private Long user_id;

@Column(name = "NICKNAME", unique = true, nullable = false)
private String nickname;
private String nickName;

@Column(name = "EMAIL", unique = true, nullable = false)
private String email;
Expand All @@ -25,12 +27,12 @@ public class User {

@Builder
public User( String nickname, String email){
this.nickname = nickname;
this.nickName = nickname;
this.email = email;
}

public void update(String nickname){
this.nickname = nickname;
public void update(String nickName){
this.nickName = nickName;
}

public void updateRefreshToken(String refreshToken){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.halil.halil.domain.user.exception;

public class ExistUserException extends RuntimeException{
public ExistUserException(String message){
super(message);
import org.springframework.dao.DataIntegrityViolationException;

public class ExistUserException extends DataIntegrityViolationException {
public ExistUserException(){
super("Already Exist User!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findUserByEmail(String email);
Optional<User> findUserByNickName(String nickName);
Optional<User> findByEmail(String email);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
public interface UserService {
UserResponseDto updateUserInfo(String email, UserUpdateRequestDto userUpdateRequestDto);
void updateRefreshToken(String email, String refreshToken);
UserCreateResponseDto CreateUser(UserCreateRequestDto userCreateRequestDto);
UserCreateResponseDto createUser(UserCreateRequestDto userCreateRequestDto);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package com.halil.halil.domain.user.service;

import com.halil.halil.domain.user.dto.UserCreateRequestDto;
import com.halil.halil.domain.user.dto.UserCreateResponseDto;
import com.halil.halil.domain.user.dto.UserResponseDto;
import com.halil.halil.domain.user.dto.UserUpdateRequestDto;
import com.halil.halil.domain.user.entity.User;
import com.halil.halil.domain.user.exception.ExistUserException;
import com.halil.halil.domain.user.repository.UserRepository;
import com.halil.halil.global.exception.ExistNicknameException;
import com.halil.halil.global.exception.NoExistUserException;
import com.halil.halil.global.util.jwt.JwtProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.NoSuchElementException;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService{
private final UserRepository userRepository;

private final JwtProvider jwtProvider;
@Override
public UserResponseDto updateUserInfo(String email, UserUpdateRequestDto userUpdateRequestDto) {
User user = userRepository.findByEmail(email).orElseThrow(() -> new NoExistUserException("존재하는 회원정보가 없습니다."));
try{
user.update(userUpdateRequestDto.getNickname());
userRepository.save(user);
UserResponseDto userResponseDto = new UserResponseDto(user);
return userResponseDto;
}catch (DataIntegrityViolationException e){
throw new ExistNicknameException("존재하는 닉네임입니다.");
}
user.update(userUpdateRequestDto.getNickName());
userRepository.save(user);
UserResponseDto userResponseDto = new UserResponseDto(user);
return userResponseDto;
}

@Override
Expand All @@ -37,6 +34,15 @@ public void updateRefreshToken(String email, String refreshToken) {
user.updateRefreshToken(refreshToken);
userRepository.save(user);
}


@Override
public UserCreateResponseDto createUser(UserCreateRequestDto userCreateRequestDto){
try {
userRepository.save(userCreateRequestDto.toEntity());
String accessToken = jwtProvider.createAccessToken(userCreateRequestDto.getNickName(), userCreateRequestDto.getEmail());
String refreshToken = jwtProvider.createRefreshToken(userCreateRequestDto.getNickName(), userCreateRequestDto.getEmail());
return new UserCreateResponseDto(accessToken, refreshToken);
}catch (ExistUserException e){
throw new ExistUserException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.halil.halil.global.cofing;

import com.halil.halil.global.util.JwtInterceptor;
import com.halil.halil.global.util.jwt.JwtInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.halil.halil.global.util;
package com.halil.halil.global.util.jwt;

import com.halil.halil.domain.user.service.UserService;
import io.jsonwebtoken.ExpiredJwtException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class JwtProvider {

@Value("${jwt.secret}")
private String SECRET_KEY;

private Long accessTokenExpired = 1000L * 60;
private Long refreshTokenExpired = 1000L * 60 * 60 * 24;
public String createAccessToken(String nickName, String email) {
Long tokenInvalidTime = 1000L * 60 * 3; // 3m
return this.createToken(email, nickName, tokenInvalidTime);
}

public String createRefreshToken(String nickName, String email) {
Long tokenInvalidTime = 1000L * 60 * 60 * 24; // 1d
String refreshToken = this.createToken(nickName, email, tokenInvalidTime);
String refreshToken = this.createToken(nickName, email, refreshTokenExpired);
return refreshToken;
}

Expand All @@ -31,7 +31,7 @@ private String createToken(String nickName, String email, Long tokenInvalidTime)
.claim("nickName",nickName)
.claim("email",email)
.setIssuedAt(date)
.setExpiration(new Date(date.getTime() + tokenInvalidTime))
.setExpiration(new Date(date.getTime() + accessTokenExpired))
.signWith(SignatureAlgorithm.HS256, SECRET_KEY)
.compact();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.halil.halil.global.util;
package com.halil.halil.global.util.jwt;

import com.fasterxml.jackson.core.JsonProcessingException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.halil.halil.global.util;
package com.halil.halil.global.util.jwt;

import com.halil.halil.domain.user.entity.User;
import com.halil.halil.domain.user.repository.UserRepository;
Expand Down
19 changes: 5 additions & 14 deletions src/test/java/com/halil/halil/domain/user/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package com.halil.halil.domain.user;

import com.halil.halil.domain.user.dto.UserCreateRequestDto;
import com.halil.halil.domain.user.entity.User;
import com.halil.halil.domain.user.exception.ExistUserException;
import com.halil.halil.domain.user.repository.UserRepository;
import com.halil.halil.domain.user.service.UserCreateServiceImpl;
import com.halil.halil.domain.user.service.UserService;
import com.halil.halil.domain.user.service.UserServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;

import static org.junit.jupiter.api.Assertions.*;
import java.util.Optional;

@SpringBootTest
class UserServiceTest {
@Autowired
private UserCreateServiceImpl userCreateServiceImpl;
private UserServiceImpl userCreateServiceImpl;
@Autowired
private UserRepository userRepository;

Expand All @@ -36,13 +27,13 @@ void Setup(){
@DisplayName("Already Exist User Exception Test")
void AlreadyExistUserTest(){
UserCreateRequestDto dupUserCreateRequestDto = new UserCreateRequestDto("email1","nickname1");
userCreateServiceImpl.CreateUser(userCreateRequestDto1);
Assertions.assertThrows(DataIntegrityViolationException.class, () -> userCreateServiceImpl.CreateUser(dupUserCreateRequestDto));
userCreateServiceImpl.createUser(userCreateRequestDto1);
Assertions.assertThrows(DataIntegrityViolationException.class, () -> userCreateServiceImpl.createUser(dupUserCreateRequestDto));
}
@Test
@DisplayName("No nickName in User Test")
void NotExistUserNameTest(){
UserCreateRequestDto userCreateRequestDto = new UserCreateRequestDto("nickname",null);
Assertions.assertThrows(DataIntegrityViolationException.class, () -> userCreateServiceImpl.CreateUser(userCreateRequestDto));
Assertions.assertThrows(DataIntegrityViolationException.class, () -> userCreateServiceImpl.createUser(userCreateRequestDto));
}
}