4
4
import java .util .Arrays ;
5
5
import java .util .List ;
6
6
7
+ import lombok .extern .slf4j .Slf4j ;
7
8
import org .springframework .http .ResponseEntity ;
8
9
import org .springframework .web .bind .annotation .GetMapping ;
9
10
import org .springframework .web .bind .annotation .PathVariable ;
22
23
23
24
@ RestController
24
25
@ RequestMapping ("/api/v1/debug" )
26
+ @ Slf4j
25
27
public class DebugController {
26
28
private final EntityService entityService ;
27
29
private final EnvironmentService environmentService ;
@@ -40,11 +42,9 @@ public DebugController(EntityService entityService, EnvironmentService environme
40
42
/**
41
43
* Creates a sample environment with a single 10x10 grid and places ten entities in random, valid locations within the grid.
42
44
* 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.
45
45
*/
46
46
@ PostMapping ("/create-sample-data" )
47
- public ResponseEntity <Boolean > createSampleData () {
47
+ public ResponseEntity <Environment > createSampleData () {
48
48
// create an environment with one 10x10 grid
49
49
Environment environment = environmentService .createEnvironment ("Sample Environment" , 1 , 10 );
50
50
List <Grid > grids = gridService .getGridsInEnvironment (environment .getEnvironmentId ());
@@ -59,46 +59,44 @@ public ResponseEntity<Boolean> createSampleData() {
59
59
int x = (int ) (Math .random () * grid .getRows ());
60
60
int y = (int ) (Math .random () * grid .getColumns ());
61
61
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 ;
65
65
break ;
66
66
}
67
67
}
68
68
if (location == null ) {
69
- return ResponseEntity .ok ( false );
69
+ return ResponseEntity .badRequest (). body ( null ); // exit if no valid location found
70
70
}
71
71
locationService .addEntityToLocation (entity .getEntityId (), location .getLocationId ());
72
72
}
73
73
74
- return ResponseEntity .ok (true );
74
+ return ResponseEntity .ok (environment );
75
75
}
76
76
77
77
/**
78
78
* Creates an environment with a single grid of fixed size, generates a random entity,
79
79
* and places the entity at a random valid location within the grid.
80
80
*
81
81
* @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
84
82
*/
85
83
@ PostMapping ("/create-world-and-place-entity/{environmentName}" )
86
- public ResponseEntity <Boolean > createWorldAndPlaceEntity (@ PathVariable String environmentName ) {
84
+ public ResponseEntity <Entity > createWorldAndPlaceEntity (@ PathVariable String environmentName ) {
87
85
// create an environment
88
86
int numGrids = 1 ;
89
87
int gridSize = 5 ;
90
88
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 ());
92
90
93
91
// get grid info
94
92
List <Grid > grids = gridService .getGridsInEnvironment (environment .getEnvironmentId ());
95
93
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 ());
97
95
98
96
// create an entity
99
97
String entityName = entityNamePool .get ((int ) (Math .random () * entityNamePool .size ()));
100
98
Entity entity = entityService .createEntity (entityName );
101
- System . out . println ("Entity created: " + entity .getName ());
99
+ log . info ("Entity created: {}" , entity .getName ());
102
100
103
101
// place entity in grid
104
102
int entityRow = (int ) (Math .random () * grid .getRows ());
@@ -112,11 +110,11 @@ public ResponseEntity<Boolean> createWorldAndPlaceEntity(@PathVariable String en
112
110
}
113
111
}
114
112
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 );
117
115
}
118
116
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 );
121
119
}
122
120
}
0 commit comments