public async Task <IActionResult> Create([Bind("Id,Title,Price,Rating,ReleaseDate,Platform,Description,Image,ManufacturerId")] Game game, int[] Genres, IFormFile Image)
        {
            var modelState = ModelState.IsValid;

            ViewBag.ModelState = modelState;

            if (ModelState.IsValid)
            {
                //Here we fill the connection table between games and genres.
                if (Genres != null)
                {
                    foreach (var genreId in Genres)
                    {
                        var genre = _context.Genres.Find(genreId);

                        var gameGenre = new GameGenreConnection()
                        {
                            Game  = game,
                            Genre = genre
                        };
                        _context.GameGenreConnection.Add(gameGenre);
                    }
                }
                //Here we add the image of the game.
                using (var stream = new MemoryStream())
                {
                    await Image.CopyToAsync(stream);

                    game.Image = stream.ToArray();
                }

                _gameRepository.Add(game);
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ManufacturerId"] = new SelectList(_context.Manufacturers, "Id", "Name", game.ManufacturerId);
            return(RedirectToAction("Create", "Games"));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Price,Rating,ReleaseDate,Platform,Description,Image,ManufacturerId")] Game game, int[] Genres, IFormFile Image)
        {
            if (id != game.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //Update the data in the connection table between games and genres
                    if (Genres != null)
                    {
                        var gameGenres = _context.GameGenreConnection.Where(g => g.GameId == id).ToList();

                        var genersId = gameGenres.Select(g => g.GenreId).ToList();
                        foreach (var genreId in Genres)
                        {
                            if (!genersId.Contains(genreId))
                            {
                                var genre = _context.Genres.Find(genreId);

                                var gameGenre = new GameGenreConnection()
                                {
                                    Game  = game,
                                    Genre = genre
                                };

                                _context.GameGenreConnection.Update(gameGenre);
                            }
                        }
                    }

                    //Update the image of the selected game.
                    using (var stream = new MemoryStream())
                    {
                        await Image.CopyToAsync(stream);

                        game.Image = stream.ToArray();
                    }

                    _gameRepository.Update(game);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!GameExists(game.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ManufacturerId"] = new SelectList(_context.Manufacturers, "Id", "Name", game.ManufacturerId);
            return(View(game));
        }