From 236223af7132d39d4e1865b6028efaf993f33363 Mon Sep 17 00:00:00 2001 From: kanat Date: Sat, 5 Oct 2024 09:41:02 +0300 Subject: [PATCH] clients in gateway --- .gitignore | 2 + gateway/src/main/kotlin/api/BookController.kt | 18 ++- gateway/src/main/kotlin/api/BookingApi.kt | 116 +++++++++--------- gateway/src/main/kotlin/api/BooksApi.kt | 37 ------ .../main/kotlin/client/CoreCatalogClient.kt | 39 ++++-- gateway/src/main/resources/api/api.yaml | 3 + 6 files changed, 107 insertions(+), 108 deletions(-) delete mode 100644 gateway/src/main/kotlin/api/BooksApi.kt diff --git a/.gitignore b/.gitignore index d111c1e..546e105 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ **/gradle/ !src/**/build/ +**/*_postgres_data + # Ignore Gradle GUI config gradle-app.setting diff --git a/gateway/src/main/kotlin/api/BookController.kt b/gateway/src/main/kotlin/api/BookController.kt index 4a398fe..4116eaf 100644 --- a/gateway/src/main/kotlin/api/BookController.kt +++ b/gateway/src/main/kotlin/api/BookController.kt @@ -14,16 +14,22 @@ import java.util.* class BookController( private val coreCatalogClient: CoreCatalogClient, ) : BooksApi { - override fun getBookContentByTitle(title: String): ResponseEntity> { - TODO("Not yet implemented") + override fun getBookContentByTitle(title: UUID): ResponseEntity> { + return ResponseEntity.ok( + coreCatalogClient.getBookContentByTitle(title) + ) } - override fun getBookInstancesByBookContentId(bookContentId: String): ResponseEntity> { - TODO("Not yet implemented") + override fun getBookInstancesByBookContentId(bookContentId: UUID): ResponseEntity> { + return ResponseEntity.ok( + coreCatalogClient.getBookInstancesByBookContentId(bookContentId) + ) } - override fun getBooksContentById(bookContentId: String): ResponseEntity> { - TODO("Not yet implemented") + override fun getBooksContentById(bookContentId: UUID): ResponseEntity> { + return ResponseEntity.ok( + coreCatalogClient.getBooksContentById(bookContentId) + ) } override fun initializeBookReservation( diff --git a/gateway/src/main/kotlin/api/BookingApi.kt b/gateway/src/main/kotlin/api/BookingApi.kt index f8a16d4..19f02f1 100644 --- a/gateway/src/main/kotlin/api/BookingApi.kt +++ b/gateway/src/main/kotlin/api/BookingApi.kt @@ -1,58 +1,58 @@ -package org.example.api - -import org.springframework.beans.factory.annotation.Value -import org.springframework.http.HttpEntity -import org.springframework.http.HttpHeaders -import org.springframework.http.HttpMethod -import org.springframework.http.ResponseEntity -import org.springframework.web.bind.annotation.RestController -import org.springframework.web.client.RestTemplate -import org.springframework.web.servlet.function.ServerRequest.Headers -import ru.openbook.api.ReservationApi -import ru.openbook.model.BookReservationRequestResponse -import java.util.UUID - -@RestController -class BookingApi( - private val restTemplate: RestTemplate, - @Value("\${spring.clients.core-booking.host}") - private val clientHost: String, -) : ReservationApi { - override fun getReservationRequest( - xUid: UUID, - reservationId: UUID, - ): ResponseEntity { - val entity = HttpEntity( - mapOf( - "X-UID" to xUid.toString(), - ), - ) - - return restTemplate.exchange( - "$clientHost/v1/reservations/$reservationId", - HttpMethod.GET, - entity, - BookReservationRequestResponse::class.java, - ) - } - - override fun reserveBook( - xIdempotencyToken: UUID, - xUid: UUID, - bookId: UUID, - ): ResponseEntity { - val entity = HttpEntity( - mapOf( - "X-Idempotency-Token" to xIdempotencyToken.toString(), - "X-UID" to xUid.toString(), - ), - ) - - return restTemplate.exchange( - "$clientHost/v1/booking/$bookId/reservation", - HttpMethod.POST, - entity, - BookReservationRequestResponse::class.java, - ) - } -} +//package org.example.api +// +//import org.springframework.beans.factory.annotation.Value +//import org.springframework.http.HttpEntity +//import org.springframework.http.HttpHeaders +//import org.springframework.http.HttpMethod +//import org.springframework.http.ResponseEntity +//import org.springframework.web.bind.annotation.RestController +//import org.springframework.web.client.RestTemplate +//import org.springframework.web.servlet.function.ServerRequest.Headers +//import ru.openbook.api.ReservationApi +//import ru.openbook.model.BookReservationRequestResponse +//import java.util.UUID +// +//@RestController +//class BookingApi( +// private val restTemplate: RestTemplate, +// @Value("\${spring.clients.core-booking.host}") +// private val clientHost: String, +//) : ReservationApi { +// override fun getReservationRequest( +// xUid: UUID, +// reservationId: UUID, +// ): ResponseEntity { +// val entity = HttpEntity( +// mapOf( +// "X-UID" to xUid.toString(), +// ), +// ) +// +// return restTemplate.exchange( +// "$clientHost/v1/reservations/$reservationId", +// HttpMethod.GET, +// entity, +// BookReservationRequestResponse::class.java, +// ) +// } +// +// override fun reserveBook( +// xIdempotencyToken: UUID, +// xUid: UUID, +// bookId: UUID, +// ): ResponseEntity { +// val entity = HttpEntity( +// mapOf( +// "X-Idempotency-Token" to xIdempotencyToken.toString(), +// "X-UID" to xUid.toString(), +// ), +// ) +// +// return restTemplate.exchange( +// "$clientHost/v1/booking/$bookId/reservation", +// HttpMethod.POST, +// entity, +// BookReservationRequestResponse::class.java, +// ) +// } +//} diff --git a/gateway/src/main/kotlin/api/BooksApi.kt b/gateway/src/main/kotlin/api/BooksApi.kt deleted file mode 100644 index 3626854..0000000 --- a/gateway/src/main/kotlin/api/BooksApi.kt +++ /dev/null @@ -1,37 +0,0 @@ -package org.example.api - -import org.springframework.http.ResponseEntity -import org.springframework.web.bind.annotation.RestController -import ru.openbook.api.BooksApi -import ru.openbook.model.BookContentResponse -import ru.openbook.model.BookInstanceResponse -import ru.openbook.model.BookSearchResponse -import java.util.UUID - -@RestController -class BooksApi: BooksApi { - override fun getBookContentByTitle(title: String): ResponseEntity> { - TODO("Not yet implemented") - } - - override fun getBookInstancesByBookContentId(bookContentId: String): ResponseEntity> { - TODO("Not yet implemented") - } - - override fun getBooksContentById(bookContentId: String): ResponseEntity> { - TODO("Not yet implemented") - } - - override fun initializeBookReservation(bookInstanceId: UUID): ResponseEntity { - TODO("Not yet implemented") - } - - override fun searchBooks( - title: String?, - author: String?, - page: Int, - pageSize: Int, - ): ResponseEntity { - TODO("Not yet implemented") - } -} \ No newline at end of file diff --git a/gateway/src/main/kotlin/client/CoreCatalogClient.kt b/gateway/src/main/kotlin/client/CoreCatalogClient.kt index bb85b73..dd10c5c 100644 --- a/gateway/src/main/kotlin/client/CoreCatalogClient.kt +++ b/gateway/src/main/kotlin/client/CoreCatalogClient.kt @@ -1,6 +1,9 @@ package org.example.client import org.springframework.beans.factory.annotation.Value +import org.springframework.core.ParameterizedTypeReference +import org.springframework.http.HttpMethod +import org.springframework.http.ResponseEntity import org.springframework.stereotype.Component import org.springframework.web.client.RestTemplate import ru.openbook.model.BookContentResponse @@ -18,12 +21,26 @@ class CoreCatalogClient( @Value("\${spring.clients.core-catalog.port}") lateinit var coreCatalogPort: String - fun getBookContentByTitle(title: String): List { - TODO("Not yet implemented") + fun getBookContentByTitle(title: UUID): List { + val url = "$coreCatalogHost:$coreCatalogPort/v1/books/name/$title" + val response: ResponseEntity> = restTemplate.exchange( + url, + HttpMethod.GET, + null, + object : ParameterizedTypeReference>() {} + ) + return response.body ?: emptyList() } - fun getBooksContentById(bookContentId: String): List { - TODO("Not yet implemented") + fun getBooksContentById(bookContentId: UUID): List { + val url = "$coreCatalogHost:$coreCatalogPort/v1/books/$bookContentId" + val response: ResponseEntity> = restTemplate.exchange( + url, + HttpMethod.GET, + null, + object : ParameterizedTypeReference>() {} + ) + return response.body ?: emptyList() } fun initializeBookReservation( @@ -31,7 +48,8 @@ class CoreCatalogClient( bookReservationInitializationRequest: BookReservationInitializationRequest ): BookInstanceResponse? { val url = "$coreCatalogHost:$coreCatalogPort/v1/books/$bookInstanceId/reservation" - val response = restTemplate.postForEntity(url, bookReservationInitializationRequest, BookInstanceResponse::class.java) + val response = + restTemplate.postForEntity(url, bookReservationInitializationRequest, BookInstanceResponse::class.java) return response.body } @@ -41,8 +59,15 @@ class CoreCatalogClient( return response.body } - fun getBookInstancesByBookContentId(bookContentId: String): List { - TODO("Not yet implemented") + fun getBookInstancesByBookContentId(bookContentId: UUID): List { + val url = "$coreCatalogHost:$coreCatalogPort/v1/books/$bookContentId/instances" + val response: ResponseEntity> = restTemplate.exchange( + url, + HttpMethod.GET, + null, + object : ParameterizedTypeReference>() {} + ) + return response.body ?: emptyList() } } \ No newline at end of file diff --git a/gateway/src/main/resources/api/api.yaml b/gateway/src/main/resources/api/api.yaml index 17eddcd..d754f7c 100644 --- a/gateway/src/main/resources/api/api.yaml +++ b/gateway/src/main/resources/api/api.yaml @@ -137,6 +137,7 @@ paths: required: true schema: type: string + format: uuid description: ID of the book content. responses: 200: @@ -178,6 +179,7 @@ paths: required: true schema: type: string + format: uuid description: ID of the book. responses: 200: @@ -201,6 +203,7 @@ paths: required: true schema: type: string + format: uuid description: Title of the book. responses: 200: