Skip to content

Commit

Permalink
fixed some report errors
Browse files Browse the repository at this point in the history
  • Loading branch information
majimearun committed Dec 6, 2022
1 parent 403ca9f commit 13ffe47
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -142,8 +145,21 @@ public List<UserInfo> getPendingManagers(Long adminId){
public List<SendCart> getItemsSoldOnADate(Long adminId, LocalDate date){
if(checkAdminStatus(adminId)){
List<Order> orders = orderRepository.findByOrderDate(date);
List<SendCart> send = orders.stream().map(order -> new SendCart(snapshotToProduct(order.getProduct()), order.getQuantity())).collect(Collectors.toList());
Map<ProductSnapshot, Integer> productToQuantity = new HashMap<>();
for(Order order : orders){
if(productToQuantity.containsKey(order.getProduct())){
productToQuantity.put(order.getProduct(), productToQuantity.get(order.getProduct()) + order.getQuantity());
}
else{
productToQuantity.put(order.getProduct(), order.getQuantity());
}
}
List<SendCart> send = new ArrayList<>();
for(Map.Entry<ProductSnapshot, Integer> entry : productToQuantity.entrySet()){
send.add(new SendCart(snapshotToProduct(entry.getKey()), entry.getValue()));
}
return send;

}
else{
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User does not have Admin level access or is not logged in");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public List<Product> getProductsByCategory(int category) {
public List<Product> getProductsByName(String name) {
List<Product> products = productRepository.findByNameIgnoreCaseContaining(name);
if(products.isEmpty()){
List<Product> productsByDescription = getProductsByDescription(name);
List<Product> productsByDescription = productRepository.findByDescriptionIgnoreCaseContaining(name);

if(productsByDescription.isEmpty()){
List<Product> fuzzyName = getProductsByFuzzyName(name);
Expand Down Expand Up @@ -172,15 +172,6 @@ private List<Product> getProductsByFuzzyName(String name) {
return fProducts.subList(0, Math.min(5, fProducts.size()));
}

@Transactional
private List<Product> getProductsByDescription(String name) {
List<Product> products = productRepository.findByDescriptionIgnoreCaseContaining(name);
if(products.isEmpty()){
return getProductsByFuzzyDescription(name);
}
return products;
}

@Transactional
private List<Product> getProductsByFuzzyDescription(String name) {
List<Product> products = getProducts();
Expand Down Expand Up @@ -239,7 +230,7 @@ public List<SendOrder> getOrders(Long userId){
.build();
List<Order> orders = orderRepository.findByCustomer(customerSnapshot);
List<SendOrder> send = orders.stream().map(order -> new SendOrder(order.getOrderTransactionId(),order.getProduct(), order.getQuantity(), order.getOrderDate())).collect(Collectors.toList());
return send;
return send.stream().sorted((o1, o2) -> o2.getOrderDate().compareTo(o1.getOrderDate())).collect(Collectors.toList());
}
else{
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User not logged in");
Expand Down

0 comments on commit 13ffe47

Please sign in to comment.