Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from appdevgbb/gok/order-service
Browse files Browse the repository at this point in the history
add dml scripts and calculation logic; closes #7, resolves #8
  • Loading branch information
thegovind committed Sep 9, 2022
2 parents 41de79e + 5f93c33 commit 12a4b71
Show file tree
Hide file tree
Showing 18 changed files with 1,441 additions and 46 deletions.
1 change: 1 addition & 0 deletions order-service/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ build/

### Local Dev ###
.env
/.jpb/
9 changes: 8 additions & 1 deletion order-service/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ services:
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_DATABASE=${MYSQL_DATABASE}

rabbitmq:
image: rabbitmq:3-management
ports:
- "4369:4369"
- "5671:5671"
- "5672:5672"
- "25672:25672"
- "15672:15672"
volumes:
zookeeper_data:
driver: local
Expand Down
5 changes: 5 additions & 0 deletions order-service/flyway.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Schema, user, password defined in environment variables
flyway.cleanDisabled=false
flyway.url=jdbc:mysql://localhost:3306/reddog
flyway.user=reddog
flyway.password=reddog
54 changes: 29 additions & 25 deletions order-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Azure Spring Apps capabilities-->

<!--Azure Spring Apps capabilities-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
Expand All @@ -57,18 +60,14 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-cloud-starter-sleuth-demo</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-starter</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-starter-actuator</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-starter</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-starter-actuator</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
Expand All @@ -77,10 +76,10 @@
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-trace-sleuth</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-trace-sleuth</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
Expand Down Expand Up @@ -135,18 +134,23 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-dependencies</artifactId>-->
<!-- <version>${spring-cloud-azure.version}</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-dependencies</artifactId>-->
<!-- <version>${spring-cloud-azure.version}</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>9.3.0</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.kafka.annotation.EnableKafka;

@EnableKafka
@SpringBootApplication
@EnableEurekaClient
public class OrderServiceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.microsoft.gbb.rasa.orderservice.controller;

