-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6c7092
commit f093487
Showing
4 changed files
with
85 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
### | ||
GET http://localhost:8080/break | ||
|
||
### | ||
GET http://localhost:8080/keys | ||
|
||
### | ||
GET http://localhost:8080/parts | ||
|
||
<> 2024-01-15T074950.200.json | ||
<> 2024-01-15T074933.200.json | ||
### | ||
GET http://localhost:8080/parts/list | ||
|
||
### | ||
GET http://localhost:8080/break | ||
GET http://localhost:8080/parts/suspend | ||
|
||
### | ||
GET http://localhost:8080/keys | ||
GET http://localhost:8080/parts/correct | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
car-parts-reactive/src/main/kotlin/org/jesperancinha/carpartsreactive/rest/CarController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.jesperancinha.carpartsreactive.rest | ||
|
||
import kotlinx.coroutines.currentCoroutineContext | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.job | ||
import org.jesperancinha.carpartsreactive.CarPart | ||
import org.jesperancinha.carpartsreactive.CarPartRepository | ||
import org.springframework.http.MediaType | ||
import org.springframework.web.bind.annotation.* | ||
import kotlin.coroutines.coroutineContext | ||
|
||
@RestController | ||
@RequestMapping | ||
class CarController( | ||
val carPartRepository: CarPartRepository | ||
) { | ||
|
||
@GetMapping("/parts/list", "/parts") | ||
suspend fun getParts(): List<String> { | ||
println(coroutineContext) | ||
return listOf("breaks", "keys", "lights") | ||
} | ||
|
||
@GetMapping("/parts/suspend", produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
suspend fun getPartsUsingFlow(): Flow<String> { | ||
println(coroutineContext) | ||
return flow { | ||
println(coroutineContext) | ||
emit("breaks") | ||
emit("keys") | ||
emit("lights") | ||
} | ||
} | ||
|
||
@GetMapping("/parts/correct", produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
fun getPartsUsingFlowWithoutSuspend(): Flow<String> { | ||
return flow { | ||
println(coroutineContext) | ||
emit("breaks") | ||
emit("keys") | ||
emit("lights") | ||
} | ||
} | ||
|
||
@GetMapping("/break") | ||
suspend fun getBreak(): String { | ||
println("Current Coroutine Context -> $coroutineContext") | ||
println("Current Coroutine Context -> ${coroutineContext.job}") | ||
println("Main Coroutine Context -> ${currentCoroutineContext()}") | ||
println("Main Coroutine Context -> ${currentCoroutineContext().job}") | ||
println("Current Thread -> ${Thread.currentThread()}") | ||
return "break" | ||
} | ||
|
||
@GetMapping("/keys") | ||
fun getKeys(): String { | ||
return "keys" | ||
} | ||
|
||
|
||
@GetMapping("/parts/{id}") | ||
suspend fun getPartsCorrect( | ||
@PathVariable("id") id: String | ||
): CarPart? = carPartRepository.findById(id) | ||
|
||
@PostMapping("/parts") | ||
suspend fun createPart(@RequestBody carPart: CarPart) { | ||
println(coroutineContext) | ||
println(carPartRepository) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters