コード例 #1
0
        public async Task DeleteConfirmedTest()
        {
            DatabaseContext context = GetNewInMemoryDbWithData();

            var controller = new ThemeProductsController(context);

            //Get the first ThemeProduct from the In Memory Database
            ThemeProduct ThemeProductBeforeDelete = context.ThemeProducts.FirstOrDefault(t => t.Theme != null && t.Product != null);

            //Make sure it has the correct values
            Assert.Equal(4, ThemeProductBeforeDelete.ThemeProductId);
            Assert.Equal(4, ThemeProductBeforeDelete.ThemeId);
            Assert.Equal(4, ThemeProductBeforeDelete.Id);

            //Delete the first ThemeProduct
            var result = await controller.DeleteConfirmed(ThemeProductBeforeDelete.ThemeId);

            //Get the first ThemeProduct from the In Memory Database after deletion
            ThemeProduct ThemeProductAfterDelete = context.ThemeProducts.FirstOrDefault(t => t.Theme != null && t.Product != null);

            Assert.Equal(5, ThemeProductAfterDelete.ThemeProductId);
            Assert.Equal(5, ThemeProductAfterDelete.ThemeId);
            Assert.Equal(5, ThemeProductAfterDelete.Id);

            var viewResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(viewResult.ControllerName);
            Assert.Equal("Index", viewResult.ActionName);
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("ThemeProductId,Id,ThemeId")] ThemeProduct themeProduct)
        {
            if (ModelState.IsValid)
            {
                //If there is already a ThemeProduct with the same Theme and Product relationship and with a different ThemeProductId than the one you are creating, don't Create it and show
                //an error.
                if (_context.ThemeProducts.Any(tp => tp.Product.Id == themeProduct.Id && tp.ThemeId == themeProduct.ThemeId && tp.ThemeProductId != themeProduct.ThemeProductId))
                {
                    ViewData["Error"]   = "This Product and Theme combination already exists.";
                    ViewData["Id"]      = new SelectList(_context.Products, "Id", "Title", themeProduct.Id);
                    ViewData["ThemeId"] = new SelectList(_context.Themes, "ThemeId", "ThemeName", themeProduct.ThemeId);
                    return(View(themeProduct));
                }
                //If it's a unique relationship between the Theme and Product, create it.
                else
                {
                    _context.Add(themeProduct);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            //If ModelState isn't valid, don't create it.
            ViewData["Id"]      = new SelectList(_context.Products, "Id", "Title", themeProduct.Id);
            ViewData["ThemeId"] = new SelectList(_context.Themes, "ThemeId", "ThemeName", themeProduct.ThemeId);
            return(View(themeProduct));
        }
コード例 #3
0
        public async Task CreateTest()
        {
            DatabaseContext context = GetNewInMemoryDbWithData();

            var controller = new ThemeProductsController(context);

            //Create a new ThemeProduct to add
            ThemeProduct NewThemeProduct = new ThemeProduct()
            {
                ThemeProductId = 7,
                Product        = new Product
                {
                    Id           = 7,
                    Title        = "Nieuw Product",
                    Category     = "Create",
                    Contents     = "Nieuw Product beschrijving",
                    ImageUrl     = "\\images\\products\\fishxd.jpg",
                    Description  = "Product create omschrijving",
                    DownloadLink = "productCreation.pdf",
                },
                Theme = new Theme
                {
                    ThemeId     = 7,
                    ThemeName   = "Nieuw Thema",
                    Description = "Thema create omschrijving",
                    ImageUrl    = "\\images\\themes\\beakers.jpg"
                }
            };

            //Create the new ThemeProduct
            var result = await controller.Create(NewThemeProduct);

            //Make sure it has the correct ThemeProductId
            Assert.Equal(7, context.ThemeProducts.Find(7).ThemeProductId);

            //Make sure the Product has all the correct values
            Assert.Equal(7, context.ThemeProducts.Find(7).Product.Id);
            Assert.Equal("Nieuw Product", context.ThemeProducts.Find(7).Product.Title);
            Assert.Equal("Create", context.ThemeProducts.Find(7).Product.Category);
            Assert.Equal("Nieuw Product beschrijving", context.ThemeProducts.Find(7).Product.Contents);
            Assert.Equal("\\images\\products\\fishxd.jpg", context.ThemeProducts.Find(7).Product.ImageUrl);
            Assert.Equal("Product create omschrijving", context.ThemeProducts.Find(7).Product.Description);
            Assert.Equal("productCreation.pdf", context.ThemeProducts.Find(7).Product.DownloadLink);

            //Make sure the Theme has all the correct values
            Assert.Equal(7, context.ThemeProducts.Find(7).Theme.ThemeId);
            Assert.Equal("Nieuw Thema", context.ThemeProducts.Find(7).Theme.ThemeName);
            Assert.Equal("Thema create omschrijving", context.ThemeProducts.Find(7).Theme.Description);
            Assert.Equal("\\images\\themes\\beakers.jpg", context.ThemeProducts.Find(7).Theme.ImageUrl);

            var viewResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(viewResult.ControllerName);
            Assert.Equal("Index", viewResult.ActionName);
        }
コード例 #4
0
        public async Task <IActionResult> Edit(int id, [Bind("ThemeProductId,Id,ThemeId")] ThemeProduct themeProduct)
        {
            var themeProductToUpdate = _context.ThemeProducts.Where(tp => tp.ThemeProductId == id).Single();
            var productId            = Convert.ToInt32(Request.Form["productId"]);
            var themeId = Convert.ToInt32(Request.Form["themeId"]);

            themeProductToUpdate.Id      = productId;
            themeProductToUpdate.ThemeId = themeId;
            if (ModelState.IsValid)
            {
                _context.Update(themeProductToUpdate);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(themeProduct));
        }