Beispiel #1
0
        public async Task <IActionResult> Add(Game game, IFormFile image, string publisherName)
        {
            if (game == null)
            {
                return(BadRequest());
            }
            var dir = _env.WebRootPath;

            await using (var fileStream = new FileStream(Path.Combine(dir, $"{game.Name}.png"), FileMode.Create, FileAccess.Write))
            {
                await image.CopyToAsync(fileStream);

                game.Img = $"{game.Name}.png";
            }
            await using (var db = new GameCatalogContext())
            {
                if (db.Publishers.FirstOrDefault(publisher => publisher.Name == publisherName) == null)
                {
                    await db.Publishers.AddAsync(new Publisher()
                    {
                        Name = publisherName
                    });

                    await db.SaveChangesAsync();
                }
                game.PublisherId = db.Publishers.FirstOrDefault(publisher => publisher.Name == publisherName).Id;
                await _catalog.AddGameAsync(game);
            }
            return(RedirectToAction("Index", "Home"));
        }