Example #1
0
        public void CalculateCostsMovie_ValidMembership_returnsValue()
        {
            var calculatedCosts = new CalculateCosts();

            //Arrange
            var result = calculatedCosts.CalculateCostsMovie(
                new MembershipType {
                Description = "Quarterly", DiscountPct = 20, Months = 3
            },
                new Movie {
                Price = 1, Title = "Test movie"
            });

            var expected  = Decimal.Round((Decimal)0.8, 2);
            var resultDec = Decimal.Round((Decimal)result, 2);

            try
            {
                Assert.AreEqual(expected, resultDec);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public IHttpActionResult CreateRentals(RentalDto rentalDto)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == rentalDto.CustomerId);

            if (customer == null)
            {
                return(BadRequest("There are not customers for the provided Id"));
            }

            if (rentalDto.MovieIds.Count == 0)
            {
                return(BadRequest("The provided list of movies is empty"));
            }

            var movies = _context.Movies.Where(m => rentalDto.MovieIds.Contains(m.Id)).ToList();

            if (movies.Count != rentalDto.MovieIds.Count)
            {
                BadRequest("Some movie Id's provided are incorrect or there are not movies for that movie id");
            }

            var membershipType = _context.MembershipTypes.Single(m => m.Id == customer.MembershipTypeId);

            if (membershipType == null)
            {
                BadRequest("The membership type is incorrect for the customer provided");
            }

            var calculations = new CalculateCosts();

            foreach (var movie in movies)
            {
                if (movie.AvailableForRental == 0)
                {
                    return(BadRequest("The movie is not available for renting"));
                }

                movie.AvailableForRental--;
                var rental = new Rental
                {
                    Movie              = movie,
                    Customer           = customer,
                    RentedDate         = DateTime.Now,
                    TotalAfterDiscount = calculations.CalculateCostsMovie(membershipType, movie)
                };
                _context.Rentals.Add(rental);
                _context.SaveChanges();
            }

            return(Ok());
        }