public void UpdateOrAddTransaction(Transaction transaction)
        {
            bool editMode = _dbContext.Transactions.Any(t => t.Id == transaction.Id);

            if (editMode)
            {
                _dbContext.Update(transaction);
            }
            else
            {
                _dbContext.Add(transaction);
            }

            _dbContext.SaveChanges();
        }
Beispiel #2
0
        public (bool success, string error) Write(List <Transaction> transactions)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                foreach (var blockData in transactions)
                {
                    try
                    {
                        var currentData = _context.Transactions.FirstOrDefault(d =>
                                                                               d.Index == blockData.Index &&
                                                                               d.Location == blockData.Location &&
                                                                               !d.Stale);

                        if (currentData != null)
                        {
                            currentData.Stale = true;
                            _context.Update(currentData);
                        }

                        blockData.DateCreated = DateTimeOffset.Now;
                        blockData.Stale       = false;

                        _context.Add(blockData);

                        _context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        return(false, e.InnerException.Message);
                    }
                }

                transaction.Commit();



                return(true, null);
            }
        }