-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShopControllerV2.kt
58 lines (48 loc) · 2.13 KB
/
ShopControllerV2.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package kr.co.hasys.springr2dbcstudy.shop
import io.r2dbc.spi.ConnectionFactory
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate
import org.springframework.data.relational.core.query.Criteria.where
import org.springframework.data.relational.core.query.Query.query
import org.springframework.data.relational.core.query.Update
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
/**
* R2dbcEntityTemplate
*/
@RestController
class ShopControllerV2(val connectionFactory: ConnectionFactory) {
private val r2dbcEntityTemplate = R2dbcEntityTemplate(connectionFactory)
@GetMapping("/v2/shops")
fun gets() = r2dbcEntityTemplate
.select(Shop::class.java)
.all()
.map { ShopResponse(it.id ?: "", it.name) }
@GetMapping("/v2/shops/{id}")
fun get(@PathVariable id: String) = r2dbcEntityTemplate
.selectOne(query(where("id").`is`(id)), Shop::class.java)
.switchIfEmpty(Mono.error(ShopNotFoundException()))
.map { ShopResponse(it.id ?: "", it.name) }
@PostMapping("/v2/shops")
@ResponseStatus(HttpStatus.CREATED)
fun post(@RequestBody request: ShopRequest) = r2dbcEntityTemplate
.insert(Shop::class.java)
.using(Shop(request.name))
.map { ShopResponse(it.id ?: "", it.name) }
@PutMapping("/v2/shops/{id}")
fun put(@RequestBody request: ShopRequest, @PathVariable id: String) = r2dbcEntityTemplate
.selectOne(query(where("id").`is`(id)), Shop::class.java)
.switchIfEmpty(Mono.error(ShopNotFoundException()))
.flatMap {
r2dbcEntityTemplate
.update(Shop::class.java)
.matching(query(where("id").`is`(id)))
.apply(Update.update("name", request.name))
}
@DeleteMapping("/v2/shops/{id}")
fun delete(@PathVariable id: String) = r2dbcEntityTemplate
.delete(Shop::class.java)
.matching(query(where("id").`is`(id)))
.all()
.onErrorMap { ShopHasStylerException() }
}