Ejemplo n.º 1
0
        public void Create(Customer customer)
        {
            string sql = $"INSERT INTO CUSTOMER (ID, FIRSTNAME, LASTNAME, BANKID" +
                         $"VALUES ({customer.Id}, {customer.Firstname}, {customer.Lastname}, {customer.BankId})";

            _BankdbContext.Add(customer);
            _BankdbContext.SaveChanges();
        }
        public void Create(Transaction transaction)
        {
            string sql = $"INSERT INTO TRANSACTION (ID, IBAN, AMOUNT, TIMESTAMP" +
                         $"VALUES ({transaction.Id}, {transaction.IBAN}, {transaction.Amount}, {transaction.TimeStamp})";

            _BankdbContext.Add(transaction);
            _BankdbContext.SaveChanges();
        }
Ejemplo n.º 3
0
        public void Create(Bank bank)
        {
            string sql = $"INSERT INTO BANK (ID, NAME, BIC" +
                         $"VALUES ({bank.Id}, {bank.Name}, {bank.BIC})";

            _BankdbContext.Add(bank);
            _BankdbContext.SaveChanges();
        }
Ejemplo n.º 4
0
        public void Create(Account account)
        {
            string sql = $"INSERT INTO ACCOUNT (IBAN, NAME, BANKID, CUSTOMERID, BALANCE" +
                         $"VALUES ({account.IBAN}, {account.Name}, {account.BankId}, {account.CustomerId}, {account.Balance})";

            _BankdbContext.Add(account);
            _BankdbContext.SaveChanges();
        }
Ejemplo n.º 5
0
 //Add account to database, produce error if given
 public Account Create(Account newAccount)
 {
     try
     {
         _context.Account.Add(newAccount);
         _context.SaveChanges();
         return(newAccount);
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception.Message);
         return(null);
     }
 }
Ejemplo n.º 6
0
 //Add bank into database, produce error if given
 public Bank Create(Bank newBank)
 {
     try
     {
         _context.Bank.Add(newBank);
         _context.SaveChanges();
         return(newBank);
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception.Message);
         return(null);
     }
 }
Ejemplo n.º 7
0
 //Add customer to database, produce error if given
 public Customer Create(Customer newCustomer)
 {
     try
     {
         _context.Customer.Add(newCustomer);
         _context.SaveChanges();
         return(newCustomer);
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception.Message);
         return(null);
     }
 }
Ejemplo n.º 8
0
 public Bank Create(Bank newBank)
 {
     try
     {
         _bankdbContext.Bank.Add(newBank);
         _bankdbContext.SaveChanges();
         Console.WriteLine("Bank added successfully");
         return(newBank);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Bank creation failed" + ex.Message);
         return(null);
     }
 }
Ejemplo n.º 9
0
 public Customer Create(Customer newCustomer)
 {
     try
     {
         _bankdbContext.Customer.Add(newCustomer);
         _bankdbContext.SaveChanges();
         Console.WriteLine("Customer added successfully");
         return(newCustomer);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Customer creation failed" + ex.Message);
         return(null);
     }
 }
Ejemplo n.º 10
0
 public Account Create(Account newAccount)
 {
     try
     {
         _bankdbContext.Account.Add(newAccount);
         _bankdbContext.SaveChanges();
         Console.WriteLine("Account added successfully");
         return(newAccount);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Account creation failed" + ex.Message);
         return(null);
     }
 }
Ejemplo n.º 11
0
        //Update Customer's Account
        public Tuple <int, string, string> UpdateAccount(int custId, string iban, string accountName, decimal balance)
        {
            using (var db = new BankdbContext()) {
                try  {
                    var bankAccount = db.BankAccount
                                      .Where(ba => (ba.CustomerId == custId) && (ba.Iban == iban))
                                      .FirstOrDefault();

                    bankAccount.Name    = accountName;
                    bankAccount.Balance = balance; // >= 0 accepted
                    db.BankAccount.Update(bankAccount);

                    if (db.SaveChanges() > 0)
                    {
                        return(new Tuple <int, string, string>(1, "Bank account successfully updated!", "Success"));
                    }
                    else
                    {
                        return(new Tuple <int, string, string>(2, "Bank account not successfully updated!", "Failure"));
                    }
                } catch (Exception e) {
                    return(new Tuple <int, string, string>(3, "Error occurred in bank account updating operation!", "Failure"));
                }
            }
        }
