// Save images from UpdatePokemon model private string[] UpdatedPokemonImages(UpdatePokemon model) { string[] imageUrls = new string[2]; if (model.UploadImage != null || model.UploadShinyImage != null) { string uploadsFolder = Path.Combine(webEnvironment.WebRootPath, "images/pokemon"); if (model.UploadImage != null) { imageUrls[0] = "uploaded" + Guid.NewGuid().ToString() + "_" + model.UploadImage.FileName; string imagePath = Path.Combine(uploadsFolder, imageUrls[0]); using (var fileStream = new FileStream(imagePath, FileMode.Create)) { model.UploadImage.CopyTo(fileStream); } } if (model.UploadShinyImage != null) { imageUrls[1] = "uploaded" + Guid.NewGuid().ToString() + "_" + model.UploadShinyImage.FileName; string shinyImagePath = Path.Combine(uploadsFolder, imageUrls[1]); using (var fileStream = new FileStream(shinyImagePath, FileMode.Create)) { model.UploadShinyImage.CopyTo(fileStream); } } } return(imageUrls); }
public async Task <IActionResult> UpdatePokemon(int id) { Pokemon model = _context.Pokemon.Find(id); var areaIDs = await _context.AreaPokemon.Where(p => p.PokemonId == id).Select(a => a.AreaId).ToListAsync(); List <Area> allAreas = await _context.Areas.ToListAsync(); UpdatePokemon formModel = new UpdatePokemon { PokemonId = model.PokemonId, PokemonName = model.PokemonName, PokedexNum = model.PokedexNum, Type_1 = model.Type_1, Type_2 = model.Type_2, Classification = model.Classification, Description = model.Description, Height = model.Height, Weight = model.Weight, Generation = model.Generation, ImageUrl = model.Image, SelectedAreas = allAreas, AreaIds = areaIDs, Rarity = model.Rarity }; ViewData["Types"] = await _context.Pokemon.Select(p => p.Type_1).Distinct().ToListAsync(); ViewData["Areas"] = await _context.Areas.Select(a => a.AreaId).ToListAsync(); return(View(formModel)); }
public async Task <IActionResult> UpdatePokemon(UpdatePokemon model) { if (ModelState.IsValid) { try { string[] imageUrls = UpdatedPokemonImages(model); string standardUrl = imageUrls[0]; string shinyImageUrl = imageUrls[1]; Pokemon pokemon = _context.Pokemon.Find(model.PokemonId); string filepath = Path.Combine(webEnvironment.WebRootPath, "images/pokemon"); // If the user updated the images, delete the existing ones if (standardUrl != null) { var standardPath = Path.Combine(Directory.GetCurrentDirectory(), filepath, pokemon.Image); if (System.IO.File.Exists(standardPath)) { System.IO.File.Delete(standardPath); } } if (shinyImageUrl != null) { var shinyPath = Path.Combine(Directory.GetCurrentDirectory(), filepath, pokemon.ShinyImage); if (System.IO.File.Exists(shinyPath)) { System.IO.File.Delete(shinyPath); } } pokemon.PokemonName = model.PokemonName; pokemon.PokedexNum = model.PokedexNum; pokemon.Type_1 = model.Type_1; pokemon.Type_2 = model.Type_2; pokemon.Classification = model.Classification; pokemon.Description = model.Description; pokemon.Height = model.Height; pokemon.Weight = model.Weight; pokemon.Generation = model.Generation; // If the user uploaded a picture for standard/shiny, update the Image/ShinyImage, if not, keep the original. pokemon.Image = standardUrl == null ? pokemon.Image : standardUrl; pokemon.ShinyImage = shinyImageUrl == null ? pokemon.ShinyImage : shinyImageUrl; pokemon.Rarity = model.Rarity; // Removes areas that have been deselected _context.AreaPokemon.Where(ap => ap.PokemonId == pokemon.PokemonId).ToList().ForEach(a => _context.AreaPokemon.Remove(a)); // Adds areas the have been selected List <AreasPokemon> newListOfAreas = new List <AreasPokemon>(); if (model.AreaIds != null) { foreach (int areaId in model.AreaIds) { newListOfAreas.Add(new AreasPokemon { AreaId = areaId, PokemonId = pokemon.PokemonId }); } foreach (AreasPokemon area in newListOfAreas) { _context.Add(area); } } _context.SaveChanges(); return(RedirectToAction("Pokemon", "Admin")); } catch (Exception) { ModelState.AddModelError("Custom", "Pokémon Name or Number already exists!"); ViewData["Types"] = await _context.Pokemon.Select(p => p.Type_1).Distinct().ToListAsync(); ViewData["Areas"] = await _context.Areas.Select(a => a.Name).ToListAsync(); return(View(model)); } } ViewData["Types"] = await _context.Pokemon.Select(p => p.Type_1).Distinct().ToListAsync(); ViewData["Areas"] = await _context.Areas.Select(a => a.Name).ToListAsync(); return(View(model)); }