public void SaveTransaction(Model.Transaction.TransactionDTO transaction) { if (transaction.CodeTransactionID <= 0) { throw new Exception($"[{nameof(SaveTransaction)}] {nameof(TransactionDTO.CodeTransactionID)} is mandatory"); } if (transaction.TypeTransactionID <= 0) { throw new Exception($"[{nameof(SaveTransaction)}] {nameof(TransactionDTO.TypeTransactionID)} is mandatory"); } var newTransaction = new EntityModel.Transaction() { Amount = transaction.Amount, Date = transaction.Date.ToString(DATE_FORMAT), Description = transaction.Description, CodeTransaction_ID = transaction.CodeTransactionID, TypeTransaction_ID = transaction.TypeTransactionID }; contextDB.Transaction.Add(newTransaction); contextDB.SaveChanges(); transaction.ID = (uint)newTransaction.ID; }
public void DeleteTransaction(uint transactionID) { EntityModel.Transaction transactionToDelete = (from EntityModel.Transaction t in contextDB.Transaction where t.ID == transactionID select t) .SingleOrDefault() ?? throw new Exception($"[{nameof(DeleteTransaction)}]: Transaction ID:{transactionID} not found"); contextDB.Transaction.Remove(transactionToDelete); contextDB.SaveChanges(); }
public void UpdateTransaction(TransactionDTO transaction) { EntityModel.Transaction transactionToUpdate = (from EntityModel.Transaction t in contextDB.Transaction .Include(x => x.TypeTransaction) .Include(x => x.CodeTransaction) where t.ID == transaction.ID select t).SingleOrDefault(); if (transactionToUpdate == null) { throw new Exception($"[{nameof(UpdateTransaction)}] Requested transaction not exist in DB. ID:{transaction.ID}"); } transactionToUpdate.Amount = transaction.Amount; transactionToUpdate.Date = transaction.Date.ToString(DATE_FORMAT); transactionToUpdate.CodeTransaction_ID = transaction.CodeTransactionID; transactionToUpdate.Description = transaction.Description; transactionToUpdate.TypeTransaction_ID = transaction.TypeTransactionID; contextDB.SaveChanges(); }