public ActionResult Create(Season season)
        {
            if (ModelState.IsValid)
            {
                db.SeasonSet.AddObject(season);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(season);
        }
 private void UpdatePlacementInSeasonTable(Season season)
 {
     int i = 1;
     var lastSeasonTable = new SeasonTable();
     var seasonTableOrdered = db.SeasonTableSet.OrderBy("it.Points DESC");
     seasonTableOrdered = (ObjectQuery<SeasonTable>) seasonTableOrdered.Where(s => s.SeasonId == season.Id);
     foreach (SeasonTable currentSeasonTable in seasonTableOrdered)
     {
         currentSeasonTable.Placement = i;
         i++;
         if (lastSeasonTable.Id != 0)
         {
             CheckGoalDifference(lastSeasonTable, currentSeasonTable);
         }
         lastSeasonTable = currentSeasonTable;
     }
     db.SaveChanges();
 }
 private void DeleteSeasonTableEntries(Season season)
 {
     foreach (SeasonTable st in db.SeasonTableSet)
     {
         if (st.SeasonId == season.Id)
         {
             int delId = st.Id;
             SeasonTable sesDel = db.SeasonTableSet.Single(s => s.Id == delId);
             db.SeasonTableSet.DeleteObject(sesDel);
         }
     }
     db.SaveChanges();
 }
        private void CreateTableForSeason(Season season)
        {
            IEnumerable<Match> matches = season.Match.ToList();

            //create a new seasontablelist from all matches in this season
            var seasonTableEntries = CreateSeasonTableEntriesFromMatchesInSeason(season, matches);

            //delete all for this season
            DeleteSeasonTableEntries(season);

            //add all to new entries to this season in db
            AddNewSeasonTableEntries(seasonTableEntries);

            //update placement based on points and goaldifference
            UpdatePlacementInSeasonTable(season);
        }
        private IEnumerable<SeasonTable> CreateSeasonTableEntriesFromMatchesInSeason(Season season, IEnumerable<Match> matches)
        {
            var seasonTableEntries = new List<SeasonTable>();
            foreach (Match match in matches)
            {
                int homeTeamId = match.HomeTeamId;
                int awayTeamId = match.AwayTeamId;
                bool homeTeamExists = seasonTableEntries.Exists(t => t.TeamId == homeTeamId);
                bool awayTeamExists = seasonTableEntries.Exists(t => t.TeamId == awayTeamId);

                SeasonTable seasonTableEntry;
                if (!homeTeamExists)
                {
                    seasonTableEntry = new SeasonTable
                                           {
                                               SeasonId = season.Id,
                                               TeamId = match.TeamHome.Id,
                                               Placement = 0,
                                               Points = match.HomeGoals > match.AwayGoals ? 3 : 0,
                                               Victories = match.HomeGoals > match.AwayGoals ? 1 : 0,
                                               Defeats = match.HomeGoals < match.AwayGoals ? 1 : 0,
                                               GoalsScored = match.HomeGoals,
                                               GoalsLetIn = match.AwayGoals,
                                               GoalDifference = match.HomeGoals - match.AwayGoals,
                                               MatchesPlayed = 1
                                           };

                    seasonTableEntries.Add(seasonTableEntry);
                }
                else
                {
                    seasonTableEntry = seasonTableEntries.Single(s => s.TeamId == match.HomeTeamId);
                    if (match.HomeGoals > match.AwayGoals)
                    {
                        seasonTableEntry.Points += 3;
                        seasonTableEntry.Victories += 1;
                    }
                    else
                    {
                        seasonTableEntry.Defeats += 1;
                    }

                    seasonTableEntry.GoalsScored += match.HomeGoals;
                    seasonTableEntry.GoalsLetIn += match.AwayGoals;
                    seasonTableEntry.GoalDifference = seasonTableEntry.GoalsScored - seasonTableEntry.GoalsLetIn;
                    seasonTableEntry.MatchesPlayed += 1;
                }

                if (!awayTeamExists)
                {
                    seasonTableEntry = new SeasonTable
                                           {
                                               SeasonId = season.Id,
                                               TeamId = match.TeamAway.Id,
                                               Placement = 0,
                                               Points = match.AwayGoals > match.HomeGoals ? 3 : 0,
                                               Victories = match.AwayGoals > match.HomeGoals ? 1 : 0,
                                               Defeats = match.AwayGoals < match.HomeGoals ? 1 : 0,
                                               GoalsScored = match.AwayGoals,
                                               GoalsLetIn = match.HomeGoals,
                                               GoalDifference = match.AwayGoals - match.HomeGoals,
                                               MatchesPlayed = 1
                                           };

                    seasonTableEntries.Add(seasonTableEntry);
                }
                else
                {
                    seasonTableEntry = seasonTableEntries.Single(s => s.TeamId == match.AwayTeamId);
                    if (match.AwayGoals > match.HomeGoals)
                    {
                        seasonTableEntry.Points += 3;
                        seasonTableEntry.Victories += 1;
                    }
                    else
                    {
                        seasonTableEntry.Defeats += 1;
                    }

                    seasonTableEntry.GoalsScored += match.AwayGoals;
                    seasonTableEntry.GoalsLetIn += match.HomeGoals;
                    seasonTableEntry.GoalDifference = seasonTableEntry.GoalsScored - seasonTableEntry.GoalsLetIn;
                    seasonTableEntry.MatchesPlayed += 1;
                }
            }
            return seasonTableEntries;
        }
 /// <summary>
 /// Create a new Season object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="startDate">Initial value of the StartDate property.</param>
 /// <param name="endDate">Initial value of the EndDate property.</param>
 /// <param name="isPreSeason">Initial value of the IsPreSeason property.</param>
 public static Season CreateSeason(global::System.Int32 id, global::System.String name, global::System.DateTime startDate, global::System.DateTime endDate, global::System.Boolean isPreSeason)
 {
     Season season = new Season();
     season.Id = id;
     season.Name = name;
     season.StartDate = startDate;
     season.EndDate = endDate;
     season.IsPreSeason = isPreSeason;
     return season;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the SeasonSet EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSeasonSet(Season season)
 {
     base.AddObject("SeasonSet", season);
 }
 public ActionResult Edit(Season season)
 {
     if (ModelState.IsValid)
     {
         db.SeasonSet.Attach(season);
         db.ObjectStateManager.ChangeObjectState(season, EntityState.Modified);
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(season);
 }