Beispiel #1
0
        public ActionResult Create(Match match)
        {
            var matchError = GetMatchError(match);
            if (ModelState.IsValid && string.IsNullOrEmpty(matchError)) {
                db.Matches.Add(match);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.HomeTeamId = new SelectList(db.Teams, "Id", "Name", match.HomeTeamId);
            ViewBag.AwayTeamId = new SelectList(db.Teams, "Id", "Name", match.AwayTeamId);

            ViewBag.Error = matchError;
            return View(match);
        }
Beispiel #2
0
 private string GetMatchError(Match match)
 {
     var homeTeam = db.Teams.Find(match.HomeTeamId);
     var awayTeam = db.Teams.Find(match.AwayTeamId);
     var isInSameGroup = homeTeam.GroupId == awayTeam.GroupId;
     var homeTeamOpponents =
         homeTeam.HomeMatches.Select(x => x.AwayTeamId).Concat(homeTeam.AwayMatches.Select(x => x.HomeTeamId)).Distinct();
     var hasNotPlayedBefore = !homeTeamOpponents.Contains(awayTeam.Id);
     var error = isInSameGroup ? string.Empty : "Ikke i samme gruppe";
     error += hasNotPlayedBefore ? string.Empty : "Kamp allerede registrert";
     error += match.HomeTeamId != match.AwayTeamId ? string.Empty : "Samme lag";
     return error;
 }
Beispiel #3
0
 public ActionResult Edit(Match match)
 {
     if (ModelState.IsValid)
     {
         db.Entry(match).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index", "Home");
     }
     ViewBag.HomeTeamId = new SelectList(db.Teams, "Id", "Name", match.HomeTeamId);
     ViewBag.AwayTeamId = new SelectList(db.Teams, "Id", "Name", match.AwayTeamId);
     return View(match);
 }