public async void TestEdit_InvalidFavoritePlatform_ShouldFail(string value)
        {
            // Arrange
            FavoritePlatformsController controller = new FavoritePlatformsController(_context);
            int userId = int.Parse(value);

            // Act
            FavoritePlatform replayPlatform = await _context.FavoritePlatform
                                              .FirstOrDefaultAsync(a => a.UserId == userId);

            replayPlatform.PlatformId = 0;

            try
            {
                var result = await controller.Edit(replayPlatform.UserId, replayPlatform);

                // Assert
                Assert.IsType <ViewResult>(result);
                ViewResult viewResult = (ViewResult)result;
                Assert.NotNull(viewResult.ViewData.ModelState);
                Assert.NotEmpty(viewResult.ViewData.ModelState.Keys);

                foreach (string item in viewResult.ViewData.ModelState.Keys)
                {
                    Assert.Equal("", item);
                }
            }
            catch (Exception ex)
            {
                Assert.Equal("System.InvalidOperationException", ex.GetType().ToString());
            }
        }
        private void InitializeFavoritePlatform()
        {
            try
            {
                _context.Entry(favoritePlatform).State = EntityState.Detached;
            }
            catch (Exception)
            {
                // TODO: nothing...
            }

            favoritePlatform = new FavoritePlatform()
            {
                UserId     = 26,
                PlatformId = 1
            };
        }
Esempio n. 3
0
        public async Task <IActionResult> Create([Bind("UserId,PlatformId")] FavoritePlatform favoritePlatform)
        {
            if (ModelState.IsValid)
            {
                _context.Add(favoritePlatform);
                await _context.SaveChangesAsync();

                //return RedirectToAction(nameof(Index));
                //return View(favoritePlatform);
            }
            //ViewData["PlatformId"] = new SelectList(_context.Platform, "PlatformId", "PlatformName", favoritePlatform.PlatformId);

            var userId = UserHelper.GetSessionUserId(this);
            var favoritePlatformContext = _context.FavoritePlatform
                                          .Include(f => f.Platform)
                                          .Where(f => f.UserId.Equals(userId));

            ViewData["UserId"]            = userId;
            ViewData["FavoritePlatforms"] = favoritePlatformContext.ToList();
            ViewData["Platforms"]         = getPlatformsNotInFav(userId);

            return(View(favoritePlatform));
        }
Esempio n. 4
0
        public async Task <IActionResult> Edit(int id, [Bind("UserId,PlatformId")] FavoritePlatform favoritePlatform)
        {
            if (id != favoritePlatform.UserId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(favoritePlatform);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FavoritePlatformExists(favoritePlatform.UserId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            if (UserHelper.IsLoggedIn(this))
            {
                ViewData["UserId"] = HttpContext.Session.GetInt32("userId");
            }
            ViewData["PlatformId"] = new SelectList(_context.Platform, "PlatformId", "PlatformName", favoritePlatform.PlatformId);

            return(View(favoritePlatform));
        }