/// <summary> /// Maps a movie to a movie model. /// </summary> /// <param name="movie">The movie being mapped to a movie model.</param> /// <returns>A new movie model with all of the relevant properties of the movie.</returns> public static MovieModel MapToMovieModel(Movie movie) { // Create a new movie model. MovieModel movieModel = CreateNewModel <MovieModel>(); // Map all the common properties to the movie model. BaseInventoryModel baseModel = movieModel; MapCommonModel(movie, ref baseModel); // Map the unique movie properties to the movie model. movieModel.Director = movie.Director; movieModel.Duration = movie.Duration; return(movieModel); }
/// <summary> /// Maps a video game to a video game model. /// </summary> /// <param name="game">The video game being mapped to a video game model.</param> /// <returns>A new video game model with all of the relevant properties of the video game.</returns> public static VideoGameModel MapToVideoGameModel(VideoGame game) { // Create a new video game model. VideoGameModel gameModel = CreateNewModel <VideoGameModel>(); // Map all the common properties to the video game model. BaseInventoryModel baseModel = gameModel; MapCommonModel(game, ref baseModel); // Map the unique video game properties to the video game model. gameModel.Developer = game.Developer; gameModel.Rating = game.Rating; return(gameModel); }
/// <summary> /// Maps a book object to a book model. /// </summary> /// <param name="book">The book being mapped to a book model.</param> /// <returns>A new book model with all of the relevant properties of the book.</returns> public static BookModel MapToBookModel(Book book) { // Create a new book model. BookModel bookModel = CreateNewModel <BookModel>(); // Map all the common properties to the book model. BaseInventoryModel baseModel = bookModel; MapCommonModel(book, ref baseModel); // Map the unique book properties to the book model. bookModel.Author = book.Author; bookModel.Publisher = book.Publisher; return(bookModel); }
/// <summary> /// Deletes a specific item. /// </summary> /// <param name="item">The item being deleted.</param> public void DeleteItem(BaseInventoryModel item) { // Remove the item from their respective model collection. if (item is MovieModel movieModel) { MovieModels.Remove(movieModel); } else if (item is BookModel bookModel) { BookModels.Remove(bookModel); } else if (item is VideoGameModel videoGameModel) { VideoGameModels.Remove(videoGameModel); } }
/// <summary> /// Maps all the common properties in an inventory item's base class to an inventory model's base class. /// </summary> /// <param name="sourceItem">The inventory item we are mapping to a model.</param> /// <param name="targetModel">A reference to the model we are mapping the base inventory item's properties to.</param> private static void MapCommonModel(BaseInventoryItem sourceItem, ref BaseInventoryModel targetModel) { // Throw exceptions if any parameters are null. if (sourceItem == null) { throw new ArgumentNullException(nameof(sourceItem)); } if (targetModel == null) { throw new ArgumentNullException(nameof(targetModel)); } // Set all the common properties of the inventory model from the source inventory item. targetModel.Title = sourceItem.Title; targetModel.Cost = sourceItem.Cost; targetModel.Genre = sourceItem.Genre; targetModel.Platform = sourceItem.Platform; targetModel.ReleaseYear = sourceItem.ReleaseYear; }
/// <summary> /// Determines whether or not an item can be currently deleted. Disables the Delete Item button if false. /// </summary> /// <param name="item">The item being deleted.</param> /// <returns>A boolean value indicating whether or not the item can be deleted.</returns> public bool CanDeleteItem(BaseInventoryModel item) { // The user can delete an item if the item exists and the main window is not loading (i.e saving or loading a repository). return(item != null && !IsLoading); }
/// <summary> /// Creates a new dialog window that allows a user to edit a specific inventory item. /// </summary> /// <param name="item">The inventory item being edited.</param> public void ModifyItem(BaseInventoryModel item) { // Get the type of view model we are creating. Type viewModelType; // Get the type of model we need. if (item is MovieModel) { viewModelType = typeof(MovieModel); } else if (item is BookModel) { viewModelType = typeof(BookModel); } else if (item is VideoGameModel) { viewModelType = typeof(VideoGameModel); } else { throw new ArgumentOutOfRangeException(nameof(viewModelType), @"Unknown type for inventory model"); } // Resolve (create) the view model. if (!(App.ServiceProvider.GetService(viewModelType) is BaseInventoryModel viewModel)) { return; } // Copy the item's properties into the new model. viewModel.CopyFrom(item); // Create a new button to save changes to the item. UICommand saveChangesCommand = new UICommand() { Caption = "Save", IsDefault = true, // Only allow the user to save their changes if there are no errors in the data entry. Command = new DelegateCommand( () => { }, () => string.IsNullOrEmpty(viewModel.Error) ) }; // Create a new button that cancels and closes the dialog window for modifying an item. UICommand cancelCommand = new UICommand() { Caption = "Cancel", IsCancel = true, }; // Display the dialog window. UICommand result = AddOrModifyItemDialogService?.ShowDialog( dialogCommands: new[] { saveChangesCommand, cancelCommand }, title: "Modify an Item", viewModel: viewModel ); // Check if the user executed the save command (clicked the save button). if (result != saveChangesCommand) { return; } // Copy the properties from the item we modified back into the original inventory item. item.CopyFrom(viewModel); }