Ejemplo n.º 1
0
            public async void ShouldReturnOkWithProjectItems()
            {
                IEnumerable <Project> expectedProjects;

                using (var context = new SimpleProjectTimeTrackerDbContext(Options))
                {
                    context.Projects.Add(new Project
                    {
                        Id           = 1,
                        CustomerName = "Microsoft",
                        Name         = "Sample web project for BUILD event",
                        DueDate      = new DateTime(2018, 7, 31),
                        HourlyRate   = 45
                    });
                    context.Projects.Add(new Project
                    {
                        Id           = 2,
                        CustomerName = "Amazon",
                        Name         = "Creation of a Picking List application",
                        DueDate      = new DateTime(2018, 9, 30),
                        HourlyRate   = 47
                    });
                    await context.SaveChangesAsync();

                    expectedProjects = await context.Projects.ToListAsync();
                }

                var result = await Sut.GetProjects(CancellationToken.None);

                var okResult      = Assert.IsType <OkObjectResult>(result);
                var okResultValue = Assert.IsAssignableFrom <IEnumerable <Project> >(okResult.Value);

                Assert.Equal(2, okResultValue.Count());
            }
Ejemplo n.º 2
0
        public async Task <TimeRegistration> CreateAsync(TimeRegistration timeRegistration, CancellationToken cancellationToken)
        {
            var timeRegistrationEntity = _mapper.Map <TimeRegistrationEntity>(timeRegistration);

            var timeRegistrationEntities = await _dbContext
                                           .TimeRegistrations
                                           .AsNoTracking()
                                           .ToListAsync();

            if (timeRegistrationEntities.Count >= 30)
            {
                throw new Exception("This is a demo application, you can add no more than 30 registrations.");
            }

            timeRegistrationEntity.Project = null;

            var newTimeRegistration = await _dbContext.TimeRegistrations.AddAsync(timeRegistrationEntity, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(Mapper.Map <TimeRegistration>(newTimeRegistration.Entity));
        }
Ejemplo n.º 3
0
        public async Task <IEnumerable <Invoice> > CreateAsync(CancellationToken cancellationToken)
        {
            var timeRegistrations = await _dbContext
                                    .TimeRegistrations
                                    .Include("Project")
                                    .Where(t => !t.Accounted)
                                    .ToListAsync();

            var timeRegistrationCustomers = from tr in timeRegistrations
                                            group tr by new
            {
                tr.Project.CustomerName,
                tr.Project.VatPercentage
            } into CustomerRegistrations
                select new
            {
                CustomerRegistrations.Key.CustomerName,
                CustomerRegistrations.Key.VatPercentage
            };

            var invoices = new List <Invoice>();

            foreach (var customer in timeRegistrationCustomers)
            {
                var invoiceDetails = from tr in timeRegistrations
                                     where tr.Project.CustomerName == customer.CustomerName
                                     orderby tr.ProjectId
                                     select new InvoiceDetailEntity
                {
                    ProjectName        = tr.Project.Name,
                    HourlyRate         = tr.Project.HourlyRate,
                    Date               = tr.Date,
                    HoursWorked        = tr.HoursWorked,
                    Amount             = Math.Round(tr.HoursWorked * tr.Project.HourlyRate),
                    TimeRegistrationId = tr.Id
                };

                var timeRegistrationsToUpdate = from tr in timeRegistrations
                                                where tr.Project.CustomerName == customer.CustomerName
                                                select tr;

                foreach (var timeRegistrationToUpdate in timeRegistrationsToUpdate)
                {
                    timeRegistrationToUpdate.Accounted = true;
                }

                var totalAmount = invoiceDetails
                                  .Sum(p => p.Amount);

                var invoice = new InvoiceEntity
                {
                    CustomerName  = customer.CustomerName,
                    Date          = DateTime.Now.Date,
                    Details       = invoiceDetails.ToList(),
                    VatPercentage = customer.VatPercentage,
                    NetAmount     = totalAmount
                };

                _dbContext.Invoices.Add(invoice);

                await _dbContext.SaveChangesAsync(CancellationToken.None);

                var createdInvoice = _mapper.Map <Invoice>(invoice);
                invoices.Add(createdInvoice);
            }

            return(invoices);
        }