TIL(20240717) [카드 순서이동 기능-테스트코드 작성하기]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Transactional
public void moveCardToPosition(Long cardId, int newPosition, Long userId) {
User user = userRepository.findById(userId).orElseThrow(()-> new CustomException(ErrorEnum.USER_NOT_AUTHENTICATED));
Card card = findCardById(cardId);
card.checkUser(user);
Columns column = card.getColumns();
int currentPosition = card.getPosition();
List<Card> cards = cardRepository.findByColumnIdOrderByPositionAsc(column.getId());
if (newPosition < 0 || newPosition >= cards.size()) {
throw new CustomException(ErrorEnum.WRONG_POSITION_NUMBER);
}
if(currentPosition < newPosition) { // current 3 newPosition 4
for(Card c : cards) { // 4 , 5
if (c.getPosition() > currentPosition && c.getPosition() <= newPosition) {
c.setPosition(c.getPosition() - 1);
cardRepository.save(c);
}
}
} else {
for(Card c : cards) {
if(c.getPosition() >= newPosition && c.getPosition() < currentPosition) {
c.setPosition(c.getPosition() + 1);
cardRepository.save(c);
}
}
}
updateCardPosition(cardId, newPosition);
reorderCards(column.getId());
}
단위테스트, 통합테스트 진행하기
This post is licensed under CC BY 4.0 by the author.