예제 #1
0
        public async Task <Unit> Handle(EditTransactionCommand command, CancellationToken token)
        {
            var category = await _db.Categories
                           .Where(x => x.Id == command.CategoryId)
                           .SingleOrDefaultAsync(token);

            if (category == null)
            {
                throw NotFoundException.Create <Category>(x => x.Id, command.CategoryId);
            }

            var tags = new List <Tag>();

            if (command.TagIds != null && command.TagIds.Any())
            {
                var ids = command.TagIds.ToList();
                tags = await _db.Tags
                       .Where(x => ids.Contains(x.Id))
                       .ToListAsync(token);

                if (command.TagIds.Count != tags.Count)
                {
                    throw NotFoundException.Create <Tag>(x => x.Id,
                                                         string.Join(", ", command.TagIds));
                }
            }

            var sourceAccount = await _db.Accounts
                                .Where(x => x.Id == command.SourceAccountId)
                                .SingleOrDefaultAsync(token);

            if (sourceAccount == null)
            {
                throw NotFoundException.Create <Account>(x => x.Id, command.SourceAccountId);
            }

            var destinationAccount = await _db.Accounts
                                     .Where(x => x.Id == command.DestinationAccountId)
                                     .SingleOrDefaultAsync(token);

            if (destinationAccount == null)
            {
                throw NotFoundException.Create <Account>(x => x.Id, command.DestinationAccountId);
            }

            var transaction = new Transaction(command.Title, command.Date, command.Description, category, command.Amount, sourceAccount, destinationAccount, tags);

            _db.Transactions.Attach(transaction);

            await _db.SaveChangesAsync(token);

            return(default);
예제 #2
0
        public async Task <TransactionModel> Handle(GetTransactionQuery query, CancellationToken token)
        {
            var transaction = await _db.Transactions
                              .Specify(new TransactionDetails())
                              .SingleOrDefaultAsync(x => x.Id == query.Id,
                                                    cancellationToken: token);

            if (transaction == null)
            {
                throw NotFoundException.Create <Tag>(x => x.Id, query.Id);
            }

            return(new TransactionModel(transaction));
        }
예제 #3
0
        public async Task <Unit> Handle(DeleteTransactionCommand command, CancellationToken token)
        {
            var transaction = await _db.Transactions
                              .SingleOrDefaultAsync(x =>
                                                    x.Id == command.Id,
                                                    token);

            if (transaction == null)
            {
                throw NotFoundException.Create <Transaction>(x => x.Id, command.Id);
            }

            _db.Remove(transaction);
            await _db.SaveChangesAsync(token);

            return(default);