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);
        }
Esempio n. 2
0
        public bool Equals(Match match)
        {
            // If parameter is null return false:
            if ((object)match == null)
            {
                return false;
            }

            // Return true if the fields match:
            return (Host_TeamId == match.Host_TeamId) && (Visitor_TeamId == match.Visitor_TeamId);
        }
        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;
            }
        }