66import org .springframework .transaction .annotation .Transactional ;
77import com .gabriel_torelo .game_list .dto .GameListDTO ;
88import com .gabriel_torelo .game_list .entities .GameList ;
9+ import com .gabriel_torelo .game_list .projections .GameMinProjection ;
910import com .gabriel_torelo .game_list .repositories .GameListRepository ;
11+ import com .gabriel_torelo .game_list .repositories .GameRepository ;
1012
1113@ Service
1214public 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