TIL(20240716) [트렐로프로젝트- 카드순서이동 구현]
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
33
@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.