Module Server : utilisation clss SaleContent dans process d'application d'une sale. Impl de ce process dans le StockService

This commit is contained in:
Notmoo 2017-07-30 17:44:31 +02:00
parent dfc2ec93c2
commit ac51f3952d
4 changed files with 34 additions and 9 deletions

View File

@ -3,7 +3,8 @@ package com.pqt.server.module.sale;
import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.Product;
import com.pqt.core.entities.sale.Sale; import com.pqt.core.entities.sale.Sale;
import com.pqt.server.module.stock.StockService; import com.pqt.server.module.stock.StockService;
import com.pqt.server.utils.FileUtil; import com.pqt.server.tools.FileUtil;
import com.pqt.server.tools.entities.SaleContent;
import org.apache.commons.io.input.ReversedLinesFileReader; import org.apache.commons.io.input.ReversedLinesFileReader;
import java.io.*; import java.io.*;
@ -41,7 +42,7 @@ public class NoRevertFileSaleDao implements ISaleDao {
return -1; return -1;
long saleId = nextSaleId; long saleId = nextSaleId;
stockService.applySale(sale.getProducts()); stockService.applySale(new SaleContent(sale));
logSale(sale, saleId); logSale(sale, saleId);
generateNextSaleId(); generateNextSaleId();
return saleId; return saleId;

View File

@ -1,7 +1,8 @@
package com.pqt.server.module.stock; package com.pqt.server.module.stock;
import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.Product;
import com.pqt.server.utils.FileUtil; import com.pqt.server.tools.FileUtil;
import com.pqt.server.tools.entities.SaleContent;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
@ -17,8 +18,7 @@ public class FileStockDao implements IStockDao {
public FileStockDao() { public FileStockDao() {
random = new Random(); random = new Random();
products = new HashMap<>(); loadFromFile();
load();
generateNextProductId(); generateNextProductId();
} }
@ -82,8 +82,30 @@ public class FileStockDao implements IStockDao {
} }
@Override @Override
public void applySale(Map<Product, Integer> productAmounts) { public void applySale(SaleContent saleContent) throws IllegalArgumentException {
//TODO faire ça try {
saleContent.getProductList().forEach(product -> applyRecursiveStockRemoval(product, saleContent.getProductAmount(product)));
}catch (IllegalStateException e){
loadFromFile();
throw new IllegalArgumentException(e);
}
}
private void applyRecursiveStockRemoval(Product product, int amount)throws IllegalStateException{
Product correspondingProduct = getProduct(product.getId());
if(correspondingProduct!=null) {
correspondingProduct.setAmountSold(correspondingProduct.getAmountSold() + amount);
correspondingProduct.setAmountRemaining(correspondingProduct.getAmountRemaining() - amount);
correspondingProduct.getComponents().forEach(component -> applyRecursiveStockRemoval(component, amount));
}else{
StringBuffer sb = new StringBuffer("StockService>StockDao : Un produit vendu ne correspond pas à un produit connu : ");
sb.append(product.getId()).append(" - ").append(product.getName()).append("(").append(product.getCategory()).append(")");
throw new IllegalStateException(sb.toString());
}
}
private void loadFromFile() {
products = new HashMap<>(load());
} }
private Map<Long, Product> load(){ private Map<Long, Product> load(){

View File

@ -1,6 +1,7 @@
package com.pqt.server.module.stock; package com.pqt.server.module.stock;
import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.Product;
import com.pqt.server.tools.entities.SaleContent;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -18,5 +19,5 @@ public interface IStockDao {
public void modifyProduct(long id, Product product); public void modifyProduct(long id, Product product);
void applySale(Map<Product, Integer> productAmounts); void applySale(SaleContent productAmounts) throws IllegalArgumentException;
} }

View File

@ -3,6 +3,7 @@ package com.pqt.server.module.stock;
import com.pqt.core.entities.product.Product; import com.pqt.core.entities.product.Product;
import com.pqt.core.entities.product.ProductUpdate; import com.pqt.core.entities.product.ProductUpdate;
import com.pqt.server.exception.ServerQueryException; import com.pqt.server.exception.ServerQueryException;
import com.pqt.server.tools.entities.SaleContent;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -25,7 +26,7 @@ public class StockService {
return dao.getProduct(id); return dao.getProduct(id);
} }
public void applySale(Map<Product, Integer> productAmounts) { public void applySale(SaleContent productAmounts) {
dao.applySale(productAmounts); dao.applySale(productAmounts);
} }