Example #1
0
        public async Task <uint> SaveTransaction(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 TransactionOutlay()
            {
                Amount             = transaction.Amount,
                DateTransaction    = transaction.Date.ToString(DATE_FORMAT),
                Description        = transaction.Description,
                CodeTransaction_ID = transaction.CodeTransactionID,
                TypeTransaction_ID = transaction.TypeTransactionID
            };

            await _contextDB.TransactionOutlay.AddAsync(newTransaction);

            await _contextDB.Context.SaveChangesAsync();

            //load by ref the ID given by the DB
            transaction.ID = (uint)newTransaction.ID;

            ClearCacheTransactions();

            return(transaction.ID);
        }
Example #2
0
        public async Task <TransactionDTO> UpdateTransaction(TransactionDTO transaction)
        {
            TransactionOutlay transactionToUpdate = (from TransactionOutlay t
                                                     in _contextDB.TransactionOutlay
                                                     .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.DateTransaction    = transaction.Date.ToString(DATE_FORMAT);
            transactionToUpdate.CodeTransaction_ID = transaction.CodeTransactionID;
            transactionToUpdate.Description        = transaction.Description;
            transactionToUpdate.TypeTransaction_ID = transaction.TypeTransactionID;

            await _contextDB.Context.SaveChangesAsync();

            ClearCacheTransactions();

            return(transactionToUpdate.ToTransactionDTO());
        }
Example #3
0
 public static TransactionDTO ToTransactionDTO(this TransactionOutlay entityTransaction)
 {
     return((entityTransaction == null)
                     ? new TransactionDTO()
                     : new TransactionDTO()
     {
         ID = (uint)entityTransaction.ID,
         Amount = entityTransaction.Amount,
         Date = entityTransaction.DateTransaction.ToDateTime(),
         CodeTransaction = entityTransaction.CodeTransaction?.Code,
         CodeTransactionID = entityTransaction.CodeTransaction_ID,
         TypeTransaction = entityTransaction.TypeTransaction?.Type,
         TypeTransactionID = entityTransaction.TypeTransaction_ID,
         Description = entityTransaction.Description
     });
 }
Example #4
0
        public async Task <uint> DeleteTransaction(uint transactionID)
        {
            TransactionOutlay transactionToDelete = await(from TransactionOutlay t
                                                          in _contextDB.TransactionOutlay
                                                          where t.ID == transactionID
                                                          select t)
                                                    .SingleOrDefaultAsync() ?? throw new Exception($"[{nameof(DeleteTransaction)}]: Transaction ID:{transactionID} not found");


            _contextDB.TransactionOutlay.Remove(transactionToDelete);
            await _contextDB.Context.SaveChangesAsync();

            ClearCacheTransactions();

            return((uint)transactionToDelete.ID);
        }