public async Task <IActionResult> DeleteCompetitor(int Id) { int competitionId = (int)TempData["CompetitionId"]; CompetitionCompetitor Link = _context.CompetitionCompetitor.Where(j => j.CompetitorId == Id).FirstOrDefault(c => c.CompetitionId == competitionId); if (Link == null) { return(NotFound()); } _context.CompetitionCompetitor.Remove(Link); //Rensa bort dives för den tävlande List <Dive> dives = await _context.Dive.Where(j => j.CompetitorId == Id && j.CompetitionId == competitionId).ToListAsync(); foreach (var dive in dives) { _context.Dive.Remove(dive); } await _context.SaveChangesAsync(); return(RedirectToAction("addCompetitors", new { id = competitionId })); }
public async Task <IActionResult> AddCompetitors(int CompetitorName) { int competitionId = (int)TempData["CompetitionId"]; var newLink = new CompetitionCompetitor { CompetitionId = competitionId, CompetitorId = CompetitorName }; if (!_context.CompetitionCompetitor.Any(cc => cc.CompetitorId == newLink.CompetitorId && cc.CompetitionId == newLink.CompetitionId)) { _context.CompetitionCompetitor.Add(newLink); } int DivesToCreate = 5; // Create 5 dives associated to the added competitor for (int i = 0; i < DivesToCreate; i++) { var newDive = new Dive { CompetitionId = competitionId, CompetitorId = CompetitorName }; _context.Dive.Add(newDive); } _context.SaveChanges(); return(RedirectToAction("AddCompetitors")); }
// GET: Dives/JudgeDives/5 public async Task <IActionResult> JudgeDives(int CompetitorId, int CompetitionId) { ViewData["competitorId"] = CompetitorId; ViewData["competitionId"] = CompetitionId; //Returns the link CompetitionCompetitor link = await _context.CompetitionCompetitor.Where(cc => cc.CompetitionId == CompetitionId && cc.CompetitorId == CompetitorId).FirstAsync(); //Returns list of dives for this link List <Dive> dives = await _context.Dive.Where(cc => cc.CompetitionId == CompetitionId && cc.CompetitorId == CompetitorId).ToListAsync(); Competitor divers = _context.Competitor.Where(d => d.Id == CompetitorId).First(); // Returns the competitor ViewData["competitorName"] = divers.Name; Competition divecomp = _context.Competition.Where(c => c.Id == CompetitionId).First(); // Rturns the competition ViewData["competitionName"] = divecomp.Name; ViewData["divedifficulty"] = _context.DiveGroup.ToList(); //Full list of all Divegroups //Iterates over the list of dives and adds the scores together double?FinalScore = 0; foreach (var dive in dives) { FinalScore += (double?)dive.Score; } //Displays final score ViewData["FinalScore"] = FinalScore; //Updates the final score to the database link.FinalScore = FinalScore; _context.CompetitionCompetitor.Update(link); await _context.SaveChangesAsync(); return(View("JudgeDives", await _context.Dive.ToListAsync())); }
public async Task <IActionResult> Edit(int id, [Bind("Id,CompetitionId,CompetitorId,DiveGroup,Judge1,Judge2,Judge3")] Dive dive) { if (id != dive.Id) { return(NotFound()); } //Returns list of dives List <Dive> dives = null; //Returns the link CompetitionCompetitor link = await _context.CompetitionCompetitor.Where(cc => cc.CompetitionId == dive.CompetitionId && cc.CompetitorId == dive.CompetitorId).FirstAsync(); // Searches through the DB for the match where the divegroups match. The DiveGroup which match are put in a variable DiveGroup grupp = await _context.DiveGroup.Where(cc => cc.Dive == dive.DiveGroup).FirstAsync(); double?diff = grupp.Difficulty; //Gets the difficulty from the variable that matched the dive // Checks the median of all dives and takes the difficulty and multiplies it to the median score // E.g: If J1 < J2 and J2 < J3 OR J3 < J2 and J2 < J1, then J2 is the median if (ModelState.IsValid) { try { if ((dive.Judge1 < dive.Judge2 && dive.Judge2 < dive.Judge3) || (dive.Judge3 < dive.Judge2 && dive.Judge2 < dive.Judge1)) { dive.Score = dive.Judge2 * diff * 3; } else if ((dive.Judge2 < dive.Judge1 && dive.Judge1 < dive.Judge3) || (dive.Judge3 < dive.Judge1 && dive.Judge1 < dive.Judge2)) { dive.Score = dive.Judge1 * diff * 3; } else { dive.Score = dive.Judge3 * diff * 3; } _context.Dive.Update(dive); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DiveExists(dive.Id)) { return(NotFound()); } else { throw; } } dives = await _context.Dive.Where(cc => cc.CompetitionId == dive.CompetitionId && cc.CompetitorId == dive.CompetitorId).ToListAsync(); //Iterates over the list of dives and adds the scores together double?FinalScore = 0; foreach (var div in dives) { FinalScore += (double?)div.Score; } link.FinalScore = FinalScore; _context.CompetitionCompetitor.Update(link); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Competitions", new { id = dive.CompetitionId })); } ViewData["CompetitionId"] = new SelectList(_context.Competition, "Id", "Id", dive.CompetitionId); ViewData["CompetitorId"] = new SelectList(_context.Competitor, "Id", "Id", dive.CompetitorId); // Gets all of the divecategories and outs them in a list ViewData["divecategories"] = new SelectList(_context.DiveGroup, "Dive", "Dive", dive.DiveGroup); return(RedirectToAction("JudgeDives", new { CompetitorId = dive.CompetitorId, CompetitionId = dive.CompetitionId })); }