Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
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 {

@Autowired
private GameListRepository gamelistRepository;

@Autowired
private GameRepository gameRepository;

@Transactional(readOnly = true)
public List<GameListDTO> readAll() {
List<GameList> rGameLists = gamelistRepository.findAll();
Expand All @@ -27,4 +32,19 @@ public GameListDTO readID(Long id) {

return new GameListDTO(rGameList);
}

@Transactional
public void moveGame(Long listID, int currentIndex, int newIndex) {
List<GameMinProjection> 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);
}
}
}