43
loading...
This website collects cookies to deliver better user experience
CREATE TABLE orders (
id INT PRIMARY KEY NOT NULL,
customer_id INT NOT NULL,
payment_id INT NOT NULL,
order_date timestamp NOT NULL,
description VARCHAR(255)
);
CREATE TABLE customers (
id INT PRIMARY KEY NOT NULL,
email_address VARCHAR(255) NOT NULL,
name VARCHAR(255),
UNIQUE (email_address)
);
CREATE TABLE payments (
id INT PRIMARY KEY NOT NULL,
transaction_id VARCHAR(36) NOT NULL,
amount DECIMAL(12,2),
customer_id INT NOT NULL
);
{
"description" : "Sample Order #4",
"emailAddress" : "[email protected]",
"name" : "Brian Johnson",
"amount" : 19.99
}
{
"emailAddress" : "[email protected]",
"name" : "Brian Johnson"
}
public CustomerDto getCustomer(String emailAddress, String name) {
CustomerDto customerDto = new CustomerDto();
customerDto.setEmailAddress(emailAddress);
customerDto.setName(name);
return rabbitTemplate.convertSendAndReceiveAsType(customerDirectExchange.getName(),
messagingConfigurationProperties.getCustomerRoutingKey(),
customerDto,
new ParameterizedTypeReference<>() {});
}
@RabbitListener(queues = "#{messagingConfigurationProperties.customerRequestQueue}")
@Transactional(propagation = Propagation.REQUIRES_NEW)
public CustomerDto receive(CustomerDto customerDto) {
log.debug("CustomerProcessor: receive(customerDto={})", customerDto);
Customer customer = customerRepository.findByEmailAddressEquals(customerDto.getEmailAddress());
if (customer != null) {
log.debug("Found existing customer={}", customer);
// return customer as a CustomerDto
} else {
log.info("Creating new customer={}", customerDto);
// return new customer as a CustomerDto
}
log.debug("customerDto={}", customerDto);
return customerDto;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
public class CustomerDto {
private int id;
private String emailAddress;
private String Name;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PaymentDto {
private int id;
private String transactionId;
private BigDecimal amount;
private int customerId;
}
@RabbitListener(queues = "#{messagingConfigurationProperties.paymentRequestQueue}")
@Transactional(propagation = Propagation.REQUIRES_NEW)
public PaymentDto receive(PaymentDto paymentDto) {
log.debug("PaymentProcessor: receive(paymentDto={})", paymentDto);
Payment payment = new Payment();
payment.setAmount(paymentDto.getAmount());
payment.setCustomerId(paymentDto.getCustomerId());
payment.setTransactionId(UUID.randomUUID().toString());
paymentRepository.save(payment);
paymentDto.setId(payment.getId());
paymentDto.setTransactionId(payment.getTransactionId());
return paymentDto;
}
curl --location --request POST 'https://jvc-order.herokuapp.com/orders' \
--header 'Content-Type: application/json' \
--data-raw '{
"description" : "Sample Order #4",
"emailAddress" : "[email protected]",
"name" : "Brian Johnson",
"amount" : 19.99
}'
{
"id": 4,
"customerId": 4,
"paymentId": 4,
"orderDate": "2021-06-07T04:31:52.497082",
"description": "Sample Order #4"
}
GET https://jvc-customer.herokuapp.com/customers/4
{
"id": 4,
"emailAddress": "[email protected]",
"name": "Brian Johnson"
}
GET https://jvc-payment.herokuapp.com/payments/4
{
"id": 4,
"transactionId": "3fcb379e-cb89-4013-a141-c6fad4b55f6b",
"amount": 19.99,
"customerId": 4
}
GET https://jvc-order.herokuapp.com/orders/4
{
"id": 4,
"customerId": 4,
"paymentId": 4,
"orderDate": "2021-06-07T04:31:52.497082",
"description": "Sample Order #4"
}
“Focus your time on delivering features/functionality which extends the value of your intellectual property. Leverage frameworks, products, and services for everything else.”