public IActionResult Add(AddGameViewModel addGameViewModel) { if (ModelState.IsValid) { Game newGame = new Game(addGameViewModel); newGame.UserId = Models.Extensions.GetUserID(User); context.Games.Add(newGame); context.SaveChanges(); string genres = addGameViewModel.GenreNames; int gameId = newGame.GameId; Models.Extensions.AddGenres(context, gameId, genres); return(Redirect("/List?m=Game Successfully Added")); } string genreIds = addGameViewModel.GenreIds; AddGameViewModel newAddGameViewModel = new AddGameViewModel(context, genreIds); return(View(newAddGameViewModel)); }
public static int GetPropId(string propName, GameTradeDbContext context, string dbSetType) { try { var dbSet = GetDbSet(context, dbSetType); var query = dbSet.Single(g => g.GetType().GetProperty("Name").GetValue(g).ToString() == propName); var itemId = query.GetType().GetProperty("Id").GetValue(query).ToString(); return(Int32.Parse(itemId)); } catch (InvalidOperationException) { if (dbSetType == "Platforms") { Platform newPlatform = new Platform { Name = propName }; context.Platforms.Add(newPlatform); context.SaveChanges(); return(newPlatform.Id); } else { Genre newGenre = new Genre { Name = propName }; context.Genres.Add(newGenre); context.SaveChanges(); return(newGenre.Id); } } }
public static void AddGenres(GameTradeDbContext context, int gameId, string genreIds = null) { string[] genreIdsSplit = SplitGenreIds(genreIds); for (int i = 0; i < genreIdsSplit.Length; i++) { int genreId = GenreNameToId(context, genreIdsSplit, i); GameGenre newGameGenre = new GameGenre { GameId = gameId, GenreId = genreId }; context.GameGenres.Add(newGameGenre); context.SaveChanges(); } }