Exemplo n.º 1
0
        public async Task ReturnAsync(int rentId, decimal penalty, ReturnedOnTime returnedOnTime)
        {
            var rent = await this.context.Rents
                       .SingleOrDefaultAsync(x => x.Id == rentId && x.RentStatus == RentStatus.Rented);

            if (rent == null)
            {
                return;
            }

            rent.Penalty        = penalty;
            rent.ReturnedOnTime = returnedOnTime;
            rent.RentStatus     = RentStatus.Returned;

            this.context.Update(rent);

            var rentProducts = await this.context.RentProducts
                               .Where(x => x.RentId == rentId)
                               .Include(x => x.Product)
                               .ToArrayAsync();

            foreach (var rentProduct in rentProducts)
            {
                var product = rentProduct.Product;
                product.Quantity += 1;
                product.IsRented  = false;

                this.context.Update(product);
            }

            await this.context.SaveChangesAsync();

            await this.invoicesService.CreateRentInvoiceAsync(rent);
        }
        public async Task ReturnAsyncShouldChangeRentedStatusToReturned(int productQuantity, bool isRented, RentStatus rentStatus, decimal penalty, ReturnedOnTime returnedOnTime, RentStatus expectedStatus, bool expextedIsRented, int expectedProductQuantity, decimal expectedPenalty)
        {
            var options = new DbContextOptionsBuilder <PhotoparallelDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            var dbContext = new PhotoparallelDbContext(options);

            var rent = new Rent {
                RentStatus = rentStatus
            };

            dbContext.Rents.Add(rent);

            var productService  = new Mock <IProductsService>();
            var invoicesService = new Mock <IInvoicesService>();
            var userService     = new Mock <IUsersService>();

            var product = new Product {
                Name = "Canon M50", Quantity = productQuantity, IsRented = isRented
            };

            dbContext.Products.Add(product);

            var rentProduct = new RentProduct {
                Product = product, Quantity = 1
            };

            rent.Products.Add(rentProduct);
            await dbContext.SaveChangesAsync();

            var rentsService = new RentsService(dbContext, userService.Object, invoicesService.Object, productService.Object);

            await rentsService.ReturnAsync(rent.Id, penalty, returnedOnTime);

            Assert.Equal(expectedStatus, rent.RentStatus);
            Assert.Equal(expextedIsRented, product.IsRented);
            Assert.Equal(expectedProductQuantity, product.Quantity);
            Assert.Equal(expectedPenalty, rent.Penalty);
        }