From e016103cbf03f9a8630edd33aaf5187c82f46707 Mon Sep 17 00:00:00 2001
From: "roxanne.grant" <roxanne.grant@etu.hesge.ch>
Date: Sun, 3 Feb 2019 21:18:11 +0100
Subject: [PATCH] Error resolved : Credit an account that doesn't exist and
 stock a product that doesn't exist

---
 .../src/main/java/ch/hepia/account/AccountService.java |  9 +++++++--
 .../src/main/java/ch/hepia/stock/StockService.java     | 10 ++++++++--
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/services/src/main/java/ch/hepia/account/AccountService.java b/services/src/main/java/ch/hepia/account/AccountService.java
index d57e225..3377d9b 100644
--- a/services/src/main/java/ch/hepia/account/AccountService.java
+++ b/services/src/main/java/ch/hepia/account/AccountService.java
@@ -84,8 +84,13 @@ public class AccountService{
                             //------------------------------------------
                             if (obj instanceof EventCreditAnAccount){
                                 EventCreditAnAccount e = (EventCreditAnAccount) obj;
-                                accountsDB.creditAnAccount(e.idAccount(), e.amount());
-                                send(new EventAccountCredited(e.id(), "The account is credited"));
+                                if(accountsDB.accountPresent(e.idAccount())){
+                                    accountsDB.creditAnAccount(e.idAccount(), e.amount());
+                                    send(new EventAccountCredited(e.id(), "The account is credited"));
+                                }
+                                else{
+                                    send(new EventAccountDoesntExist(e.id(), "The account doesn't exist"));
+                                }
                                 System.out.println(e.toString());
                             }
                             //------------------------------------------
diff --git a/services/src/main/java/ch/hepia/stock/StockService.java b/services/src/main/java/ch/hepia/stock/StockService.java
index ab32d84..206c39d 100644
--- a/services/src/main/java/ch/hepia/stock/StockService.java
+++ b/services/src/main/java/ch/hepia/stock/StockService.java
@@ -89,8 +89,14 @@ public class StockService {
                             // ------------------------------------------
                             if (obj instanceof EventAddProductQuantity) {
                                 EventAddProductQuantity e = (EventAddProductQuantity) obj;
-                                stockDB.addQuantity(e.idProduct(), e.quantity());
-                                send(new EventProductQuantityAdded(e.id(), "Quantity added to the stock"));
+                                if(productsDB.getById(e.idProduct()).isPresent()){
+                                    stockDB.addQuantity(e.idProduct(), e.quantity());
+                                    send(new EventProductQuantityAdded(e.id(), "Quantity added to the stock"));
+                                }
+                                else{
+                                    send(new EventProductDoesntExist(e.id(), "Product doesn't exist"));
+                                }
+                                
                                 System.out.println(e.toString());
                             }
 
-- 
GitLab