Final backend commit
This commit is contained in:
@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.truecaller.truecallerassignment.backend.entities.CBRouteResponse;
|
||||||
import com.truecaller.truecallerassignment.backend.entities.Tile;
|
import com.truecaller.truecallerassignment.backend.entities.Tile;
|
||||||
import com.truecaller.truecallerassignment.backend.services.ChessBoardService;
|
import com.truecaller.truecallerassignment.backend.services.ChessBoardService;
|
||||||
import com.truecaller.truecallerassignment.backend.utilities.ChessBoardUtility;
|
import com.truecaller.truecallerassignment.backend.utilities.ChessBoardUtility;
|
||||||
@@ -22,15 +23,15 @@ public class TruecallerAssignmentController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ChessBoardUtility utility;
|
private ChessBoardUtility utility;
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/findpath")
|
||||||
public List<Tile> findPath(@RequestParam int row, @RequestParam int column) {
|
public List<CBRouteResponse> findPath(@RequestParam int row, @RequestParam int column) {
|
||||||
Set<Tile> allTiles = utility.populateAllTiles();
|
Set<Tile> allTiles = utility.populateAllTiles();
|
||||||
allTiles.stream().forEach(tile -> utility.generateAllowedMoves(tile, allTiles));
|
allTiles.stream().forEach(tile -> utility.generateAllowedMoves(tile, allTiles));
|
||||||
Tile tile = utility.getTile(row, column, allTiles);
|
Tile tile = utility.getTile(row, column, allTiles);
|
||||||
List<Tile> traversedTiles = new ArrayList<Tile>();
|
List<Tile> traversedTiles = new ArrayList<Tile>();
|
||||||
service.findPath(tile, traversedTiles);
|
service.findPath(tile, traversedTiles);
|
||||||
utility.transformResponse(traversedTiles);
|
List<CBRouteResponse> response = utility.transformResponse(traversedTiles);
|
||||||
return traversedTiles;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,4 +4,38 @@ public class CBRouteResponse {
|
|||||||
private int row;
|
private int row;
|
||||||
private int column;
|
private int column;
|
||||||
private int order;
|
private int order;
|
||||||
|
private String allowedMoves;
|
||||||
|
|
||||||
|
public CBRouteResponse(int row, int column, int order) {
|
||||||
|
super();
|
||||||
|
this.row = row;
|
||||||
|
this.column = column;
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRow() {
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
public void setRow(int row) {
|
||||||
|
this.row = row;
|
||||||
|
}
|
||||||
|
public int getColumn() {
|
||||||
|
return column;
|
||||||
|
}
|
||||||
|
public void setColumn(int column) {
|
||||||
|
this.column = column;
|
||||||
|
}
|
||||||
|
public int getOrder() {
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
public void setOrder(int order) {
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
public String getAllowedMoves() {
|
||||||
|
return allowedMoves;
|
||||||
|
}
|
||||||
|
public void setAllowedMoves(String allowedMoves) {
|
||||||
|
this.allowedMoves = allowedMoves;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,9 +10,6 @@ import com.truecaller.truecallerassignment.backend.entities.Tile;
|
|||||||
public class ChessBoardService {
|
public class ChessBoardService {
|
||||||
|
|
||||||
public static void findPath(Tile currentTile, List<Tile> traversedTiles) {
|
public static void findPath(Tile currentTile, List<Tile> traversedTiles) {
|
||||||
if (currentTile.isVisited()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (currentTile.isVisited()) {
|
if (currentTile.isVisited()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.truecaller.truecallerassignment.backend.entities.CBRouteResponse;
|
||||||
import com.truecaller.truecallerassignment.backend.entities.Tile;
|
import com.truecaller.truecallerassignment.backend.entities.Tile;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@@ -108,16 +110,20 @@ public class ChessBoardUtility {
|
|||||||
tile.setAllowedMoves(allowedMoves.stream().filter(tile1 -> !tile1.isVisited()).collect(Collectors.toList()));
|
tile.setAllowedMoves(allowedMoves.stream().filter(tile1 -> !tile1.isVisited()).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void transformResponse(List<Tile> traversedTiles) {
|
public List<CBRouteResponse> transformResponse(List<Tile> traversedTiles) {
|
||||||
traversedTiles = traversedTiles.stream().map(tile -> {
|
AtomicInteger index = new AtomicInteger();
|
||||||
|
List<CBRouteResponse> response = traversedTiles.stream().map(tile -> {
|
||||||
|
CBRouteResponse singleResponse = new CBRouteResponse(tile.getRow(), tile.getColumn(), index.incrementAndGet());
|
||||||
List<Tile> tiles = tile.getAllowedMoves();
|
List<Tile> tiles = tile.getAllowedMoves();
|
||||||
String moves = null;
|
String moves = null;
|
||||||
if (tiles != null) {
|
if (tiles != null) {
|
||||||
moves = tiles.stream().map(tile1 -> tile1.getRow() + "-" + tile1.getColumn()).collect(Collectors.joining(", "));
|
moves = tiles.stream().map(tile1 -> tile1.getRow() + "-" + tile1.getColumn()).collect(Collectors.joining(", "));
|
||||||
}
|
}
|
||||||
tile.setAllowedMoves(null);
|
singleResponse.setAllowedMoves(moves);
|
||||||
tile.setAllowedMovesUI(moves);
|
// tile.setAllowedMoves(null);
|
||||||
return tile;
|
// tile.setAllowedMovesUI(moves);
|
||||||
|
return singleResponse;
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user