public async Task <IActionResult> Create([Bind("Id,InnovationScore,UsefulnessScore,QualityScore,CompanyValueScore,PresentationScore,Team")] JudgeScoreViewModel teamScoreViewModel) { if (ModelState.IsValid) { var me = User.FindFirst(ClaimTypes.NameIdentifier).Value; //If user already inserted score for this team, don't reinsert inform them var record = await _context.JudgeScores.Where(x => x.Team.TeamCode == teamScoreViewModel.Team && x.KolpiUserId == me).ToListAsync(); var team = await _context.Teams.FirstAsync(x => x.TeamCode.Equals(teamScoreViewModel.Team)); if (record.Any()) { return(RedirectToAction(nameof(HomeController.Error), "Home", new { errorCode = "Duplicate Record", message = $"You aleady evaluated team: {team.TeamName}. Please update their score if you wish to." })); } var teamScore = new JudgeScore(teamScoreViewModel) { KolpiUserId = me, Team = team }; _context.Add(teamScore); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } //Something went wrong, keep user on same page return(View(teamScoreViewModel)); }
public async Task <IActionResult> Create() { var teams = await _context.Teams.Where(x => x.CreatedOn.Year == DateTime.Now.Year).ToListAsync(); var teamSelectlist = teams.Select(x => new SelectListItem { Value = x.TeamCode, Text = $"{x.TeamName} ( {x.Theme.ToString()} )" }).ToList(); var model = new JudgeScoreViewModel { Teams = teamSelectlist }; return(View(model)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var me = User.FindFirst(ClaimTypes.NameIdentifier).Value; var teamScore = await _context.JudgeScores.Include(x => x.Team).SingleOrDefaultAsync(m => m.Id == id && m.KolpiUserId == me); if (teamScore == null) { return(NotFound($"Record with id {id} does not exist in store.")); } var teamScoreViewModel = new JudgeScoreViewModel(teamScore); return(View(teamScoreViewModel)); }
public async Task <IActionResult> Edit(int id, JudgeScoreViewModel teamScoreViewModel) { var allowEditing = await IsScoreEditEnabled(); if (!allowEditing) { return(View("Error", new ErrorViewModel { ErrorCode = "Score Edit Disabled", Message = "Possibly all judges already concluded their scores." })); } if (id != teamScoreViewModel.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var teamScore = _context.JudgeScores.Find(id); var entity = _context.Attach(teamScore); entity.Entity.Id = id; entity.Entity.InnovationScore = teamScoreViewModel.InnovationScore.Value; entity.Entity.UsefulnessScore = teamScoreViewModel.UsefulnessScore.Value; entity.Entity.CompanyValueScore = teamScoreViewModel.CompanyValueScore.Value; entity.Entity.PresentationScore = teamScoreViewModel.PresentationScore.Value; entity.Entity.QualityScore = teamScoreViewModel.QualityScore.Value; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ScoreExists(teamScoreViewModel.Id)) { return(NotFound()); } throw; } return(RedirectToAction(nameof(Index))); } return(View(teamScoreViewModel)); }