public async Task <Unit> Handle(UpsertTransactionsCommand request, CancellationToken cancellationToken) { var mappedTransactions = request.Transactions.Select(x => new Transaction { Id = x.Id, Status = _enumMapper.MapEnum <Domain.Enums.TransactionStatus, TransactionStatus>(x.Status), Type = _enumMapper.MapEnum <Domain.Enums.TransactionType, TransactionType>(x.Type), ClientName = x.ClientName, Amount = x.Amount }); var toInsert = mappedTransactions.Where(x => !_context.Transactions.Any(entity => entity.Id == x.Id)).ToList(); var toUpdate = mappedTransactions.Where(x => !toInsert.Any(i => i.Id == x.Id)).ToList(); if (toInsert.Any()) { await _context.Transactions.AddRangeAsync(toInsert); } if (toUpdate.Any()) { _context.Transactions.UpdateRange(toUpdate); } await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeleteTransactionCommand request, CancellationToken cancellationToken) { var transaction = await _context.Transactions.FindAsync(request.Id); if (transaction == null) { throw new NotFoundException($"Transaction with Id {request.Id} not found."); } _context.Transactions.Remove(transaction); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(UpdateTransactionCommand request, CancellationToken cancellationToken) { var transaction = await _context.Transactions.FindAsync(request.Id); if (transaction == null) { throw new NotFoundException($"Transaction with Id {request.Id} not found."); } var mappedStatus = _enumMapper.MapEnum <Domain.Enums.TransactionStatus, TransactionStatus>(request.Status); if (transaction.Status != mappedStatus) { transaction.Status = mappedStatus; await _context.SaveChangesAsync(cancellationToken); } return(Unit.Value); }