renamed folder
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package io.pratik.dynamodbapps;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DynamodbappsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DynamodbappsApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.dynamodbapps;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import io.pratik.dynamodbapps.models.Order;
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
||||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
|
||||
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
|
||||
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
|
||||
import software.amazon.awssdk.enhanced.dynamodb.Key;
|
||||
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
public class OrderRepository {
|
||||
|
||||
@Autowired
|
||||
private DynamoDbEnhancedClient dynamoDbenhancedClient ;
|
||||
|
||||
private DynamoDbTable<Order> orderTable ;
|
||||
|
||||
|
||||
|
||||
public OrderRepository() {
|
||||
super();
|
||||
}
|
||||
|
||||
// Store this order item in the database
|
||||
public void save(final Order order) {
|
||||
DynamoDbTable<Order> orderTable = getTable();
|
||||
|
||||
orderTable.putItem(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
private DynamoDbTable<Order> getTable() {
|
||||
DynamoDbTable<Order> orderTable =
|
||||
dynamoDbenhancedClient.table("Order",
|
||||
TableSchema.fromBean(Order.class));
|
||||
return orderTable;
|
||||
}
|
||||
|
||||
// Retrieve a single order item from the database
|
||||
public Order getOrder(final String customerID, final String orderID) {
|
||||
DynamoDbTable<Order> orderTable = getTable();
|
||||
// Construct the key with partition and sort key
|
||||
Key key = Key.builder().partitionValue(customerID)
|
||||
.sortValue(orderID)
|
||||
.build();
|
||||
|
||||
Order order = orderTable.getItem(key);
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.dynamodbapps.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
||||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
|
||||
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
@Bean
|
||||
public DynamoDbClient getDynamoDbClient() {
|
||||
AwsCredentialsProvider credentialsProvider = DefaultCredentialsProvider.builder().profileName("pratikpoc")
|
||||
.build();
|
||||
|
||||
return DynamoDbClient.builder().region(Region.US_EAST_1).credentialsProvider(credentialsProvider).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DynamoDbEnhancedClient getDynamoDbEnhancedClient() {
|
||||
return DynamoDbEnhancedClient.builder()
|
||||
.dynamoDbClient(getDynamoDbClient())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.dynamodbapps.models;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbAttribute;
|
||||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
|
||||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
|
||||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
@DynamoDbBean
|
||||
public class Order {
|
||||
private String customerID;
|
||||
private String orderID;
|
||||
private double orderValue;
|
||||
private Instant createdDate;
|
||||
|
||||
private List<Product> products;
|
||||
|
||||
@DynamoDbPartitionKey
|
||||
@DynamoDbAttribute("CustomerID")
|
||||
public String getCustomerID() {
|
||||
return customerID;
|
||||
}
|
||||
public void setCustomerID(String customerID) {
|
||||
this.customerID = customerID;
|
||||
}
|
||||
|
||||
@DynamoDbSortKey
|
||||
@DynamoDbAttribute("OrderID")
|
||||
public String getOrderID() {
|
||||
return orderID;
|
||||
}
|
||||
public void setOrderID(String orderID) {
|
||||
this.orderID = orderID;
|
||||
}
|
||||
|
||||
public Instant getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
public void setCreatedDate(Instant createdDate) {
|
||||
this.createdDate = createdDate;
|
||||
}
|
||||
public double getOrderValue() {
|
||||
return orderValue;
|
||||
}
|
||||
public void setOrderValue(double orderValue) {
|
||||
this.orderValue = orderValue;
|
||||
}
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
public void setProducts(List<Product> products) {
|
||||
this.products = products;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.dynamodbapps.models;
|
||||
|
||||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
@DynamoDbBean
|
||||
public class Product {
|
||||
private String name;
|
||||
private String brand;
|
||||
private double price;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.pratik.dynamodbapps;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class DynamodbappsApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.dynamodbapps;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import io.pratik.dynamodbapps.models.Order;
|
||||
import io.pratik.dynamodbapps.models.Product;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
@SpringBootTest
|
||||
class OrderRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@AfterEach
|
||||
void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateOrder() {
|
||||
Order order = new Order();
|
||||
order.setOrderID("ORD-010");
|
||||
order.setCustomerID("CUST-001");
|
||||
|
||||
List<Product> products = new ArrayList<Product>();
|
||||
|
||||
Product product = new Product();
|
||||
product.setName("Television");
|
||||
product.setBrand("samsung");
|
||||
product.setPrice(112.56);
|
||||
products.add(product);
|
||||
|
||||
product = new Product();
|
||||
product.setName("Washing Machine");
|
||||
product.setBrand("panasonic");
|
||||
product.setPrice(119.99);
|
||||
products.add(product);
|
||||
|
||||
order.setProducts(products );
|
||||
order.setOrderValue(56.7);
|
||||
order.setCreatedDate(Instant.now());
|
||||
orderRepository.save(order);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetOrder() {
|
||||
Order order =
|
||||
orderRepository.getOrder("CUST-001", "ORD-010");
|
||||
System.out.println("order "+order.getProducts());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user