<commit_before>using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OnlineWallet.Web.DataLayer;
using OnlineWallet.Web.Modules.TransactionModule.Models;

namespace OnlineWallet.Web.Modules.TransactionModule.Commands
{
    public class BatchSaveCommand : IBatchSaveCommand
    {
        #region Fields

        private readonly IWalletDbContext _db;

        #endregion

        #region  Constructors

        public BatchSaveCommand(IWalletDbContext db, IReadOnlyCollection<IBatchSaveEvent> events)
        {
            _db = db;
            Events = events;
        }

        #endregion

        #region Properties

        public IReadOnlyCollection<IBatchSaveEvent> Events { get; }

        #endregion

        #region  Public Methods

        public async Task Execute(TransactionOperationBatch model, CancellationToken token)
        {
            var existingIds = model.Save.Where(e => e.TransactionId > 0).Select(e => e.TransactionId).ToList();
            existingIds.AddRange(model.Delete);
            var existingEntities = _db.Transactions.Where(e => existingIds.Contains(e.TransactionId)).ToList();
            var operations = new List<TransactionOperation>();
            foreach (var operation in model.Save)
            {
                token.ThrowIfCancellationRequested();
                operation.CreatedAt = operation.CreatedAt.Date;
                if (operation.TransactionId != 0)
                {
                    var existingEntity = existingEntities.Find(e => e.TransactionId == operation.TransactionId);

                    operations.Add(new TransactionOperation(existingEntity, operation, BatchSaveOperationType.Update));
                }
                else
                {
                    operations.Add(new TransactionOperation(null, operation, BatchSaveOperationType.New));
                }
            }

            foreach (var id in model.Delete)
            {
                token.ThrowIfCancellationRequested();
                var existingEntity = existingEntities.Find(e => e.TransactionId == id);
                if (existingEntity != null)
                {
                    operations.Add(new TransactionOperation(existingEntity, null, BatchSaveOperationType.Delete));
                }
            }

            var args = new TransactionEventArgs(operations);
            foreach (var transactionEvent in Events)
            {
                token.ThrowIfCancellationRequested();
                transactionEvent.BeforeSave(args, token);
            }

            foreach (var operation in operations)
            {
                token.ThrowIfCancellationRequested();
                switch (operation.OperationType)
                {
                    case BatchSaveOperationType.Update:
                        //Too slow. Need to be replaced with a custom solution. (And should make multi threaded)
                        _db.UpdateEntityValues(operation.OldTransaction, operation.NewTransaction);
                        break;
                    case BatchSaveOperationType.New:
                        await _db.Transactions.AddAsync(operation.NewTransaction, token);
                        break;
                    case BatchSaveOperationType.Delete:
                        _db.Transactions.Remove(operation.OldTransaction);
                        break;
                }
            }

            await _db.SaveChangesAsync(token);

            foreach (var transactionEvent in Events)
            {
                token.ThrowIfCancellationRequested();
                transactionEvent.AfterSave(args, token);
            }
        }

        #endregion
    }


    public interface IBatchSaveCommand
    {
        #region  Public Methods

        Task Execute(TransactionOperationBatch model, CancellationToken token);

        #endregion
    }
}<commit_msg>Added missing awaits.<commit_after>using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OnlineWallet.Web.DataLayer;
using OnlineWallet.Web.Modules.TransactionModule.Models;

namespace OnlineWallet.Web.Modules.TransactionModule.Commands
{
    public class BatchSaveCommand : IBatchSaveCommand
    {
        #region Fields

        private readonly IWalletDbContext _db;

        #endregion

        #region  Constructors

        public BatchSaveCommand(IWalletDbContext db, IReadOnlyCollection<IBatchSaveEvent> events)
        {
            _db = db;
            Events = events;
        }

        #endregion

        #region Properties

        public IReadOnlyCollection<IBatchSaveEvent> Events { get; }

        #endregion

        #region  Public Methods

        public async Task Execute(TransactionOperationBatch model, CancellationToken token)
        {
            var existingIds = model.Save.Where(e => e.TransactionId > 0).Select(e => e.TransactionId).ToList();
            existingIds.AddRange(model.Delete);
            var existingEntities = _db.Transactions.Where(e => existingIds.Contains(e.TransactionId)).ToList();
            var operations = new List<TransactionOperation>();
            foreach (var operation in model.Save)
            {
                token.ThrowIfCancellationRequested();
                operation.CreatedAt = operation.CreatedAt.Date;
                if (operation.TransactionId != 0)
                {
                    var existingEntity = existingEntities.Find(e => e.TransactionId == operation.TransactionId);

                    operations.Add(new TransactionOperation(existingEntity, operation, BatchSaveOperationType.Update));
                }
                else
                {
                    operations.Add(new TransactionOperation(null, operation, BatchSaveOperationType.New));
                }
            }

            foreach (var id in model.Delete)
            {
                token.ThrowIfCancellationRequested();
                var existingEntity = existingEntities.Find(e => e.TransactionId == id);
                if (existingEntity != null)
                {
                    operations.Add(new TransactionOperation(existingEntity, null, BatchSaveOperationType.Delete));
                }
            }

            var args = new TransactionEventArgs(operations);
            foreach (var transactionEvent in Events)
            {
                token.ThrowIfCancellationRequested();
                await transactionEvent.BeforeSave(args, token);
            }

            foreach (var operation in operations)
            {
                token.ThrowIfCancellationRequested();
                switch (operation.OperationType)
                {
                    case BatchSaveOperationType.Update:
                        //Too slow. Need to be replaced with a custom solution. (And should make multi threaded)
                        _db.UpdateEntityValues(operation.OldTransaction, operation.NewTransaction);
                        break;
                    case BatchSaveOperationType.New:
                        await _db.Transactions.AddAsync(operation.NewTransaction, token);
                        break;
                    case BatchSaveOperationType.Delete:
                        _db.Transactions.Remove(operation.OldTransaction);
                        break;
                }
            }

            await _db.SaveChangesAsync(token);

            foreach (var transactionEvent in Events)
            {
                token.ThrowIfCancellationRequested();
                await transactionEvent.AfterSave(args, token);
            }
        }

        #endregion
    }


    public interface IBatchSaveCommand
    {
        #region  Public Methods

        Task Execute(TransactionOperationBatch model, CancellationToken token);

        #endregion
    }
}
{'fraction_non_alphanumeric': 0.07173802242609582, 'fraction_numerical': 0.0005096839959225281, 'mean_word_length': 1.7463261021693492, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7324620', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1750, 'n_words': 374}