public JsonResult CreateMatch([DataSourceRequest] DataSourceRequest request, MatchViewModel match)
        {
            if (match != null && ModelState.IsValid)
            {
                var hostTeam = this.Data.Teams.All().FirstOrDefault(x => x.Name == match.Host);
                var visitorTeam = this.Data.Teams.All().FirstOrDefault(x => x.Name == match.Visitor);
                var gameweek = this.Data.Gameweeks.All().FirstOrDefault(g => g.Name == match.Gameweek);
                var matchExists = this.Data.Matches.All().FirstOrDefault(m => m.Host.Name == match.Host &&
                    m.Visitor.Name == match.Visitor);

                if (matchExists == null)
                {
                    var newMatch = new Match
                    {
                        Host = hostTeam,
                        Visitor = visitorTeam,
                        HostScore = match.HostScore,
                        VistorScore = match.VisitorScore,
                        Gameweek = gameweek,
                        MatchDate = match.MatchDate
                    };

                    this.Data.Matches.Add(newMatch);
                    this.Data.SaveChanges();
                }
                match.Id = match.Id;
            }

            return Json(new[] { match }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
        public JsonResult EditMatch([DataSourceRequest] DataSourceRequest request, MatchViewModel match)
        {
            var editedMatch = this.Data.Matches.GetById(match.Id);
            var hostTeam = this.Data.Teams.All().FirstOrDefault(x => x.Name == match.Host);
            var visitorTeam = this.Data.Teams.All().FirstOrDefault(x => x.Name == match.Visitor);
            var gameweek = this.Data.Gameweeks.All().FirstOrDefault(g => g.Name == match.Gameweek);

            editedMatch.Gameweek = gameweek;
            editedMatch.Host = hostTeam;
            editedMatch.Visitor = visitorTeam;
            editedMatch.HostScore = match.HostScore;
            editedMatch.VistorScore = match.VisitorScore;
            editedMatch.MatchDate = match.MatchDate;

            this.Data.SaveChanges();

            return Json(new[] { match }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteMatch([DataSourceRequest] DataSourceRequest request, MatchViewModel match)
        {
            var deletedMatch = this.Data.Matches.GetById(match.Id);

            this.Data.Matches.Delete(deletedMatch);
            this.Data.SaveChanges();

            return Json(new[] { match }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
Exemplo n.º 4
0
        private Match AddOrUpdateMatch(MatchViewModel matchModel)
        {
            var matchExists = data.Matches.All()
                .FirstOrDefault(m => (m.Host.Name.Contains(matchModel.Host)) &&
                    (m.Visitor.Name.Contains(matchModel.Visitor)));

            var hostTeam = data.Teams.All().FirstOrDefault(x => x.Name.Contains(matchModel.Host));
            var visitorTeam = data.Teams.All().FirstOrDefault(x => x.Name.Contains(matchModel.Visitor));
            if (hostTeam == null || visitorTeam == null)
            {
                return null;
            }

            var gameweekEntity = AddOrUpdateGameweek(matchModel);

            if (matchExists == null)
            {
                var newMatch = new Match()
                {
                    Gameweek = gameweekEntity,
                    Host = hostTeam,
                    Visitor = visitorTeam,
                    HostScore = matchModel.HostScore,
                    VistorScore = matchModel.VisitorScore,
                    MatchDate = matchModel.MatchDate
                };

                data.Matches.Add(newMatch);
                return newMatch;
            }
            else
            {
                matchExists.HostScore = matchModel.HostScore;
                matchExists.VistorScore = matchModel.VisitorScore;
                matchExists.MatchDate = matchModel.MatchDate;
                matchExists.Gameweek = gameweekEntity;

                return matchExists;
            }
        }
Exemplo n.º 5
0
        private Gameweek AddOrUpdateGameweek(MatchViewModel matchModel)
        {
            var gameweek = data.Gameweeks.All().FirstOrDefault(g => g.Name == matchModel.Gameweek);

            int gamewekIndex = matchModel.Gameweek.LastIndexOf(' ');
            int gameweekNumber = int.Parse(matchModel.Gameweek.Substring(gamewekIndex + 1));
            var previousGameweek = data.Gameweeks.GetById(gameweekNumber - 1);
            if (previousGameweek != null)
            {
                startDate = previousGameweek.EndDate;
            }

            if (gameweek == null)
            {
                var newGameweek = new Gameweek()
                {
                    Id = gameweekNumber,
                    Name = matchModel.Gameweek,
                    StartDate = startDate,
                    EndDate = matchModel.MatchDate.AddDays(1)
                };

                data.Gameweeks.Add(newGameweek);
                data.SaveChanges();
                gameweek = data.Gameweeks.GetById(newGameweek.Id);
            }
            else
            {
                gameweek.StartDate = startDate;
                gameweek.EndDate = matchModel.MatchDate.AddDays(1);
            }

            return gameweek;
        }
Exemplo n.º 6
0
        public void UpdateFixtures(List<string> fixtures)
        {
            startDate = startSeason;
            List<Match> gameweekMatches = new List<Match>();
            string gameweek = fixtures[0];
            for (int i = 1; i < fixtures.Count; i += 4)
            {
                string[] dateParts = fixtures[i].Split(' ');
                string dateAsString = string.Format("{0} {1} {2} {3}", dateParts[0], dateParts[1], startSeason.Year, dateParts[2]);
                DateTime matchDate = DateTime.ParseExact(dateAsString, "dd MMM yyyy HH:mm", CultureInfo.InvariantCulture);
                matchDate = matchDate < startDate ? matchDate.AddYears(1) : matchDate;
                string host = ConvertTeamName(fixtures[i + 1]);
                string visitor = ConvertTeamName(fixtures[i + 3]);

                var matchModel = new MatchViewModel()
                {
                    Host = host,
                    Visitor = visitor,
                    Gameweek = gameweek,
                    MatchDate = matchDate
                };
                if (fixtures[i + 2] != "v")
                {
                    string[] result = fixtures[i + 2].Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                    matchModel.HostScore = int.Parse(result[0]);
                    matchModel.VisitorScore = int.Parse(result[1]);
                }

                Match match = AddOrUpdateMatch(matchModel);
                if (match != null)
                {
                    this.data.SaveChanges();
                    gameweekMatches.Add(match);
                }
            }

            Gameweek currentGameweek = data.Gameweeks.All().FirstOrDefault(g => g.Name == gameweek);
            List<int> gameweekMatchesIds = gameweekMatches.Select(g => g.Id).ToList();
            data.Matches.DeleteRange(m => m.GameweekId == currentGameweek.Id && !gameweekMatchesIds.Contains(m.Id));

            data.SaveChanges();
        }