import com.microsoft.gbb.rasa.orderservice.entities.CustomerOrder;
import com.microsoft.gbb.rasa.orderservice.entities.OrderSummary;
import com.microsoft.gbb.rasa.orderservice.exception.OrderNotFoundException;
import com.microsoft.gbb.rasa.orderservice.service.OrderService;
import org.springframework.http.HttpStatus;
Expand All @@ -20,11 +21,10 @@ public OrderController(OrderService orderService) {
produces = "application/json")
@ResponseStatus(HttpStatus.CREATED)
@CrossOrigin(origins = "*")
public String order(@RequestBody CustomerOrder order) {
public OrderSummary order(@RequestBody CustomerOrder order) {
if (null == order) {
throw new OrderNotFoundException("Order is null");
}
// TODO: asynchronously create order
return orderService.createOrder(order);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.microsoft.gbb.rasa.orderservice.dto;

import java.io.Serializable;
import java.util.List;

public record CustomerOrderDto(Long customerOrderId, String loyaltyId, String firstName, String lastName,
String storeId) implements Serializable {
String storeId, List<CustomerOrderItemDto> orderItems) implements Serializable {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.microsoft.gbb.rasa.orderservice.dto;

import lombok.Builder;

import java.io.Serializable;
import java.util.Collection;

@Builder
public record OrderItemSummaryDto(String productName, Collection<ProductDto> products, int quantity, double unitCost,
String imageUrl, OrderSummaryDto orderSummary,
Long orderSummaryId) implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.microsoft.gbb.rasa.orderservice.dto;

import lombok.Builder;

import java.io.Serializable;
import java.util.Date;

@Builder
public record OrderSummaryDto(Long orderId, Date orderCompletedDate, String loyaltyId, String firstName,
String lastName, String storeId, Date orderDate,
double orderTotal) implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.microsoft.gbb.rasa.orderservice.entities;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
Expand All @@ -12,6 +10,9 @@
@Setter
@ToString
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name = "order_item_summary")
public class OrderItemSummary {
@Column(name = "product_name")
Expand All @@ -21,6 +22,7 @@ public class OrderItemSummary {
@JoinTable(name = "order_item_summary_products",
joinColumns = @JoinColumn(name = "order_item_summary_id"),
inverseJoinColumns = @JoinColumn(name = "products_product_id"))
@ToString.Exclude
private Collection<Product> products = new ArrayList<>();

@Column(name = "quantity", nullable = false)
Expand All @@ -29,6 +31,9 @@ public class OrderItemSummary {
@Column(name = "unit_cost", nullable = false)
private double unitCost;

@Column(name = "unit_price", nullable = false)
private double unitPrice;

@Column(name = "image_url")
private String imageUrl;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.microsoft.gbb.rasa.orderservice.entities;

import lombok.Getter;
import lombok.ToString;
import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
Expand All @@ -11,6 +10,9 @@
@Getter
@ToString
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name = "order_summary")
public class OrderSummary {
@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import com.microsoft.gbb.rasa.orderservice.entities.Product;
import org.springframework.data.repository.CrudRepository;

import java.util.ArrayList;

public interface ProductRepository extends CrudRepository<Product, Long> {
ArrayList<Product> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package com.microsoft.gbb.rasa.orderservice.service;

import com.microsoft.gbb.rasa.orderservice.entities.CustomerOrder;
import com.microsoft.gbb.rasa.orderservice.entities.OrderItemSummary;
import com.microsoft.gbb.rasa.orderservice.entities.OrderSummary;
import com.microsoft.gbb.rasa.orderservice.entities.Product;
import com.microsoft.gbb.rasa.orderservice.messaging.TopicProducer;
import com.microsoft.gbb.rasa.orderservice.repositories.CustomerOrderRepository;
import com.microsoft.gbb.rasa.orderservice.repositories.ProductRepository;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

/**
* The type Order service.
Expand All @@ -19,19 +30,66 @@
public class CustomerOrderService implements OrderService {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomerOrderService.class);
private final TopicProducer topicProducer;
private final ProductRepository productRepository;
private final CustomerOrderRepository customerOrderRepository;

public CustomerOrderService(TopicProducer topicProducer) {
public CustomerOrderService(TopicProducer topicProducer,
ProductRepository productRepository,
CustomerOrderRepository customerOrderRepository) {
this.topicProducer = topicProducer;
this.productRepository = productRepository;
this.customerOrderRepository = customerOrderRepository;
}
/**
* Create order for customer.
*
* @param order the order
* @return the string
*/
public String createOrder(CustomerOrder order) {
public OrderSummary createOrder(CustomerOrder order) {
LOGGER.info("Creating order");
topicProducer.send(order.toString());
return "Order created successfully";
var orderSummary = getOrderSummary(order);
topicProducer.send(orderSummary.toString());
return orderSummary;
}

public OrderSummary getOrderSummary(CustomerOrder order) {
// Retrieve all the items
ArrayList<Product> products = productRepository.findAll();

// Iterate through the list of ordered items to calculate
// the total and compile a list of item summaries.
AtomicReference<Float> orderTotal = new AtomicReference<>(0.0f);
var itemSummaries = new ArrayList<OrderItemSummary>();
order.getCustomerOrderItems().forEach(orderItem -> {
Product product = products.stream()
.filter((p) -> Objects.equals(p.getProductId(), orderItem.getId()))
.findFirst()
.orElse(null);
if (product == null) return;

orderTotal.updateAndGet(v -> (float) (v + (product.getUnitPrice() * orderItem.getQuantity())));
itemSummaries.add(OrderItemSummary.builder()
.orderSummaryId(orderItem.getId())
.productName(product.getProductName())
.unitPrice(product.getUnitPrice())
.quantity(orderItem.getQuantity())
.unitCost(product.getUnitCost())
.imageUrl(product.getImageUrl())
.build());
});

// return initialized order summary
return OrderSummary.builder()
.orderId(Long.valueOf(UUID.randomUUID().toString()))
.storeId(order.getStoreId())
.firstName(order.getFirstName())
.lastName(order.getLastName())
.loyaltyId(order.getLoyaltyId())
.orderDate(new Date())
.orderItemSummaries(itemSummaries)
.orderTotal(BigDecimal.valueOf(orderTotal.get()).setScale(2, RoundingMode.HALF_DOWN).doubleValue())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.microsoft.gbb.rasa.orderservice.entities.CustomerOrder;
import com.microsoft.gbb.rasa.orderservice.entities.OrderSummary;

/**
* The interface Order service.
Expand All @@ -13,5 +14,5 @@ public interface OrderService {
* @param order the order
* @return the string
*/
public String createOrder(CustomerOrder order);
public OrderSummary createOrder(CustomerOrder order);
}
9 changes: 5 additions & 4 deletions order-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ spring.datasource.username=${MYSQL_USER:reddog}
spring.datasource.password=${MYSQL_PASSWORD:reddog}
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
# handled by flyway
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.flyway.baseline-on-migrate=true

Expand All @@ -24,6 +25,6 @@ auto.create.topics.enable=true
# Consumer properties

# Azure Spring Apps
server.port=0
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
eureka.client.healthcheck.enabled=true
server.port=8090
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

0 comments on commit 12a4b71

Please sign in to comment.