Skip to content

Commit bbda455

Browse files
committed
cria método para movimentar um jogo dentro de uma lista específica
1 parent 20e24c4 commit bbda455

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/main/java/com/gabriel_torelo/game_list/services/GameListService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
import org.springframework.transaction.annotation.Transactional;
77
import com.gabriel_torelo.game_list.dto.GameListDTO;
88
import com.gabriel_torelo.game_list.entities.GameList;
9+
import com.gabriel_torelo.game_list.projections.GameMinProjection;
910
import com.gabriel_torelo.game_list.repositories.GameListRepository;
11+
import com.gabriel_torelo.game_list.repositories.GameRepository;
1012

1113
@Service
1214
public class GameListService {
1315

1416
@Autowired
1517
private GameListRepository gamelistRepository;
1618

19+
@Autowired
20+
private GameRepository gameRepository;
21+
1722
@Transactional(readOnly = true)
1823
public List<GameListDTO> readAll() {
1924
List<GameList> rGameLists = gamelistRepository.findAll();
@@ -27,4 +32,19 @@ public GameListDTO readID(Long id) {
2732

2833
return new GameListDTO(rGameList);
2934
}
35+
36+
@Transactional
37+
public void moveGame(Long listID, int currentIndex, int newIndex) {
38+
List<GameMinProjection> rGameListProj = gameRepository.readListID(listID);
39+
GameMinProjection rGameRemvd = rGameListProj.remove(currentIndex);
40+
41+
rGameListProj.add(newIndex, rGameRemvd);
42+
43+
int startRange = currentIndex < newIndex ? currentIndex : newIndex;
44+
int endRange = currentIndex > newIndex ? currentIndex : newIndex;
45+
46+
for (int i = startRange; i <= endRange; i++) {
47+
gamelistRepository.moveGameInList(listID, rGameListProj.get(i).getId(), i);
48+
}
49+
}
3050
}

0 commit comments

Comments
 (0)