Skip to content

Commit

Permalink
Update service
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyfrs committed May 19, 2024
1 parent e7982dc commit 1cd6f39
Show file tree
Hide file tree
Showing 58 changed files with 312 additions and 619 deletions.
9 changes: 3 additions & 6 deletions src/main/kotlin/com/fiap/order/OrderApiApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ import org.springframework.cloud.openfeign.FeignAutoConfiguration
@OpenAPIDefinition(
info =
Info(
title = "Self-Order Management API",
title = "Orders API",
version = "1.0.0",
description =
"API de autoatendimento em restaurante como implementação do Tech Challenge" +
" referente à primeira fase do curso de pós-graduação em Arquitetura de Software pela FIAP.",
contact =
Contact(
description = "Microsserviço de pedidos",
contact = Contact(
name = "Grupo 15",
url = "http://fiap-3soat-g15.s3-website-us-east-1.amazonaws.com",
),
Expand Down
84 changes: 0 additions & 84 deletions src/main/kotlin/com/fiap/order/adapter/client/MercadoPagoClient.kt

This file was deleted.

29 changes: 10 additions & 19 deletions src/main/kotlin/com/fiap/order/adapter/client/PaymentsApiClient.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
package com.fiap.order.adapter.client

import com.fiap.order.domain.entities.Order
import com.fiap.order.domain.entities.Payment
import com.fiap.order.driver.web.request.PaymentRequest
import com.fiap.order.driver.web.response.PaymentResponse
import com.fiap.order.driver.web.request.PaymentHTTPRequest
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.*

@FeignClient(name = "payments-client", url = "\${clients.payments-api.url}")
@FeignClient(
name = "payments-client",
url = "\${clients.payments-api.url}"
)
interface PaymentsApiClient {

@RequestMapping(
method = [RequestMethod.GET],
value = ["/payments/{orderNumber}"],
value = ["/payments/{paymentId}"],
consumes = ["application/json"]
)
fun getByOrderNumber(@PathVariable orderNumber: Long): Payment
fun getByPaymentId(@PathVariable paymentId: String): PaymentResponse

@RequestMapping(
method = [RequestMethod.POST],
value = ["/payments/notifications/{orderNumber}"],
value = ["/payments"],
consumes = ["application/json"]
)
fun notify(
@PathVariable orderNumber: Long,
@RequestParam(value = "id") resourceId: String,
@RequestParam topic: String
): Any

@RequestMapping(
method = [RequestMethod.POST],
value = ["/payments/create"],
consumes = ["application/json"]
)
fun create(@RequestBody order: Order): PaymentRequest
fun create(@RequestBody paymentHTTPRequest: PaymentHTTPRequest): PaymentResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod

@FeignClient(name = "stock-client", url = "\${clients.stock-api.url}", configuration = [StockApiInterceptor::class])
@FeignClient(
name = "stock-client",
url = "\${clients.stock-api.url}",
configuration = [StockApiInterceptor::class]
)
interface StockApiClient {

@RequestMapping(
Expand Down Expand Up @@ -38,4 +42,4 @@ interface StockApiClient {
)
fun getByProductNumber(@PathVariable("productNumber") productNumber: Long): Product

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes

class StockApiInterceptor: RequestInterceptor {

override fun apply(template: RequestTemplate?) {
val requestAttr = (RequestContextHolder.getRequestAttributes() as ServletRequestAttributes)
val apiToken = requestAttr.request.getHeader("authorization")
template?.header("authorization", apiToken)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fiap.order.adapter.controller


import com.fiap.order.domain.entities.Customer
import com.fiap.order.driver.web.CustomersAPI
import com.fiap.order.driver.web.request.CustomerRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import com.fiap.order.domain.entities.Order
import com.fiap.order.domain.valueobjects.OrderStatus
import com.fiap.order.driver.web.OrdersAPI
import com.fiap.order.driver.web.request.OrderRequest
import com.fiap.order.driver.web.response.OrderToPayResponse
import com.fiap.order.usecases.*
import com.fiap.order.driver.web.response.PendingOrderResponse
import com.fiap.order.usecases.ChangeOrderStatusUseCase
import com.fiap.order.usecases.LoadOrderUseCase
import com.fiap.order.usecases.CreateOrderUseCase
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.oauth2.core.ClaimAccessor
Expand All @@ -15,13 +18,12 @@ import java.util.*
@RestController
class OrderController(
private val loadOrdersUseCase: LoadOrderUseCase,
private val createOrderUseCase: PlaceOrderUseCase,
private val loadPaymentUseCase: LoadPaymentUseCase,
private val prepareOrderUseCase: PrepareOrderUseCase,
private val completeOrderUseCase: CompleteOrderUseCase,
private val cancelOrderStatusUseCase: CancelOrderStatusUseCase,
private val confirmOrderUseCase: ConfirmOrderUseCase
) : OrdersAPI {
private val createOrderUseCase: CreateOrderUseCase,
private val changeOrderStatusUseCase: ChangeOrderStatusUseCase,
) : OrdersAPI
{
private val log = LoggerFactory.getLogger(javaClass)

override fun getByOrderNumber(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(loadOrdersUseCase.getByOrderNumber(orderNumber))
}
Expand Down Expand Up @@ -54,47 +56,39 @@ class OrderController(
.run { return ResponseEntity.ok(this) }
}

override fun create(orderRequest: OrderRequest): ResponseEntity<OrderToPayResponse> {
override fun create(orderRequest: OrderRequest): ResponseEntity<PendingOrderResponse> {
var customerId: UUID? = null
try {
customerId = UUID.fromString(
(SecurityContextHolder.getContext().authentication.credentials as ClaimAccessor).getClaim<String>("custom:CUSTOMER_ID")
(SecurityContextHolder.getContext().authentication.credentials as ClaimAccessor)
.getClaim("custom:CUSTOMER_ID")
)
} catch (_: Exception) {
} catch (ex: Exception) {
log.error(ex.message, ex.cause)
}

val order =
createOrderUseCase.create(
customerId,
orderRequest.toOrderItemDomain(),
)
val payment = loadPaymentUseCase.getByOrderNumber(order.number!!)
val orderToPayResponse = createOrderUseCase.create(customerId, orderRequest.toOrderItemsDomain())

return ResponseEntity.ok(
OrderToPayResponse(
order = order,
paymentInfo = payment.paymentInfo,
),
)
return ResponseEntity.ok(orderToPayResponse)
}

override fun start(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(prepareOrderUseCase.startOrderPreparation(orderNumber))
return ResponseEntity.ok(changeOrderStatusUseCase.startOrderPreparation(orderNumber))
}

override fun finish(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(prepareOrderUseCase.finishOrderPreparation(orderNumber))
return ResponseEntity.ok(changeOrderStatusUseCase.finishOrderPreparation(orderNumber))
}

override fun complete(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(completeOrderUseCase.completeOrder(orderNumber))
return ResponseEntity.ok(changeOrderStatusUseCase.completeOrder(orderNumber))
}

override fun cancel(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(cancelOrderStatusUseCase.cancelOrder(orderNumber))
return ResponseEntity.ok(changeOrderStatusUseCase.cancelOrder(orderNumber))
}

override fun confirm(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(confirmOrderUseCase.confirmOrder(orderNumber))
return ResponseEntity.ok(changeOrderStatusUseCase.confirmOrder(orderNumber))
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
package com.fiap.order.adapter.controller.configuration

import com.fiap.order.OrderApiApp
import com.fiap.order.adapter.gateway.*
import com.fiap.order.usecases.*
import com.fiap.order.adapter.gateway.CustomerGateway
import com.fiap.order.adapter.gateway.OrderGateway
import com.fiap.order.adapter.gateway.PaymentGateway
import com.fiap.order.adapter.gateway.ProductGateway
import com.fiap.order.adapter.gateway.StockGateway
import com.fiap.order.adapter.gateway.TransactionalGateway
import com.fiap.order.usecases.AdjustStockUseCase
import com.fiap.order.usecases.LoadCustomerUseCase
import com.fiap.order.usecases.LoadProductUseCase
import com.fiap.order.usecases.RequestPaymentUseCase
import com.fiap.order.usecases.services.CustomerService
import com.fiap.order.usecases.services.OrderService
import com.fiap.order.usecases.services.PaymentService
import com.fiap.order.usecases.services.ProductService
import com.fiap.order.usecases.services.StockService
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import com.fiap.order.usecases.services.PaymentService

@Configuration
@ComponentScan(basePackageClasses = [OrderApiApp::class])
class ServiceConfig {

@Bean
fun createProductService(productRepository: ProductGateway): ProductService {
return ProductService(productRepository)
}

@Bean
fun createProductService(
productRepository: ProductGateway,
): ProductService {
return ProductService(
productRepository,
)
fun createStockService(stockRepository: StockGateway): StockService {
return StockService(stockRepository)
}

@Bean
fun createCustomerService(customerRepository: CustomerGateway): CustomerService {
return CustomerService(customerRepository)
}

@Bean
fun createPaymentService(paymentRepository: PaymentGateway): PaymentService {
return PaymentService(paymentRepository)
}

@Bean
Expand All @@ -32,7 +50,7 @@ class ServiceConfig {
loadCustomerUseCase: LoadCustomerUseCase,
loadProductsUseCase: LoadProductUseCase,
adjustInventoryUseCase: AdjustStockUseCase,
providePaymentRequestUseCase: ProvidePaymentRequestUseCase,
providePaymentRequestUseCase: RequestPaymentUseCase,
transactionalRepository: TransactionalGateway,
): OrderService {
return OrderService(
Expand All @@ -44,23 +62,4 @@ class ServiceConfig {
transactionalRepository,
)
}

@Bean
fun createStockService(stockRepository: StockGateway): StockService {
return StockService(stockRepository)
}

@Bean
fun createCustomerService(customerRepository: CustomerGateway): CustomerService {
return CustomerService(customerRepository)
}

@Bean
fun createPaymentService(
paymentRepository: PaymentGateway,
): PaymentService {
return PaymentService(
paymentRepository,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.fiap.order.domain.entities.Customer
import java.util.*

interface CustomerGateway {

fun findAll(): List<Customer>

fun findById(customerId: UUID): Customer?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ interface OrderGateway {

fun findByCustomerId(customerId: UUID): List<Order>

fun findByCustomerIdAndStatus(
customerId: UUID,
status: OrderStatus,
): List<Order>
fun findByCustomerIdAndStatus(customerId: UUID, status: OrderStatus): List<Order>

fun upsert(order: Order): Order

Expand Down
Loading

0 comments on commit 1cd6f39

Please sign in to comment.