/**
 * Created by Djidjelly Siclait on 11/26/2016.
 */
package com.evapps.Service.CRUD;

import com.evapps.Entity.History;
import com.evapps.Entity.Product;
import com.evapps.Entity.User;
import com.evapps.Repository.HistoryRepository;
import com.evapps.Repository.ProductRepository;
import com.evapps.Repository.UserRepository;
import freemarker.template.utility.NullArgumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.PersistenceException;

@Service
public class UpdateDataService {

    // Repositories
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private UserRepository userRepository;

    // History Updates
    public void updareRegisteredUserHistory(History history) throws Exception{

        if (history == null)
            throw new NullArgumentException("This history is void");

        try {
            historyRepository.save(history);
        } catch (PersistenceException exp){
            throw new PersistenceException("Persistence Error --> " + exp.getMessage());
        } catch (Exception exp){
            throw new Exception("General Error --> " + exp.getMessage());
        }
    }

    // Product Updates
    public void updateRegisteredProduct(Product product) throws Exception {

        if (product == null)
            throw new NullArgumentException("This product is void");

        try {
            productRepository.save(product);
        } catch (PersistenceException exp){
            throw new PersistenceException("Persistence Error --> " + exp.getMessage());
        } catch (Exception exp){
            throw new Exception("General Error --> " + exp.getMessage());
        }
    }

    // User and History Updates
    public void updateRegisteredUserAccount(User user) throws Exception {

        if (user == null)
            throw new NullArgumentException("This user has a null value");

        if (!isEmailAddressTaken(user.getEmail()))
            throw new IllegalArgumentException("This user account does not exist");

        try {
            // Updating user
            userRepository.save(user);

            // Updating History
            History history = historyRepository.findByUser(user.getEmail());

            history.setUser(user);

            historyRepository.save(history);
        } catch (PersistenceException exp){
            throw new PersistenceException("Persistence Error --> " + exp.getMessage());
        } catch (Exception exp){
            throw new Exception("General Error --> " + exp.getMessage());
        }
    }

    // Auxiliary Functions
    private boolean isEmailAddressTaken(String email){
        User user = userRepository.findByEmail(email);
        return (user != null);
    }
}

@@ -150,16 +150,50 @@
 roduct;%0A
+import com.evapps.Entity.Receipt;%0A
 import c
@@ -215,16 +215,16 @@
 y.User;%0A
-
 import c
@@ -303,32 +303,80 @@
 ductRepository;%0A
+import com.evapps.Repository.ReceiptRepository;%0A
 import com.evapp
@@ -819,32 +819,96 @@
 %0A    @Autowired%0A
+    private ReceiptRepository receiptRepository;%0A    @Autowired%0A
     private User
@@ -978,17 +978,17 @@
 oid upda
-r
+t
 eRegiste
@@ -1658,32 +1658,32 @@
 %0A%0A        try %7B%0A
-
             prod
@@ -1964,24 +1964,550 @@
    %7D%0A    %7D%0A%0A
+    // Receipt Updates%0A    public void updateRegisteredUserTransaction(Receipt receipt) throws Exception%7B%0A%0A        if (receipt == null)%0A            throw new NullArgumentException(%22This transaction is void%22);%0A%0A        try %7B%0A            receiptRepository.save(receipt);%0A        %7D catch (PersistenceException exp)%7B%0A            throw new PersistenceException(%22Persistence Error --%3E %22 + exp.getMessage());%0A        %7D catch (Exception exp)%7B%0A            throw new Exception(%22General Error --%3E %22 + exp.getMessage());%0A        %7D%0A    %7D%0A%0A
     // User 

Add updateRegisteredUserTransaction in UpdateDataService