@@ -134,14 +134,20 @@ fun Application.main() {
134134 worldsUpdated.add(world)
135135 }
136136
137- // Execute updates sequentially
138- worldsUpdated.forEach { world ->
139- Mono .usingWhen(dbConnFactory.create(), { connection ->
140- Mono .from(connection.createStatement(UPDATE_QUERY )
141- .bind(" $1" , world.randomNumber)
142- .bind(" $2" , world.id)
143- .execute())
144- }, Connection ::close).awaitFirstOrNull()
137+ val worldsUpdated = buildList {
138+ worlds.collect { world ->
139+ world.randomNumber = random.nextInt(DB_ROWS ) + 1
140+ add(world)
141+
142+ Mono .usingWhen(dbConnFactory.create(), { connection ->
143+ Mono .from(
144+ connection.createStatement(UPDATE_QUERY )
145+ .bind(0 , world.randomNumber)
146+ .bind(1 , world.id)
147+ .execute()
148+ ).flatMap { Mono .from(it.rowsUpdated) }
149+ }, Connection ::close).awaitFirstOrNull()
150+ }
145151 }
146152
147153 call.respondText(json.encodeToString(worldsUpdated), ContentType .Application .Json )
@@ -157,14 +163,13 @@ private fun getWorld(
157163 .execute())
158164 .flatMap { r ->
159165 Mono .from(r.map { row, _ ->
160- val id = row.get(" id" , Int ::class .java) ? : row.get(0 , Int ::class .java)
161- val randomNumber = row.get(" randomnumber" , Int ::class .java) ? : row.get(1 , Int ::class .java)
162-
163- if (id == null || randomNumber == null ) {
166+ val id = row.get(0 , Int ::class .java)
167+ val randomNumber = row.get(1 , Int ::class .java)
168+ if (id != null && randomNumber != null ) {
169+ World (id, randomNumber)
170+ } else {
164171 throw IllegalStateException (" Database returned null values for required fields" )
165172 }
166-
167- World (id, randomNumber)
168173 })
169174 }
170175}, Connection ::close)
0 commit comments