public async Task <IActionResult> Create([Bind("TeamName,Avatar,Theme,ProblemStatement,ITRequirements,Location,OtherRequirements,Participants")] TeamViewModel teamViewModel) { //if (teamViewModel.Location.Equals("Kathmandu", StringComparison.InvariantCultureIgnoreCase)) // return RedirectToAction("Error", "Home", new ErrorViewModel { ErrorCode = "Registration", Message = "Team registration expired for Nepal center." }); if (ModelState.IsValid) { if (!ParticipantsEnteredCorrectly(teamViewModel.Participants)) { ViewData["locations"] = FetchEventLocationSelectItemList(); return(View(teamViewModel)); } //Raw participants in text are good to deserialize var participantsDtos = TeamViewModel.DeserializeParticipants(teamViewModel.Participants); List <Participant> partcipantsOnDb = _context.Participants .Where(x => x.Team.CreatedOn.Year == DateTime.Now.Year).ToList(); //partcipantsOnDb = _context.Teams // .Include(x => x.Participants) // .Where(x => IsCurrentYear(x.CreatedOn)) // .SelectMany(x => x.Participants).ToList(); var alreadyTakenParticipants = partcipantsOnDb.Intersect(participantsDtos, ComplexTypeComparer <Participant> .Create(x => x.Inumber)); if (alreadyTakenParticipants.Any()) { ModelState.AddModelError("Participants", $"Participants submitted are already involved with other teams: {string.Join(", ", alreadyTakenParticipants.Select(x => x.Name))}. Note: individual participants are distinguished by their INumber."); ViewData["locations"] = FetchEventLocationSelectItemList(); return(View(teamViewModel)); } //Autogenerate properties at creation time teamViewModel.CreatedBy = User.FindFirst(ClaimTypes.Name)?.Value ?? ""; teamViewModel.CreatedOn = DateTime.Now; teamViewModel.TeamCode = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", ""); _context.Add(new Team(teamViewModel, participantsDtos)); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(teamViewModel)); }
public async Task <IActionResult> Edit(string teamCode, [Bind("Id,TeamCode,TeamName,Avatar,Theme,ProblemStatement,ITRequirements,RepoUrl,Location,OtherRequirements,Participants")] TeamViewModel teamViewModel) { if (teamCode != teamViewModel.TeamCode) { return(NotFound()); } if (ModelState.IsValid) { if (!ParticipantsEnteredCorrectly(teamViewModel.Participants)) { ViewData["locations"] = FetchEventLocationSelectItemList(); return(View(teamViewModel)); } //Raw participants in text are good to deserialize var participantsDtos = TeamViewModel.DeserializeParticipants(teamViewModel.Participants); var team = new Team(teamViewModel, participantsDtos); var oldParticipants = _context.Participants.Where(x => x.Team.Id == team.Id); _context.Participants.RemoveRange(oldParticipants); try { _context.Attach(team); _context.Entry(team).Property(p => p.TeamName).IsModified = true; _context.Entry(team).Property(p => p.ProblemStatement).IsModified = true; _context.Entry(team).Property(p => p.Theme).IsModified = true; _context.Entry(team).Collection(p => p.Participants).IsModified = true; _context.Entry(team).Property(p => p.ITRequirements).IsModified = true; _context.Entry(team).Property(p => p.OtherRequirements).IsModified = true; _context.Entry(team).Property(p => p.RepoUrl).IsModified = true; _context.Entry(team).Property(p => p.Location).IsModified = true; //Only flag as modified if user uploads something if (team?.Avatar?.Length > 0) { _context.Entry(team).Property(p => p.Avatar).IsModified = true; } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TeamViewModelExists(teamViewModel.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["locations"] = FetchEventLocationSelectItemList(); return(View(teamViewModel)); }