-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Product&Order repos implementations
- Loading branch information
Showing
2 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/main/java/io/github/rebelizant/jaxrsexample/persistence/OrderRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.github.rebelizant.jaxrsexample.persistence; | ||
|
||
import io.github.rebelizant.jaxrsexample.domain.*; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import javax.annotation.PostConstruct; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import java.util.stream.IntStream; | ||
import java.util.stream.Collectors; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Component | ||
public class OrderRepositoryImpl implements OrderRepository { | ||
|
||
private ConcurrentMap<Long, Order> orders; | ||
|
||
@Autowired | ||
private ProductRepository productRepository; | ||
|
||
@Autowired | ||
private CustomerRepository customerRepository; | ||
|
||
public OrderRepositoryImpl() { | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
orders = IntStream.range(1, 41).mapToObj(id -> randomOrder(id)) | ||
.collect(Collectors.toConcurrentMap(o -> o.getId(), o -> o)); | ||
} | ||
|
||
@Override | ||
public void addOrder(Order order) { | ||
Long nextId = nextId(orders); | ||
order.setId(nextId); | ||
orders.putIfAbsent(nextId, order); | ||
} | ||
|
||
@Override | ||
public List<Order> getAllOrders() { | ||
return orders.values().stream().collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public List<Order> getOrdersByCustomer(Long customerId) { | ||
return orders.values().stream() | ||
.filter(o -> o.getCustomer().getId().equals(customerId)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public void updateOrder(Long orderId, Order order) { | ||
order.setId(orderId); | ||
orders.put(orderId, order); | ||
} | ||
|
||
@Override | ||
public Order getOrder(Long orderId) { | ||
return orders.get(orderId); | ||
} | ||
|
||
@Override | ||
public void deleteOrder(Long orderId) { | ||
orders.remove(orderId); | ||
} | ||
|
||
private Order randomOrder(int id) { | ||
Order order = new Order(); | ||
order.setId(Long.valueOf(id)); | ||
List<OrderItem> orderItems = IntStream.range(1, id % 10 + 1).mapToObj(i -> randomOrderItem(i)).collect(Collectors.toList()); | ||
order.setOrderItems(orderItems); | ||
order.setCustomer(customerRepository.getCustomer(Long.valueOf(id % 20 + 1))); | ||
return order; | ||
} | ||
|
||
private OrderItem randomOrderItem(int id) { | ||
OrderItem orderItem = new OrderItem(); | ||
if (Math.random() > 0.9) { | ||
orderItem.setCost(BigDecimal.valueOf(Math.random() * 10)); | ||
} | ||
orderItem.setQuantity(Math.random() > 0.5 ? 2 : 1); | ||
orderItem.setProduct(productRepository.getProduct(Long.valueOf(id))); | ||
return orderItem; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/io/github/rebelizant/jaxrsexample/persistence/ProductRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.github.rebelizant.jaxrsexample.persistence; | ||
|
||
import io.github.rebelizant.jaxrsexample.domain.*; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
import java.util.stream.Collectors; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ProductRepositoryImpl implements ProductRepository { | ||
|
||
private ConcurrentMap<Long, Product> products; | ||
|
||
public ProductRepositoryImpl() { | ||
products = IntStream.range(1, 11).mapToObj(id -> randomProduct(id)) | ||
.collect(Collectors.toConcurrentMap(p -> p.getId(), p -> p)); | ||
} | ||
|
||
public void addProduct(Product product) { | ||
Long nextId = nextId(products); | ||
product.setId(nextId); | ||
products.put(nextId, product); | ||
} | ||
|
||
public Product getProduct(Long id) { | ||
return products.get(id); | ||
} | ||
|
||
public List<Product> getProducts(int start, int count) { | ||
Stream<Product> stream = products.values().stream().skip(start > 0 ? start - 1 : start); | ||
if (count > 0) { | ||
return stream.limit(count).collect(Collectors.toList()); | ||
} | ||
return stream.collect(Collectors.toList()); | ||
} | ||
|
||
public void deleteProduct(Long id) { | ||
products.remove(id); | ||
} | ||
|
||
public void updateProduct(Long id, Product product) { | ||
product.setId(id); | ||
products.put(id, product); | ||
} | ||
|
||
private Product randomProduct(int id) { | ||
Product product = new Product(); | ||
product.setId(Long.valueOf(id)); | ||
product.setName(String.format("Name-%s", id)); | ||
product.setCost(BigDecimal.valueOf(Math.random() * 10)); | ||
return product; | ||
} | ||
|
||
} |