Пример #1
0
        /// <summary>
        /// Add Sale
        /// </summary>
        /// <param name="sake">Sale object</param>
        /// <returns>Result object</returns>
        public BLResult <Sale> AddSale(Sale sale)
        {
            DBResult <Inventory> inventoryResult;
            DBResult <Sale>      saleResult;
            DBResult <Movie>     movieResult;
            BLResult <Sale>      result = new BLResult <Sale>();


            // Validating input data
            if (sale == null)
            {
                result.Message = "Invalid data";
                return(result);
            }

            // Validating inventory
            inventoryResult = inventoryDataAccess.GetInventoryById(sale.IdInventory);
            if (!inventoryResult.Success)
            {
                result.Message = "Invalid inventory";
                return(result);
            }
            // Getting current sale price
            movieResult = movieDataAccess.GetMovieById(inventoryResult.Item.IdMovie);
            if (!movieResult.Success)
            {
                result.Message = $"Error getting price from Movie: {movieResult.Message}";
                return(result);
            }

            if (inventoryResult.Item.IsNew == false || inventoryResult.Item.IsAvailable == false)
            {
                result.Message = "Inventory not found or not available";
                return(result);
            }

            sale.SalePrice = movieResult.Item.SalePrice;
            sale.SaleDate  = DateTime.Now;
            // Adding sale
            saleResult = saleDataAccess.CreateSale(sale);
            if (saleResult.Success)
            {
                result.Success = true;
                result.Item    = sale;

                // If the operation is successful the item will no longer be available
                inventoryResult = inventoryDataAccess.SetAvailability(sale.IdInventory, false);
                if (!inventoryResult.Success)
                {
                    result.Message = inventoryResult.Message;
                }
                result.Success = true;
            }
            else
            {
                result.Message = saleResult.Message;
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Add Rental
        /// </summary>
        /// <param name="sake">Rental object</param>
        /// <returns>Result object</returns>
        public BLResult <Rental> AddRental(Rental rental)
        {
            DBResult <Inventory> inventoryResult;
            DBResult <Rental>    rentalResult;
            DBResult <Movie>     movieResult;
            BLResult <Rental>    result = new BLResult <Rental>();


            // Validating input data
            if (rental == null)
            {
                result.Message = "Invalid data";
                return(result);
            }

            // Validating inventory
            inventoryResult = inventoryDataAccess.GetInventoryById(rental.IdInventory);
            if (!inventoryResult.Success)
            {
                result.Message = "Invalid inventory";
                return(result);
            }
            // Getting current rental price
            movieResult = movieDataAccess.GetMovieById(inventoryResult.Item.IdMovie);
            if (!movieResult.Success)
            {
                result.Message = $"Error getting price from Movie: {movieResult.Message}";
                return(result);
            }

            if (inventoryResult.Item.IsNew == true || inventoryResult.Item.IsAvailable == false)
            {
                result.Message = "Inventory not found or not available for rent";
                return(result);
            }

            rental.RentalPrice = movieResult.Item.RentalPrice;
            rental.RentalDate  = DateTime.Now;
            rental.ReturnDate  = DateTime.Now.AddDays(7);
            // Adding Rental
            rentalResult = rentalDataAccess.CreateRental(rental);
            if (rentalResult.Success)
            {
                result.Success = true;
                result.Item    = rental;

                // If the operation is successful the item will no longer be available
                inventoryResult = inventoryDataAccess.SetAvailability(rental.IdInventory, false);
                if (!inventoryResult.Success)
                {
                    result.Message = inventoryResult.Message;
                }
                result.Success = true;
            }
            else
            {
                result.Message = rentalResult.Message;
            }

            return(result);
        }