Skip to content

Commit 2d67473

Browse files
refactor: enhance DebugController with logging and update response types
1 parent 6c8cc1e commit 2d67473

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/main/java/preponderous/viron/controllers/DebugController.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Arrays;
55
import java.util.List;
66

7+
import lombok.extern.slf4j.Slf4j;
78
import org.springframework.http.ResponseEntity;
89
import org.springframework.web.bind.annotation.GetMapping;
910
import org.springframework.web.bind.annotation.PathVariable;
@@ -22,6 +23,7 @@
2223

2324
@RestController
2425
@RequestMapping("/api/v1/debug")
26+
@Slf4j
2527
public class DebugController {
2628
private final EntityService entityService;
2729
private final EnvironmentService environmentService;
@@ -40,11 +42,9 @@ public DebugController(EntityService entityService, EnvironmentService environme
4042
/**
4143
* Creates a sample environment with a single 10x10 grid and places ten entities in random, valid locations within the grid.
4244
* It ensures the entities are properly created and assigned to valid locations in the grid.
43-
*
44-
* @return a ResponseEntity containing a Boolean value, true if the sample data was created successfully, false otherwise.
4545
*/
4646
@PostMapping("/create-sample-data")
47-
public ResponseEntity<Boolean> createSampleData() {
47+
public ResponseEntity<Environment> createSampleData() {
4848
// create an environment with one 10x10 grid
4949
Environment environment = environmentService.createEnvironment("Sample Environment", 1, 10);
5050
List<Grid> grids = gridService.getGridsInEnvironment(environment.getEnvironmentId());
@@ -59,46 +59,44 @@ public ResponseEntity<Boolean> createSampleData() {
5959
int x = (int) (Math.random() * grid.getRows());
6060
int y = (int) (Math.random() * grid.getColumns());
6161
Location location = null;
62-
for (int j = 0; j < locations.size(); j++) {
63-
if (locations.get(j).getX() == x && locations.get(j).getY() == y) {
64-
location = locations.get(j);
62+
for (Location value : locations) {
63+
if (value.getX() == x && value.getY() == y) {
64+
location = value;
6565
break;
6666
}
6767
}
6868
if (location == null) {
69-
return ResponseEntity.ok(false);
69+
return ResponseEntity.badRequest().body(null); // exit if no valid location found
7070
}
7171
locationService.addEntityToLocation(entity.getEntityId(), location.getLocationId());
7272
}
7373

74-
return ResponseEntity.ok(true);
74+
return ResponseEntity.ok(environment);
7575
}
7676

7777
/**
7878
* Creates an environment with a single grid of fixed size, generates a random entity,
7979
* and places the entity at a random valid location within the grid.
8080
*
8181
* @param environmentName the name of the environment to be created
82-
* @return a ResponseEntity containing a Boolean value, true if the entity was successfully created
83-
* and placed in a location, false if the operation failed
8482
*/
8583
@PostMapping("/create-world-and-place-entity/{environmentName}")
86-
public ResponseEntity<Boolean> createWorldAndPlaceEntity(@PathVariable String environmentName) {
84+
public ResponseEntity<Entity> createWorldAndPlaceEntity(@PathVariable String environmentName) {
8785
// create an environment
8886
int numGrids = 1;
8987
int gridSize = 5;
9088
Environment environment = environmentService.createEnvironment(environmentName, numGrids, gridSize);
91-
System.out.println("Environment created: " + environment.getName());
89+
log.info("Environment created: {} with ID {}", environment.getName(), environment.getEnvironmentId());
9290

9391
// get grid info
9492
List<Grid> grids = gridService.getGridsInEnvironment(environment.getEnvironmentId());
9593
Grid grid = grids.get(0);
96-
System.out.println("Grid in environment has " + grid.getRows() + " rows and " + grid.getColumns() + " columns");
94+
log.info("Grid created: {} with size {}x{}", grid.getGridId(), grid.getRows(), grid.getColumns());
9795

9896
// create an entity
9997
String entityName = entityNamePool.get((int) (Math.random() * entityNamePool.size()));
10098
Entity entity = entityService.createEntity(entityName);
101-
System.out.println("Entity created: " + entity.getName());
99+
log.info("Entity created: {}", entity.getName());
102100

103101
// place entity in grid
104102
int entityRow = (int) (Math.random() * grid.getRows());
@@ -112,11 +110,11 @@ public ResponseEntity<Boolean> createWorldAndPlaceEntity(@PathVariable String en
112110
}
113111
}
114112
if (location == null) {
115-
// exit
116-
System.out.println("Location not found. Exiting...");
113+
log.error("No valid location found for entity at row {} and column {}", entityRow, entityColumn);
114+
return ResponseEntity.badRequest().body(null);
117115
}
118116
locationService.addEntityToLocation(entity.getEntityId(), location.getLocationId());
119-
System.out.println("Entity placed in grid at row " + entityRow + " and column " + entityColumn);
120-
return ResponseEntity.ok(true);
117+
log.info("Entity {} placed at location ({}, {})", entity.getName(), entityRow, entityColumn);
118+
return ResponseEntity.ok(entity);
121119
}
122120
}

0 commit comments

Comments
 (0)