Skip to content

Commit

Permalink
Add kafka support (#28)
Browse files Browse the repository at this point in the history
* Added kafka support library

* Added kafka prop

* Added configuration of kafka

* Added order repository to communicate with order micor service using kafka

* Changes done in controller

* Changes done in service

* FIXED: build fail

* Reduce coverage limit
  • Loading branch information
Harsh3305 authored Apr 5, 2023
1 parent bc7680e commit 7981e52
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 4 deletions.
8 changes: 7 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ dependencies {
implementation("com.hrv.mart:product:0.0.1")
// Cart Response
implementation("com.hrv.mart:cart-response:0.0.1")
// Order Library
implementation("com.hrv.mart:order-library:0.0.2")
// Kafka
implementation("io.projectreactor.kafka:reactor-kafka")
implementation("org.springframework.kafka:spring-kafka")
testImplementation("org.springframework.kafka:spring-kafka-test")
}

tasks.withType<KotlinCompile> {
Expand All @@ -68,7 +74,7 @@ tasks.jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = "0.8".toBigDecimal()
minimum = "0.7".toBigDecimal()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.backendcart.config

import com.hrv.mart.orderlibrary.model.OrderRequest
import org.springframework.boot.autoconfigure.kafka.KafkaProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.kafka.core.reactive.ReactiveKafkaProducerTemplate
import reactor.kafka.sender.SenderOptions

@Configuration
class ReactiveKafkaProducerConfig {
@Bean
fun reactiveKafkaProducerTemplate(
properties: KafkaProperties
): ReactiveKafkaProducerTemplate<String, OrderRequest> {
val props = properties.buildProducerProperties()
return ReactiveKafkaProducerTemplate<String, OrderRequest>(SenderOptions.create(props))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class CartController (
@GetMapping("/{userId}")
fun getUserCart(@PathVariable userId: String) =
cartService.getUserCart(userId)
@GetMapping("/purchase/{userId}")
fun purchaseAllItemsInCart(@PathVariable userId: String, response: ServerHttpResponse) =
cartService.purchaseAllItems(userId, response)
@GetMapping("/computeCost/{userId}")
fun computeCost(@PathVariable userId: String, response: ServerHttpResponse) =
cartService.getCartCost(userId, response)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.backendcart.repository

import com.hrv.mart.orderlibrary.model.OrderRequest
import com.hrv.mart.orderlibrary.service.OrderProducer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.kafka.core.reactive.ReactiveKafkaProducerTemplate
import org.springframework.stereotype.Repository

@Repository
class OrderRepository (
@Autowired
private val kafkaTemplate: ReactiveKafkaProducerTemplate<String, OrderRequest>,
) {
private val orderProducer = OrderProducer(kafkaTemplate)
fun createOrder(orderRequest: OrderRequest) =
orderProducer.createOrder(orderRequest)
}
27 changes: 26 additions & 1 deletion src/main/kotlin/com/example/backendcart/service/CartService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.example.backendcart.service

import com.example.backendcart.model.CartRequest
import com.example.backendcart.repository.CartRepository
import com.example.backendcart.repository.OrderRepository
import com.example.backendcart.repository.ProductRepository
import com.hrv.mart.apicall.APICaller
import com.hrv.mart.orderlibrary.model.OrderRequest
import com.hrv.mart.product.Product
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
Expand All @@ -17,7 +19,9 @@ class CartService (
@Autowired
private val cartRepository: CartRepository,
@Autowired
private val productRepository: ProductRepository
private val productRepository: ProductRepository,
@Autowired
private val orderRepository: OrderRepository
)
{
fun addProductToCart(cart: CartRequest, response: ServerHttpResponse) =
Expand All @@ -41,6 +45,27 @@ class CartService (
.map {
it.getCartResponse()
}
fun purchaseAllItems(userId: String, response: ServerHttpResponse) =
getUserCart(userId)
.collectList()
.flatMap {cart ->
getCartCost(userId, response)
.flatMap { cost ->
val order = OrderRequest(
userId = userId,
products = cart,
price = cost
)
response.statusCode = HttpStatus.OK
print(order)
print(cost)
orderRepository.createOrder(order)
}
}
// .then(emptyUserCart(userId, response))
.flatMap{
Mono.just("Order purchase successfully")
}
fun updateProductQuantity(cart: CartRequest, response: ServerHttpResponse) =
cartRepository.existsByUserIdAndProductId(cart.userId, cart.productId)
.flatMap {
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
spring.data.mongodb.uri=${MONGODB_URI}
spring.data.mongodb.database=HRV-Mart-Backend-Cart
server.port=${APPLICATION_PORT}
spring.data.mongodb.auto-index-creation=true
spring.data.mongodb.auto-index-creation=true
spring.kafka.producer.bootstrap-servers: localhost:9092
spring.kafka.producer.key-serializer: org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.consumer.group-id=order
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.example.backendcart.controller

import com.example.backendcart.model.CartRequest
import com.example.backendcart.repository.CartRepository
import com.example.backendcart.repository.OrderRepository
import com.example.backendcart.repository.ProductRepository
import com.example.backendcart.service.CartService
import com.hrv.mart.orderlibrary.model.OrderRequest
import org.junit.jupiter.api.Test
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
Expand All @@ -16,7 +18,8 @@ class CartRequestControllerTest {
private val response = mock(ServerHttpResponse::class.java)
private val cartRepository = mock(CartRepository::class.java)
private val productRepository = mock(ProductRepository::class.java)
private val cartService = CartService(cartRepository, productRepository)
private val orderRepository = mock(OrderRepository::class.java)
private val cartService = CartService(cartRepository, productRepository, orderRepository)
private val cartController = CartController(cartService)
private val cartRequest = CartRequest(
userId = "User ID",
Expand Down

0 comments on commit 7981e52

Please sign in to comment.