@@ -57,16 +57,17 @@ public World db() {
5757 * GET /queries?queries=10 HTTP/1.1
5858 */
5959 @ GetMapping ("/queries" )
60- public World [] queries (@ RequestParam (defaultValue = "1" ) Integer queries ) {
61- return Utils .randomWorldNumbers ().mapToObj (dbRepository ::getWorld ). limit ( queries ).toArray (World []::new );
60+ public World [] queries (@ RequestParam (defaultValue = "1" ) String queries ) {
61+ return Utils .randomWorldNumbers ().limit ( parseQueryCount ( queries )). mapToObj (dbRepository ::getWorld ).toArray (World []::new );
6262 }
6363
6464 /**
6565 * GET /updates?queries=10 HTTP/1.1
6666 */
6767 @ GetMapping ("/updates" )
68- public List <World > updates (@ RequestParam (defaultValue = "1" ) Integer queries ) {
68+ public List <World > updates (@ RequestParam (defaultValue = "1" ) String queries ) {
6969 List <World > worlds = Utils .randomWorldNumbers ()
70+ .limit (parseQueryCount (queries ))
7071 .mapToObj (id -> {
7172 World world = dbRepository .getWorld (id );
7273 int randomNumber ;
@@ -76,7 +77,6 @@ public List<World> updates(@RequestParam(defaultValue = "1") Integer queries) {
7677 world .randomNumber = randomNumber ;
7778 return world ;
7879 })
79- .limit (queries )
8080 .sorted (Comparator .comparingInt (w -> w .id ))
8181 .toList ();
8282 dbRepository .updateWorlds (worlds );
@@ -95,4 +95,17 @@ public String fortunes() {
9595
9696 return JStachio .render (new Fortunes (fortunes ));
9797 }
98+
99+ private static int parseQueryCount (String textValue ) {
100+ if (textValue == null ) {
101+ return 1 ;
102+ }
103+ int parsedValue ;
104+ try {
105+ parsedValue = Integer .parseInt (textValue );
106+ } catch (NumberFormatException e ) {
107+ return 1 ;
108+ }
109+ return Math .min (500 , Math .max (1 , parsedValue ));
110+ }
98111}
0 commit comments