Ejemplo n.º 12
0
        public static void DeleteBank(string Id)
        {
            int bankId;

            int.TryParse(Id, out bankId);

            if (bankId > 0)
            {
                try
                {
                    var context     = new BankdbContext();
                    var DeletedBank = context.Bank.Where(b => b.Id == bankId).FirstOrDefault();
                    context.Bank.Remove(DeletedBank);
                    context.SaveChanges();

                    Console.WriteLine("Bank Deleted.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                Console.WriteLine("Invalid Inputs");
            }
        }
Ejemplo n.º 13
0
        public static void UpdateBank(string Id, string NewName, string NewBic)
        {
            int bankId;

            int.TryParse(Id, out bankId);

            if (bankId > 0 && NewName.Length >= 2 && NewBic.Length >= 5 && NewName.Length <= 50 && NewBic.Length <= 10)
            {
                try
                {
                    var context     = new BankdbContext();
                    var UpdatedBank = context.Bank.Where(b => b.Id == bankId).FirstOrDefault();
                    UpdatedBank.Name = NewName;
                    UpdatedBank.Bic  = NewBic;
                    context.Bank.Update(UpdatedBank);
                    context.SaveChanges();

                    Console.WriteLine("Bank Updated.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                Console.WriteLine("Invalid Inputs");
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Adds a new transaction for the customer account and update account balance
        /// </summary>
        public static void MakeTransaction(string iban, float amount)
        {
            try
            {
                BankdbContext context = new BankdbContext();

                var account = context.BankAccount.Where(a => a.Iban == iban).SingleOrDefault();
                if (account != null)
                {
                    BankAccountTransaction newTransaction = new BankAccountTransaction
                    {
                        Iban      = iban,
                        Amount    = (decimal)amount,
                        TimeStamp = DateTime.Today
                    };

                    account.Balance += (decimal)amount;

                    context.BankAccount.Update(account);
                    context.BankAccountTransaction.Add(newTransaction);
                    context.SaveChanges();
                    Console.WriteLine($"Transaction of the amount of {amount} done for account {iban}. Current balance is: {account.Balance}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 15
0
        public static void UpdateCustomerData(string selectedUserId, string FirstName, string LastName)
        {
            int userId;

            int.TryParse(selectedUserId, out userId);

            if (userId > 0)
            {
                try
                {
                    var context         = new BankdbContext();
                    var UpdatedCustomer = context.Customer.Where(c => c.Id == userId).FirstOrDefault();
                    UpdatedCustomer.FirstName = FirstName;
                    UpdatedCustomer.LastName  = LastName;
                    context.Customer.Update(UpdatedCustomer);
                    context.SaveChanges();

                    Console.WriteLine("Customer Data Updated.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                Console.WriteLine("Invalid Inputs");
            }
        }
Ejemplo n.º 16
0
        public static void AddCustomerTransaction(string selectedAccount, List <string> IbanList, string amount)
        {
            int IbanSelector;

            int.TryParse(selectedAccount, out IbanSelector);
            decimal newAmount;

            decimal.TryParse(amount, out newAmount);


            string selectedIban = IbanList[IbanSelector - 1];

            try
            {
                var context        = new BankdbContext();
                var newTransaction = new Model.BankAccountTransaction
                {
                    Iban      = selectedIban,
                    Amount    = newAmount,
                    TimeStamp = DateTime.Now
                };

                context.BankAccountTransaction.Add(newTransaction);
                context.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 17
0
        public static void AddCustomerBankAccount(string NewIBAN, string NewName, int NewBankId, int NewCustomerId, string NewBalance)
        {
            decimal Balance;

            decimal.TryParse(NewBalance, out Balance);
            if (NewIBAN.Length >= 12 && NewName.Length >= 2 && Balance >= 0 && NewIBAN.Length <= 50 && NewName.Length <= 50)
            {
                try
                {
                    var context        = new BankdbContext();
                    var newBankAccount = new Model.BankAccount
                    {
                        Iban       = NewIBAN,
                        Name       = NewName,
                        BankId     = NewBankId,
                        CustomerId = NewCustomerId,
                        Balance    = Balance
                    };

                    context.BankAccount.Add(newBankAccount);
                    context.SaveChanges();

                    Console.WriteLine("User Bank Account added to database.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                Console.WriteLine("Invalid Inputs");
            }
        }
Ejemplo n.º 18
0
        public static List <int> AddCustomer(string NewFirstName, string NewLastName, string Id)
        {
            int SelectedBankId;

            int.TryParse(Id, out SelectedBankId);
            List <int> UserData = new List <int>();

            if (SelectedBankId > 0 && NewFirstName.Length >= 2 && NewLastName.Length >= 2 && NewFirstName.Length <= 50 && NewLastName.Length <= 50)
            {
                var context     = new BankdbContext();
                var newCustomer = new Model.Customer
                {
                    FirstName = NewFirstName,
                    LastName  = NewLastName,
                    BankId    = SelectedBankId
                };

                context.Customer.Add(newCustomer);
                context.SaveChanges();

                UserData.Add(newCustomer.BankId);
                UserData.Add(newCustomer.Id);

                Console.WriteLine("User Added to database.");

                return(UserData);
            }
            else
            {
                Console.WriteLine("Invalid Inputs");
                return(UserData);
            }
        }
Ejemplo n.º 19
0
        public static void DeleteCustomer(string selectedUserId)
        {
            int customerId;

            int.TryParse(selectedUserId, out customerId);

            if (customerId > 0)
            {
                try
                {
                    var context = new BankdbContext();

                    var DeletedUser = context.Customer.Where(c => c.Id == customerId).FirstOrDefault();
                    context.Customer.Remove(DeletedUser);
                    context.SaveChanges();

                    Console.WriteLine("Customer Deleted.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                Console.WriteLine("Invalid Inputs");
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Adds a new account for the customer
        /// </summary>
        public static void AddAccount(int customerId, int bankId, string iban, string accountName = null)
        {
            try
            {
                BankdbContext context = new BankdbContext();

                var  customer      = context.Customer.Where(c => c.Id == customerId).SingleOrDefault();
                var  bank          = context.Bank.Where(b => b.Id == bankId).SingleOrDefault();
                bool dublicateIban = context.BankAccount.Where(a => a.Iban == iban).Any();
                if (customer != null && bank != null && !dublicateIban)
                {
                    BankAccount newAccount = new BankAccount
                    {
                        Iban       = iban,
                        Name       = accountName,
                        BankId     = bankId, //Bank id should be derived from iban number
                        CustomerId = customerId,
                        Balance    = 0,
                    };

                    context.BankAccount.Add(newAccount);
                    context.SaveChanges();
                    Console.WriteLine($"Account {iban.Trim()} added for customer {customerId}.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException.Message);
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 1. etsitään oikea tili, jolle tilitapahtuma kirjataan
        /// 2. tilin saldo (Balance) laitetaan vastaaman oikeaa suoritusta +/- tapahtuma
        /// 3. päivitetään Account:n Balance
        /// 4. Tallennetaan muutokset sekä Transaction että Account tauluihin
        /// </summary>
        /// <param name="transaction"></param>
        /// <returns></returns>
        //public bool AddTransaction(Transaction transaction)
        //{
        //    bool res = false;
        //    using (var context = new BankdbContext())
        //    {
        //        try
        //        {
        //            context.Add(transaction);
        //            var account = GetAccountByIban(transaction.Iban);
        //            decimal balanceBeforeTransaction = account.Balance;
        //            var lastTransaction = GetTransactionsOfAccount(transaction.Iban);
        //            int result = DateTime.Compare(transaction.TimeStamp, lastTransaction.TimeStamp);
        //            if (result>0)
        //            {
        //                account.Balance += transaction.Amount;
        //            }
        //            if (account.Balance - transaction.Amount == balanceBeforeTransaction)
        //            {
        //                res = true;
        //            }

        //            //Update Account-table
        //            context.Account.Update(account);

        //            context.SaveChanges();
        //        }
        //        catch (Exception ex)
        //        {
        //            throw new NotImplementedException($"{ex.Message}\n{ex.InnerException.Message} \n");
        //        }
        //        return res;
        //    }
        //}
        /// <summary>
        /// 1. etsitään oikea tili, jolle tilitapahtuma kirjataan
        /// 2. tilin saldo (Balance) laitetaan vastaaman oikeaa suoritusta +/- tapahtuma
        /// 3. päivitetään Account:n Balance
        /// 4. Tallennetaan muutokset sekä Transaction että Account tauluihin
        /// </summary>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public void AddTransaction(Transaction transaction)
        {
            using (var context = new BankdbContext())
            {
                try
                {
                    //Lisätään tapahtumatauluun rivi
                    context.Add(transaction);

                    //Etsitään tili, jonka tietoja päivitetään
                    var account = GetAccountByIban(transaction.Iban);
                    //Lasketaan tilille uusi saldoa
                    account.Balance += transaction.Amount;

                    //Update Account-table
                    context.Account.Update(account);
                    //Tallennetaan muutokset tietokantaan
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new NotImplementedException($"{ex.Message}\n{ex.InnerException.Message} \n");
                }
            }
        }
Ejemplo n.º 22
0
 //Update Bank
 public bool UpdateBank(int id, string txtBankName, string txtBiccode)
 {
     using (var db = new BankdbContext()) {
         var bank = db.Bank.Where(b => b.Id == id).FirstOrDefault();
         bank.Name = txtBankName;
         bank.Bic  = txtBiccode;
         db.Bank.Update(bank);
         return(db.SaveChanges() > 0 ? true : false);
     }
 }
Ejemplo n.º 23
0
        public void DeleteCustomer(long id) //Delete henkilö
        {
            var delCustomer = _context.Customer.FirstOrDefault(c => c.Id == id);

            if (delCustomer != null)
            {
                _context.Customer.Remove(delCustomer);
            }
            _context.SaveChanges();
        }
Ejemplo n.º 24
0
        public void Delete(string iban)
        {
            var delAccount = _context.Account.FirstOrDefault(a => a.Iban == iban);

            if (delAccount != null)
            {
                _context.Account.Remove(delAccount);
            }
            _context.SaveChanges();
        }
Ejemplo n.º 25
0
        public void CreateTransaction(Transaction transaction)
        {
            _bankdbContext.Transaction.Add(transaction);
            var account = _accRepository.Read(transaction.Iban);

            account.Balance += transaction.Amount;

            _bankdbContext.Account.Update(account);
            _bankdbContext.SaveChanges();
        }
Ejemplo n.º 26
0
        public void CreateTransaction(Transaction transaction)
        {
            _bankdbContext.Transaction.Add(transaction);
            var account = _accountrepository.Read(transaction.Iban);

            account.Balance += transaction.Amount;

            _bankdbContext.Account.Update(account);
            _bankdbContext.SaveChanges();
            Console.WriteLine("Tilitapahtumien päivitys onnistui!");
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Removes all accounts that are linked to the given bank
        /// </summary>
        public static void RemoveAllAccounts(int bankId)
        {
            BankdbContext      context  = new BankdbContext();
            List <BankAccount> accounts = context.BankAccount.Where(a => a.BankId == bankId).ToList();

            foreach (BankAccount a in accounts)
            {
                AccountHandling.RemoveAccount(a.Iban);
            }
            context.SaveChanges();
        }
Ejemplo n.º 28
0
 //Create New Bank
 public Tuple <int, string, string> CreateBank(string bank, string bic)
 {
     using (var db = new BankdbContext()) {
         var newBank = new Bank()
         {
             Name = bank, Bic = bic
         };
         db.Bank.Add(newBank);
         db.SaveChanges();
         return(new Tuple <int, string, string>(newBank.Id, newBank.Name, newBank.Bic));
     }
 }
Ejemplo n.º 29
0
        /// <summary>
        /// Removes account from customer
        /// </summary>
        public static void RemoveAccount(string iban)
        {
            BankdbContext context       = new BankdbContext();
            BankAccount   removeAccount = context.BankAccount.Where(a => a.Iban == iban).FirstOrDefault();

            if (removeAccount != null)
            {
                // First remove all transactions that are linked to the account
                List <BankAccountTransaction> transactions = context.BankAccountTransaction.Where(t => t.Iban == iban).ToList();
                foreach (BankAccountTransaction t in transactions)
                {
                    context.BankAccountTransaction.Remove(t);
                }
                context.SaveChanges();

                decimal bal = removeAccount.Balance;
                context.BankAccount.Remove(removeAccount);
                context.SaveChanges();
                Console.WriteLine($"Account number {iban.Trim()} removed. {bal} amount of money was lost.");
            }
        }
Ejemplo n.º 30
0
 //Remove Bank
 public Tuple <int, string, string> RemoveBank(int id)
 {
     using (var db = new BankdbContext()) {
         using (var transaction = db.Database.BeginTransaction())  {
             try
             {
                 var customer = db.Customer.Where(c => c.BankId == id).FirstOrDefault();
                 if (customer != null)
                 {
                     db.Customer.Remove(customer);
                     db.SaveChanges();
                 }
                 var bankAccount = db.BankAccount.Where(ba => ba.BankId == id).FirstOrDefault();
                 if (bankAccount != null)
                 {
                     db.BankAccount.Remove(bankAccount);
                     db.SaveChanges();
                 }
                 var bank = db.Bank.Where(b => b.Id == id).FirstOrDefault();
                 db.Bank.Remove(bank);
                 int _b = db.SaveChanges();
                 if (_b > 0)
                 {
                     transaction.Commit();
                     return(new Tuple <int, string, string>(1, "Bank removed successfully", "Success"));
                 }
                 else
                 {
                     transaction.Rollback();
                     return(new Tuple <int, string, string>(2, "Bank not removed successfully", "Failure"));
                 }
             }
             catch (Exception e)
             {
                 transaction.Rollback();
                 return(new Tuple <int, string, string>(3, "Error occurred in deletion operation!", "Failure"));
             }
         }
     }
 }