public async Task <IActionResult> Edit(int id, Developer developer) { if (id != developer.DeveloperId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(developer); await _context.SaveChangesAsync(); var selectedDeveloper = _context.Developers.Where(d => d.IdentityUserId == developer.IdentityUserId).SingleOrDefault(); foreach (string ability in selectedDeveloper.SelectedAbilities) { var abilitiesThatMatch = _context.DeveloperAbilities.Where(da => da.Ability.AbilityName == ability).ToList(); var developerThatMatchAbilities = abilitiesThatMatch.Where(am => am.DeveloperId == developer.DeveloperId).ToList(); if (developerThatMatchAbilities != null) { } else { var selectedAbilities = _context.Abilities.Where(a => a.AbilityName == ability).SingleOrDefault(); DeveloperAbilities developerAbilities = new DeveloperAbilities(); developerAbilities.DeveloperId = selectedDeveloper.DeveloperId; developerAbilities.AbilityId = selectedAbilities.AbilityId; _context.DeveloperAbilities.Add(developerAbilities); _context.SaveChanges(); } } } catch (DbUpdateConcurrencyException) { if (!DeveloperExists(developer.DeveloperId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(developer)); }
public async Task <IActionResult> Create(Developer developer) { if (ModelState.IsValid) { var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); developer.IdentityUserId = userId; developer.AvgRating = 0; //Save image to wwwroot/image string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(developer.ProfileImgURL.FileName); string extension = Path.GetExtension(developer.ProfileImgURL.FileName); developer.ImageName = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; string path = Path.Combine(wwwRootPath + "/Image/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await developer.ProfileImgURL.CopyToAsync(fileStream); } _context.Developers.Add(developer); await _context.SaveChangesAsync(); var selectedDeveloper = _context.Developers.Where(d => d.IdentityUserId == userId).SingleOrDefault(); foreach (string ability in selectedDeveloper.SelectedAbilities) { var selectedAbilities = _context.Abilities.Where(a => a.AbilityName == ability).SingleOrDefault(); DeveloperAbilities developerAbilities = new DeveloperAbilities(); developerAbilities.DeveloperId = selectedDeveloper.DeveloperId; developerAbilities.AbilityId = selectedAbilities.AbilityId; _context.DeveloperAbilities.Add(developerAbilities); _context.SaveChanges(); } return(RedirectToAction(nameof(HomePage))); } return(View(developer)); }