/// <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); }
/// <summary> /// Add new inventory /// </summary> /// <param name="liked">Liked object</param> /// <returns>Result object</returns> public BLResult <Inventory> AddInventory(Inventory inventory) { DBResult <Inventory> dbResult; DBResult <Movie> movieResult; BLResult <Inventory> result = new BLResult <Inventory>(); // Validating input data if (inventory == null) { result.Message = "Invalid data"; return(result); } // Validating movie movieResult = movieDataAccess.GetMovieById(inventory.IdMovie); if (!movieResult.Success) { result.Message = "Invalid movie"; return(result); } // Adding Inventory dbResult = inventoryDataAccess.CreateInventory(inventory); if (dbResult.Success) { result.Success = true; result.Item = inventory; // Increment Stock counter movieResult = movieDataAccess.IncrementStockCounter(inventory.IdMovie, 1); if (!movieResult.Success) { result.Success = false; result.Message = movieResult.Message; } } else { result.Message = dbResult.Message; } return(result); }
/// <summary> /// Get one movie /// </summary> /// <param name="movie">Id movie</param> /// <returns>Result object</returns> public BLResult <Movie> GetMovieById(int idMovie) { DBResult <Movie> dbResult; BLResult <Movie> result = new BLResult <Movie>(); // Try to get movie dbResult = movieDataAccess.GetMovieById(idMovie); if (dbResult.Success) { result.Success = true; result.Item = dbResult.Item; } else { result.Message = dbResult.Message; } return(result); }
/// <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); }