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