From bbda455bcdf3c76283f0b076524e3ff983bea01f Mon Sep 17 00:00:00 2001 From: Gabriel Torelo <57268681+GabrielTorelo@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:44:59 -0300 Subject: [PATCH] =?UTF-8?q?cria=20m=C3=A9todo=20para=20movimentar=20um=20j?= =?UTF-8?q?ogo=20dentro=20de=20uma=20lista=20espec=C3=ADfica?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../game_list/services/GameListService.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/gabriel_torelo/game_list/services/GameListService.java b/src/main/java/com/gabriel_torelo/game_list/services/GameListService.java index a3efd7e..3635d76 100644 --- a/src/main/java/com/gabriel_torelo/game_list/services/GameListService.java +++ b/src/main/java/com/gabriel_torelo/game_list/services/GameListService.java @@ -6,7 +6,9 @@ import org.springframework.transaction.annotation.Transactional; import com.gabriel_torelo.game_list.dto.GameListDTO; import com.gabriel_torelo.game_list.entities.GameList; +import com.gabriel_torelo.game_list.projections.GameMinProjection; import com.gabriel_torelo.game_list.repositories.GameListRepository; +import com.gabriel_torelo.game_list.repositories.GameRepository; @Service public class GameListService { @@ -14,6 +16,9 @@ public class GameListService { @Autowired private GameListRepository gamelistRepository; + @Autowired + private GameRepository gameRepository; + @Transactional(readOnly = true) public List readAll() { List rGameLists = gamelistRepository.findAll(); @@ -27,4 +32,19 @@ public GameListDTO readID(Long id) { return new GameListDTO(rGameList); } + + @Transactional + public void moveGame(Long listID, int currentIndex, int newIndex) { + List rGameListProj = gameRepository.readListID(listID); + GameMinProjection rGameRemvd = rGameListProj.remove(currentIndex); + + rGameListProj.add(newIndex, rGameRemvd); + + int startRange = currentIndex < newIndex ? currentIndex : newIndex; + int endRange = currentIndex > newIndex ? currentIndex : newIndex; + + for (int i = startRange; i <= endRange; i++) { + gamelistRepository.moveGameInList(listID, rGameListProj.get(i).getId(), i); + } + } }