Skip to content

Commit

Permalink
Merge pull request #12 from FIAP-3SOAT-G15/add-logs
Browse files Browse the repository at this point in the history
Add logs
  • Loading branch information
wellyfrs authored May 19, 2024
2 parents 54c55bf + 4eb2eb8 commit e7982dc
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push docker image to Amazon ECR
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
#if: github.ref == 'refs/heads/main' && github.event_name == 'push'
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ vars.AWS_ECR_REPO_NAME }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ interface PaymentsApiClient {

@RequestMapping(
method = [RequestMethod.POST],
value = ["/payments/create/{orderNumber}"],
value = ["/payments/create"],
consumes = ["application/json"]
)
fun create(
@PathVariable orderNumber: Long,
@RequestBody order: Order
): PaymentRequest
}
fun create(@RequestBody order: Order): PaymentRequest
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PaymentGatewayImpl(
}

override fun createFromOrder(order: Order): PaymentRequest {
return paymentsApiClient.create(order.number!!, order)
return paymentsApiClient.create(order)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.fiap.order.domain.entities.Customer
import com.fiap.order.domain.errors.ErrorType
import com.fiap.order.domain.errors.SelfOrderManagementException
import com.fiap.order.usecases.*
import org.slf4j.LoggerFactory
import java.util.*

class CustomerService(
Expand All @@ -14,6 +15,8 @@ class CustomerService(
CreateCustomerUseCase,
UpdateCustomerUseCase,
RemoveCustomerUseCase {
private val log = LoggerFactory.getLogger(javaClass)

override fun getById(customerId: UUID): Customer {
return repository.findById(customerId)
?: throw SelfOrderManagementException(
Expand All @@ -39,14 +42,17 @@ class CustomerService(
}

override fun create(customer: Customer): Customer {
log.info("Creating customer $customer")
return repository.create(customer.copy(id = UUID.randomUUID()))
}

override fun update(customer: Customer): Customer {
log.info("Updating customer $customer")
return repository.update(customer)
}

override fun remove(customerId: UUID): Customer {
log.info("Removing customer [$customerId]")
return repository.deleteById(customerId)
}
}
29 changes: 19 additions & 10 deletions src/main/kotlin/com/fiap/order/usecases/services/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fiap.order.domain.errors.SelfOrderManagementException
import com.fiap.order.domain.valueobjects.OrderStatus
import com.fiap.order.domain.valueobjects.PaymentStatus
import com.fiap.order.usecases.*
import org.slf4j.LoggerFactory
import java.time.LocalDate
import java.util.*

Expand All @@ -25,6 +26,8 @@ open class OrderService(
PrepareOrderUseCase,
CompleteOrderUseCase,
CancelOrderStatusUseCase {
private val log = LoggerFactory.getLogger(javaClass)

override fun getByOrderNumber(orderNumber: Long): Order {
return orderRepository.findByOrderNumber(orderNumber)
?: throw SelfOrderManagementException(
Expand Down Expand Up @@ -85,6 +88,7 @@ open class OrderService(

providePaymentRequestUseCase.providePaymentRequest(order)

log.info("Storing order $order")
orderRepository.upsert(order.copy(status = OrderStatus.PENDING)
.copy(items = order.items.map { i -> i.copy(orderNumber = order.number) }))
}
Expand All @@ -95,6 +99,7 @@ open class OrderService(
val order = getByOrderNumber(orderNumber)
when (order.status) {
OrderStatus.PENDING -> {
log.info("Confirming order $order")
orderRepository.upsert(order.copy(status = OrderStatus.CONFIRMED))
}
else -> {
Expand All @@ -110,8 +115,9 @@ open class OrderService(
override fun startOrderPreparation(orderNumber: Long): Order {
return getByOrderNumber(orderNumber)
.takeIf { it.status == OrderStatus.CONFIRMED }
?.run {
orderRepository.upsert(copy(status = OrderStatus.PREPARING))
?.let { order ->
log.info("Starting preparation of order $order")
orderRepository.upsert(order.copy(status = OrderStatus.PREPARING))
}
?: throw SelfOrderManagementException(
errorType = ErrorType.INVALID_ORDER_STATE_TRANSITION,
Expand All @@ -122,8 +128,9 @@ open class OrderService(
override fun completeOrder(orderNumber: Long): Order {
return getByOrderNumber(orderNumber)
.takeIf { it.status == OrderStatus.PREPARING }
?.run {
orderRepository.upsert(copy(status = OrderStatus.COMPLETED))
?.let { order ->
log.info("Completing order $order")
orderRepository.upsert(order.copy(status = OrderStatus.COMPLETED))
}
?: throw SelfOrderManagementException(
errorType = ErrorType.INVALID_ORDER_STATE_TRANSITION,
Expand All @@ -134,8 +141,9 @@ open class OrderService(
override fun finishOrderPreparation(orderNumber: Long): Order {
return getByOrderNumber(orderNumber)
.takeIf { it.status == OrderStatus.COMPLETED }
?.run {
orderRepository.upsert(copy(status = OrderStatus.DONE))
?.let { order ->
log.info("Finishing preparation of order $order")
orderRepository.upsert(order.copy(status = OrderStatus.DONE))
}
?: throw SelfOrderManagementException(
errorType = ErrorType.INVALID_ORDER_STATE_TRANSITION,
Expand All @@ -147,14 +155,15 @@ open class OrderService(
return transactionalRepository.transaction {
getByOrderNumber(orderNumber)
.takeIf { it.status != OrderStatus.COMPLETED && it.status != OrderStatus.DONE }
?.run {
if (status == OrderStatus.CREATED || status == OrderStatus.CONFIRMED) {
?.let { order ->
log.info("Cancelling order $order")
if (order.status == OrderStatus.CREATED || order.status == OrderStatus.CONFIRMED) {
// in this case, make reserved products available again
items.forEach {
order.items.forEach {
it.number?.let { number -> adjustInventoryUseCase.increment(number, 1) }
}
}
orderRepository.upsert(copy(status = OrderStatus.CANCELLED))
orderRepository.upsert(order.copy(status = OrderStatus.CANCELLED))
}
?: throw SelfOrderManagementException(
errorType = ErrorType.INVALID_ORDER_STATE_TRANSITION,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package com.fiap.order.usecases.services

import com.fiap.order.adapter.gateway.PaymentGateway
import com.fiap.order.adapter.gateway.PaymentProviderGateway
import com.fiap.order.domain.entities.Order
import com.fiap.order.domain.entities.Payment
import com.fiap.order.domain.errors.ErrorType
import com.fiap.order.domain.errors.SelfOrderManagementException
import com.fiap.order.domain.valueobjects.PaymentStatus
import com.fiap.order.driver.web.request.PaymentRequest
import com.fiap.order.usecases.LoadPaymentUseCase
import com.fiap.order.usecases.ProvidePaymentRequestUseCase
import java.time.LocalDateTime
import org.slf4j.LoggerFactory

class PaymentService(
private val paymentRepository: PaymentGateway,
private val paymentGateway: PaymentGateway,
) :
LoadPaymentUseCase,
ProvidePaymentRequestUseCase {
private val log = LoggerFactory.getLogger(javaClass)

override fun getByOrderNumber(orderNumber: Long): Payment {
return paymentRepository.findByOrderNumber(orderNumber)
log.info("Requesting payment for order [$orderNumber]")
return paymentGateway.findByOrderNumber(orderNumber)
?: throw SelfOrderManagementException(
errorType = ErrorType.PAYMENT_NOT_FOUND,
message = "Payment not found for order [$orderNumber]",
)
}

override fun providePaymentRequest(order: Order): PaymentRequest {
return paymentRepository.createFromOrder(order)
log.info("Requesting payment request for order $order")
return paymentGateway.createFromOrder(order)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import com.fiap.order.domain.entities.Product
import com.fiap.order.domain.errors.ErrorType
import com.fiap.order.domain.errors.SelfOrderManagementException
import com.fiap.order.usecases.LoadProductUseCase
import org.slf4j.LoggerFactory

class ProductService(
private val productRepository: ProductGateway,
private val productGateway: ProductGateway,
) : LoadProductUseCase {
private val log = LoggerFactory.getLogger(javaClass)

override fun getByProductNumber(productNumber: Long): Product {
return productRepository.findByProductNumber(productNumber)
log.info("Requesting product [$productNumber]")
return productGateway.findByProductNumber(productNumber)
?: throw SelfOrderManagementException(
errorType = ErrorType.PRODUCT_NOT_FOUND,
message = "Product [$productNumber] not found",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ package com.fiap.order.usecases.services
import com.fiap.order.adapter.gateway.StockGateway
import com.fiap.order.domain.entities.Stock
import com.fiap.order.usecases.AdjustStockUseCase

import org.slf4j.LoggerFactory

class StockService(
private val stockGateway: StockGateway,
) : AdjustStockUseCase {
private val log = LoggerFactory.getLogger(javaClass)

override fun increment(
componentNumber: Long,
quantity: Long,
): Stock {
log.info("Requesting stock of [$componentNumber] to be incremented")
return stockGateway.increment(componentNumber, quantity)
}

override fun decrement(
componentNumber: Long,
quantity: Long,
): Stock {
log.info("Requesting stock of [$componentNumber] to be decremented")
return stockGateway.decrement(componentNumber, quantity)

}


}

0 comments on commit e7982dc

Please sign in to comment.