package com.techempower.spring.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.techempower.spring.domain.World;
import com.techempower.spring.repository.WorldRepository;

@RestController
final class WorldDatabaseController {

	private static final int DB_ROWS = 10000;

	@Autowired
	private WorldRepository worldRepository;

	@RequestMapping(value = "/db", produces = "application/json")
	World singleQuery() {
		final Random random = ThreadLocalRandom.current();
		return this.worldRepository.findOne(random.nextInt(DB_ROWS) + 1);
	}

	@RequestMapping(value = "/queries", produces = "application/json")
	List<World> multipleQueries(@RequestParam("queries") Integer rawQueryCount) {
		Integer queryCount = boundQueryCount(rawQueryCount);

		List<World> worlds = new ArrayList<World>(queryCount);
		Random random = ThreadLocalRandom.current();

		for (int i = 0; i < queryCount; i++) {
			worlds.add(this.worldRepository.findOne(random.nextInt(DB_ROWS) + 1));
		}

		return worlds;
	}

	@RequestMapping(value = "/updates", produces = "application/json")
	List<World> updateQueries(@RequestParam(value="queries", required=false, defaultValue="1") Integer rawQueryCount) {
		Integer queryCount = boundQueryCount(rawQueryCount);

		List<World> worlds = new ArrayList<World>(queryCount);
		Random random = ThreadLocalRandom.current();

		for (int i = 0; i < queryCount; i++) {
			World world = this.worldRepository.findOne(random.nextInt(DB_ROWS) + 1);
			world.setRandomNumber(random.nextInt(DB_ROWS) + 1);
			this.worldRepository.save(world);
			worlds.add(world);
		}

		return worlds;
	}

	private Integer boundQueryCount(final Integer raw) {
		if (raw == null || raw < 1) {
			return 1;
		} else if (raw > 500) {
			return 500;
		}

		return raw;
	}

}

@@ -984,16 +984,22 @@
 stParam(
+value=
 %22queries
@@ -999,25 +999,58 @@
 queries%22
-) Integer
+, required=false, defaultValue=%221%22) String
  rawQuer
@@ -1521,23 +1521,22 @@
 ue=%221%22) 
-Integer
+String
  rawQuer
@@ -2006,22 +2006,242 @@
 nal 
-Integer raw) %7B
+String rawString) %7B%0A                Integer raw;%0A                try %7B%0A                       raw = Integer.parseInt(rawString);%0A                %7D catch (NumberFormatException e) %7B%0A                       raw = null;%0A                %7D 
 %0A%09%09i

Fix spring warnings on ?querys=foo etc