duplication exception
This commit is contained in:
@@ -12,4 +12,4 @@ public interface PostCommandRepository extends CrudRepository<Post, Long> {
|
|||||||
Optional<Post> findByIdAndWriter(Long id, UserEntity writer);
|
Optional<Post> findByIdAndWriter(Long id, UserEntity writer);
|
||||||
void deleteById(Long id);
|
void deleteById(Long id);
|
||||||
void delete(Post post);
|
void delete(Post post);
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,9 @@ import javax.persistence.FetchType;
|
|||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Index;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
import com.example.oneul.domain.user.domain.UserEntity;
|
import com.example.oneul.domain.user.domain.UserEntity;
|
||||||
|
|
||||||
@@ -21,6 +23,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@EntityListeners(AuditingEntityListener.class)
|
@EntityListeners(AuditingEntityListener.class)
|
||||||
|
@Table(indexes = @Index(name = "i_post", columnList="createdAt"))
|
||||||
public class Post {
|
public class Post {
|
||||||
@Id @GeneratedValue(strategy = GenerationType.AUTO)
|
@Id @GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ import java.util.Optional;
|
|||||||
|
|
||||||
import com.example.oneul.domain.user.domain.UserEntity;
|
import com.example.oneul.domain.user.domain.UserEntity;
|
||||||
|
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface UserRepository extends CrudRepository<UserEntity, Long> {
|
public interface UserRepository extends CrudRepository<UserEntity, Long> {
|
||||||
Optional<UserEntity> findByUsername(String username);
|
Optional<UserEntity> findByUsername(String username);
|
||||||
|
UserEntity save(UserEntity userEntity) throws DataIntegrityViolationException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import javax.servlet.http.HttpSession;
|
|||||||
|
|
||||||
import com.example.oneul.domain.user.dao.UserRepository;
|
import com.example.oneul.domain.user.dao.UserRepository;
|
||||||
import com.example.oneul.domain.user.domain.UserEntity;
|
import com.example.oneul.domain.user.domain.UserEntity;
|
||||||
|
import com.example.oneul.domain.user.exception.UserAlreadyExistException;
|
||||||
import com.example.oneul.domain.user.exception.WrongUsernameAndPasswordException;
|
import com.example.oneul.domain.user.exception.WrongUsernameAndPasswordException;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -28,11 +29,15 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Override
|
@Override
|
||||||
@Transactional(isolation = Isolation.SERIALIZABLE)
|
@Transactional(isolation = Isolation.SERIALIZABLE)
|
||||||
public UserEntity signUp(UserEntity userEntity){
|
public UserEntity signUp(UserEntity userEntity){
|
||||||
|
if(userRepository.findByUsername(userEntity.getUsername()).isPresent()){
|
||||||
|
throw new UserAlreadyExistException(userEntity.getUsername() + " is already exists");
|
||||||
|
}
|
||||||
|
|
||||||
UserEntity user = UserEntity.builder().username(userEntity.getUsername())
|
UserEntity user = UserEntity.builder().username(userEntity.getUsername())
|
||||||
.password(
|
.password(
|
||||||
passwordEncoder.encode(userEntity.getPassword()))
|
passwordEncoder.encode(userEntity.getPassword()))
|
||||||
.build();
|
.build();
|
||||||
return userRepository.save(user);
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -14,12 +14,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
|
||||||
protected ResponseEntity<String> handleException(Exception e){
|
|
||||||
log.info(e.getMessage());
|
|
||||||
return new ResponseEntity<>("invalid request", HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExceptionHandler(UserAlreadyExistException.class)
|
@ExceptionHandler(UserAlreadyExistException.class)
|
||||||
protected ResponseEntity<String> handleUserAlreadyExistException(UserAlreadyExistException e){
|
protected ResponseEntity<String> handleUserAlreadyExistException(UserAlreadyExistException e){
|
||||||
log.info(e.getMessage());
|
log.info(e.getMessage());
|
||||||
|
|||||||
@@ -35,9 +35,10 @@ public class UserControllerTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
private MockHttpSession httpSession = new MockHttpSession();
|
private MockHttpSession httpSession = new MockHttpSession();
|
||||||
|
private UserEntity testUser;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp(){
|
public void eachSetUp(){
|
||||||
mvc = MockMvcBuilders.standaloneSetup(new UserApi(userService))
|
mvc = MockMvcBuilders.standaloneSetup(new UserApi(userService))
|
||||||
.addFilters(new CharacterEncodingFilter("UTF-8", true))
|
.addFilters(new CharacterEncodingFilter("UTF-8", true))
|
||||||
.build();
|
.build();
|
||||||
@@ -53,10 +54,12 @@ public class UserControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private UserEntity createTestUser(String username, String password){
|
private UserEntity createTestUser(String username, String password){
|
||||||
return userService.signUp(UserEntity.builder()
|
if(testUser != null)
|
||||||
|
testUser = userService.signUp(UserEntity.builder()
|
||||||
.username(username)
|
.username(username)
|
||||||
.password(password)
|
.password(password)
|
||||||
.build());
|
.build());
|
||||||
|
return testUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user