コード例 #1
0
ファイル: InvoiceTest.cs プロジェクト: arubesu/TDD-practice
        public void ShouldBePaidIfHasTotalPayment()
        {
            invoice.AddPayment(new Payment(500));
            var actual = invoice.TotalPaid;

            actual.Should().BeTrue();
        }
コード例 #2
0
        public async Task <Invoice> AddPayment(Payment payment, User user)
        {
            int month = 0;
            int year  = 0;

            //Try to get month and year from charge event
            if (payment.Date != default(DateTime))
            {
                month = payment.Date.Month;
                year  = payment.Date.Year;
            }
            Invoice foundInvoice = await _invoiceRepository.FindAsync(x =>
                                                                      x.User.Id == user.Id &&
                                                                      x.Month == month &&
                                                                      x.Year == year &&
                                                                      x.Currency == payment.Currency);

            //If no invoice exists for this user/month/year combination
            //create a new one
            if (foundInvoice == null)
            {
                Invoice newInvoice = new Invoice(
                    month,
                    year,
                    payment.Currency,
                    user
                    );

                newInvoice.AddPayment(payment);

                if (newInvoice.IsValid())
                {
                    return(_invoiceRepository.Insert(newInvoice));
                }
                else
                {
                    throw new InvalidEntityException(newInvoice);
                }
            }
            else
            {
                //If invoice was found, add charge
                foundInvoice.AddPayment(payment);
                if (foundInvoice.IsValid())
                {
                    return(_invoiceRepository.Update(foundInvoice));
                }
                else
                {
                    throw new InvalidEntityException(foundInvoice);
                }
            }
        }