public async Task <Unit> Handle(ImportPaymentsCommand request, CancellationToken cancellationToken)
        {
            var importResult = _importReport.Import(request.Report);

            foreach (var paymentInfo in importResult.ValidPayments)
            {
                if (paymentInfo.PaymentId == null)
                {
                    throw new InvalidOperationException("Payment has no generated id. Parsing report has been done with problems.");
                }

                //TODO we need to improve details saving
                var details = await _dBContext.Details.FindAsync(paymentInfo.Details) ?? new Details()
                {
                    FullDetails = paymentInfo.Details
                };

                if (paymentInfo.Income)
                {
                    await CreateOrUpdateIncome(cancellationToken, paymentInfo, details);
                }
                else
                {
                    await CreateOrUpdatePayment(cancellationToken, paymentInfo, details);
                }
            }

            var savedPayments = await _dBContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

            //TODO send notification that we have new payments
            //TODO send notification about problematic report lines
            return(Unit.Value);
        }
        public async Task <Unit> Handle(EditDetailsRequest request, CancellationToken cancellationToken)
        {
            var requestDetails  = request.Details;
            var categoryChanged = !string.Equals(request.NewDefaultCategoryName, requestDetails.DefaultCategory?.Name,
                                                 StringComparison.InvariantCultureIgnoreCase);

            if (categoryChanged)
            {
                var category = _accountingDbContext.Categories.FirstOrDefault(
                    c => c.Name.Equals(request.NewDefaultCategoryName, StringComparison.InvariantCultureIgnoreCase));

                if (category == null)
                {
                    category = await CreateNewCategoryWithName(request, cancellationToken);
                }

                requestDetails.DefaultCategory = category;

                UpdateCategoryByDefaultCategoryInDetails(requestDetails);
            }

            _accountingDbContext.Details.Update(requestDetails);
            await _accountingDbContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Category> Handle(NewCategoryRequest request, CancellationToken cancellationToken)
        {
            var category = _accountingDbContext.Add(new Category
            {
                ParentCategory = request.ParentCategory,
                Name           = request.Name
            });

            var id = await _accountingDbContext.SaveChangesAsync(cancellationToken);

            return(category.Entity);
        }
Exemple #4
0
        public async Task <Unit> Handle(SetCategoryCommand request, CancellationToken cancellationToken)
        {
            if (request.ApplyToAllWithSuchDetails)
            {
                var payments = await _accountingDbContext.Payments.Where(payment => payment.Details == request.Payment.Details)
                               .ToArrayAsync(cancellationToken: cancellationToken);

                foreach (var payment in payments)
                {
                    payment.Category = request.Category;
                }
            }
            else
            {
                request.Payment.Category = request.Category;
            }

            await _accountingDbContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }