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 55a07a8
Show file tree
Hide file tree
Showing 60 changed files with 315 additions and 649 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
Expand Up @@ -13,10 +13,7 @@ class ControllerExceptionHandler {
protected fun domainErrorHandler(domainException: SelfOrderManagementException): ResponseEntity<ApiError> {
val apiErrorResponseEntity: ApiErrorResponseEntity =
when (domainException.errorType) {
ErrorType.PRODUCT_ALREADY_EXISTS,
ErrorType.CUSTOMER_ALREADY_EXISTS,
ErrorType.STOCK_ALREADY_EXISTS,
ErrorType.PAYMENT_ALREADY_EXISTS,
ErrorType.INSUFFICIENT_STOCK,
->
ApiErrorResponseEntity(
Expand All @@ -26,10 +23,7 @@ class ControllerExceptionHandler {

ErrorType.CUSTOMER_NOT_FOUND,
ErrorType.PRODUCT_NOT_FOUND,
ErrorType.COMPONENT_NOT_FOUND,
ErrorType.STOCK_NOT_FOUND,
ErrorType.ORDER_NOT_FOUND,
ErrorType.PAYMENT_NOT_FOUND,
->
ApiErrorResponseEntity(
ApiError(domainException.errorType.name, domainException.message),
Expand All @@ -40,22 +34,12 @@ class ControllerExceptionHandler {
ErrorType.INVALID_ORDER_STATE_TRANSITION,
ErrorType.INVALID_PRODUCT_CATEGORY,
ErrorType.EMPTY_ORDER,
ErrorType.PRODUCT_NUMBER_IS_MANDATORY,
ErrorType.COMPONENT_NUMBER_IS_MANDATORY,
->
ApiErrorResponseEntity(
ApiError(domainException.errorType.name, domainException.message),
HttpStatus.BAD_REQUEST,
)

ErrorType.PAYMENT_NOT_CONFIRMED,
ErrorType.PAYMENT_REQUEST_NOT_ALLOWED,
->
ApiErrorResponseEntity(
ApiError(domainException.errorType.name, domainException.message),
HttpStatus.PAYMENT_REQUIRED,
)

else ->
ApiErrorResponseEntity(
ApiError(ErrorType.UNEXPECTED_ERROR.name, domainException.localizedMessage),
Expand Down
Loading

0 comments on commit 55a07a8

Please sign in to comment.