Initial commit
This commit is contained in:
62
store-persistence/pom.xml
Normal file
62
store-persistence/pom.xml
Normal file
@@ -0,0 +1,62 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>store-persistence</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<artifactId>hexagonal-architecture</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../../hexagonal-architecture</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.javafaker</groupId>
|
||||
<artifactId>javafaker</artifactId>
|
||||
<version>${faker.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>store-core</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.1.3.RELEASE</spring.boot.version>
|
||||
<faker.version>0.17.2</faker.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.hexagonal.store.persistence;
|
||||
|
||||
import com.baeldung.hexagonal.store.persistence.config.EntityConfig;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories("com.baeldung.hexagonal.store.persistence")
|
||||
@Import({EntityConfig.class})
|
||||
@ComponentScan(basePackages = "com.baeldung.hexagonal.store.persistence")
|
||||
public class PersistenceConfig {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.hexagonal.store.persistence.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EntityScan(basePackages = {"com.baeldung.hexagonal.store.core"})
|
||||
public class EntityConfig {
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.hexagonal.store.persistence.listener;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.customer.entity.Customer;
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.Order;
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.OrderProduct;
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.OrderProductId;
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.Product;
|
||||
import com.baeldung.hexagonal.store.persistence.repo.customer.CustomerRepository;
|
||||
import com.baeldung.hexagonal.store.persistence.repo.order.OrderProductRepository;
|
||||
import com.baeldung.hexagonal.store.persistence.repo.order.OrderRepository;
|
||||
import com.baeldung.hexagonal.store.persistence.repo.product.ProductRepository;
|
||||
import com.github.javafaker.Faker;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class StartupFakeDataSeeder implements ApplicationListener<ApplicationReadyEvent> {
|
||||
public static final int FAKE_CUSTOMER_COUNT = 10;
|
||||
public static final int FAKE_PRODUCT_COUNT = 40;
|
||||
private final OrderRepository orderRepository;
|
||||
private final OrderProductRepository orderProductRepository;
|
||||
private final ProductRepository productRepository;
|
||||
private final CustomerRepository customerRepository;
|
||||
private Faker faker = new Faker();
|
||||
|
||||
@Autowired
|
||||
public StartupFakeDataSeeder(CustomerRepository customerRepository, OrderRepository orderRepository, OrderProductRepository orderProductRepository, ProductRepository productRepository) {
|
||||
this.customerRepository = customerRepository;
|
||||
this.orderRepository = orderRepository;
|
||||
this.orderProductRepository = orderProductRepository;
|
||||
this.productRepository = productRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
|
||||
seedData();
|
||||
}
|
||||
|
||||
private void seedData() {
|
||||
List<Product> products = generateRandomProducts();
|
||||
for (int i = 0; i < FAKE_CUSTOMER_COUNT; i++) {
|
||||
Customer customer = new Customer();
|
||||
customer.setFirstname(faker.name().firstName());
|
||||
customer.setLastname(faker.name().lastName());
|
||||
customer.setBalance(faker.number().randomDouble(3, 0, 2000));
|
||||
customer.setEmail(faker.internet().emailAddress());
|
||||
Order order = generateRandomOrder(products);
|
||||
order.setCustomer(customer);
|
||||
order.setStatus(faker.code().asin());
|
||||
customer.addOrder(order);
|
||||
customerRepository.save(customer);
|
||||
}
|
||||
}
|
||||
|
||||
private Order generateRandomOrder(List<Product> products) {
|
||||
Order order = new Order();
|
||||
long orderItems = faker.number().randomNumber(1, false);
|
||||
List<OrderProduct> orderProducts = new ArrayList<>();
|
||||
List<Product> addedProducts = new ArrayList<>();
|
||||
for (int i = 0; i < orderItems; i++) {
|
||||
Product product = null;
|
||||
while (product == null || addedProducts.contains(product)) {
|
||||
product = products.get(faker.random().nextInt(0, products.size() - 1));
|
||||
}
|
||||
OrderProduct orderProduct = new OrderProduct();
|
||||
orderProduct.setId(new OrderProductId(order, product));
|
||||
addedProducts.add(product);
|
||||
orderProduct.setQuantity((int) faker.number().randomNumber(1, false));
|
||||
orderProducts.add(orderProduct);
|
||||
}
|
||||
order.setOrderProducts(orderProducts);
|
||||
return order;
|
||||
}
|
||||
|
||||
private List<Product> generateRandomProducts() {
|
||||
List<Product> products = new ArrayList<>();
|
||||
for (int i = 0; i < FAKE_PRODUCT_COUNT; i++) {
|
||||
Product product = new Product();
|
||||
product.setName(faker.commerce().productName());
|
||||
product.setPrice(faker.random().nextDouble() * 100);
|
||||
productRepository.save(product);
|
||||
products.add(product);
|
||||
}
|
||||
return products;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.hexagonal.store.persistence.repo.customer;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.customer.entity.Customer;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CustomerRepository extends CrudRepository<Customer, Long> {
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.hexagonal.store.persistence.repo.customer;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.customer.entity.Customer;
|
||||
import com.baeldung.hexagonal.store.core.context.customer.infrastructure.CustomerDataStore;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class CustomerRepositoryImpl implements CustomerDataStore {
|
||||
@Autowired
|
||||
private CustomerRepository customerRepository;
|
||||
|
||||
@Override
|
||||
public Customer save(Customer customer) {
|
||||
return customerRepository.save(customer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Customer> findById(Long customerId) {
|
||||
return customerRepository.findById(customerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Customer> findAll() {
|
||||
return customerRepository.findAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.hexagonal.store.persistence.repo.order;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.OrderProduct;
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.OrderProductId;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OrderProductRepository extends CrudRepository<OrderProduct, OrderProductId> {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.hexagonal.store.persistence.repo.order;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.OrderProduct;
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.OrderProductId;
|
||||
import com.baeldung.hexagonal.store.core.context.order.infrastructure.OrderProductDataStore;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class OrderProductRepositoryImpl implements OrderProductDataStore {
|
||||
@Autowired
|
||||
private OrderProductRepository orderProductRepository;
|
||||
|
||||
@Override
|
||||
public OrderProduct save(OrderProduct orderProduct) {
|
||||
return this.orderProductRepository.save(orderProduct);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<OrderProduct> findById(OrderProductId orderProductPkId) {
|
||||
return this.orderProductRepository.findById(orderProductPkId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.hexagonal.store.persistence.repo.order;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.Order;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OrderRepository extends CrudRepository<Order, Long> {
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.hexagonal.store.persistence.repo.order;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.Order;
|
||||
import com.baeldung.hexagonal.store.core.context.order.infrastructure.OrderDataStore;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class OrderRepositoryImpl implements OrderDataStore {
|
||||
@Autowired
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
@Override
|
||||
public Order save(Order order) {
|
||||
return this.orderRepository.save(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Order> findById(Long orderId) {
|
||||
return this.orderRepository.findById(orderId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Order> findAll() {
|
||||
return this.orderRepository.findAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.hexagonal.store.persistence.repo.product;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.Product;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ProductRepository extends CrudRepository<Product, Long> {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.hexagonal.store.persistence.repo.product;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.order.entity.Product;
|
||||
import com.baeldung.hexagonal.store.core.context.order.infrastructure.ProductDataStore;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class ProductRepositoryImpl implements ProductDataStore {
|
||||
@Autowired
|
||||
private ProductRepository productRepository;
|
||||
|
||||
@Override
|
||||
public Product save(Product product) {
|
||||
return this.productRepository.save(product);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Product> findById(Long productId) {
|
||||
return this.productRepository.findById(productId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user