public ActionResult Index(LoadRankingsModel loadRankingsModel) { using (tennisDBEntities db = new tennisDBEntities()) { if (Request.RequestType == "POST") { if (loadRankingsModel.RankingsToUpdate == "ATP") { using (WebClient wc = new WebClient()) { string Gender = "M"; var sJson = wc.DownloadString("https://vbarbaresi.opendatasoft.com/explore/dataset/atp-rankings/download/?format=json&timezone=Europe/London"); JArray json = JsonConvert.DeserializeObject <JArray>(sJson); JEnumerable <JToken> records = json.Children(); JToken Timestamp = records.First().SelectToken("record_timestamp"); int year = Timestamp.ToObject <DateTime>().Year; int count = 0; db.rankings.RemoveRange(db.rankings.Select(r => r).Where(r => r.player.gender == Gender && r.year == year)); foreach (var record in records) { JToken Fields = record.SelectToken("fields"); string PlayerName = Fields.Value <string>("player_name"); string CountryName = Fields.Value <string>("player_country"); int Points = Fields.Value <int>("player_points"); if (CountryName == "United Kingdom") { CountryName = "Great Britain"; } else if (CountryName.Equals("usa", StringComparison.InvariantCultureIgnoreCase) || CountryName == "United States") { CountryName = "USA"; } else if (CountryName == "Bosnia") { CountryName = "Bosnia And Herzegovina"; } else if (CountryName == "Slovak Republic") { CountryName = "Slovakia"; } else if (CountryName == "Chinese Taipei") { CountryName = "Taiwan"; } PlayerName = PlayerName.Trim(); PlayerName = Regex.Replace(PlayerName, " +", " "); ranking Ranking = new ranking(); Ranking.points = Points; Ranking.year = year; if (db.players.Select(p => p).Where(p => p.name == PlayerName && p.country.name == CountryName).SingleOrDefault() == null) { player Player = new player(); Player.name = PlayerName; Player.gender = Gender; if (db.countries.Select(c => c).Where(c => c.name == CountryName).SingleOrDefault() == null) { country Country = new country(); Country.name = CountryName; db.countries.Add(Country); db.SaveChanges(); } Player.country_id = db.countries.Select(c => c).Where(c => c.name == CountryName).Single().id; db.players.Add(Player); db.SaveChanges(); } Ranking.player_id = db.players.Select(p => p).Where(p => p.name == PlayerName && p.country.name == CountryName).Single().id; db.rankings.Add(Ranking); db.SaveChanges(); count++; } RankingsUpdate.ResetRank(year, Gender, db); db.SaveChanges(); loadRankingsModel.Message = "Loaded " + count + " rankings."; } } if (loadRankingsModel.RankingsToUpdate == "WTA") { using (WebClient wc = new WebClient()) { string Gender = "F"; var sJson = wc.DownloadString("https://data.opendatasoft.com/explore/dataset/wta-rankings@vbarbaresi/download/?format=json&timezone=Europe/London"); JArray json = JsonConvert.DeserializeObject <JArray>(sJson); JEnumerable <JToken> records = json.Children(); JToken Timestamp = records.First().SelectToken("record_timestamp"); int year = Timestamp.ToObject <DateTime>().Year; int count = 0; db.rankings.RemoveRange(db.rankings.Select(r => r).Where(r => r.player.gender == Gender && r.year == year)); foreach (var record in records) { JToken Fields = record.SelectToken("fields"); string PlayerName = Fields.Value <string>("player_name"); string CountryName = Fields.Value <string>("player_country"); int Points = Fields.Value <int>("player_points"); if (CountryName == "United Kingdom") { CountryName = "Great Britain"; } else if (CountryName.Equals("usa", StringComparison.InvariantCultureIgnoreCase) || CountryName == "United States") { CountryName = "USA"; } else if (CountryName == "Bosnia") { CountryName = "Bosnia And Herzegovina"; } else if (CountryName == "Slovak Republic") { CountryName = "Slovakia"; } else if (CountryName == "Chinese Taipei") { CountryName = "Taiwan"; } PlayerName = PlayerName.Trim(); PlayerName = Regex.Replace(PlayerName, " +", " "); ranking Ranking = new ranking(); Ranking.points = Points; Ranking.year = year; if (db.players.Select(p => p).Where(p => p.name == PlayerName && p.country.name == CountryName).SingleOrDefault() == null) { player Player = new player(); Player.name = PlayerName; Player.gender = Gender; if (db.countries.Select(c => c).Where(c => c.name == CountryName).SingleOrDefault() == null) { country Country = new country(); Country.name = CountryName; db.countries.Add(Country); db.SaveChanges(); } Player.country_id = db.countries.Select(c => c).Where(c => c.name == CountryName).Single().id; db.players.Add(Player); db.SaveChanges(); } Ranking.player_id = db.players.Select(p => p).Where(p => p.name == PlayerName && p.country.name == CountryName).Single().id; db.rankings.Add(Ranking); db.SaveChanges(); count++; } RankingsUpdate.ResetRank(year, Gender, db); db.SaveChanges(); loadRankingsModel.Message = "Loaded " + count + " rankings."; } } } } return(View(loadRankingsModel)); }
public ActionResult Edit(RankingsEditModel rankingsEditModel, int?id) { if (id == null) { return(BadRequest()); } rankingsEditModel.Player = db.Player.Find(id); if (rankingsEditModel.Player == null) { return(NotFound()); } int year = DateTime.Now.Year; for (int i = 2000; i <= year; year--) { rankingsEditModel.Years.Add(year); } if (Request.Method == "GET") { rankingsEditModel.Rankings = db.Ranking.Select(ranking => ranking).Where(r => r.PlayerId == id).OrderByDescending(r => r.Year).ToList(); foreach (Ranking item in rankingsEditModel.Rankings) { rankingsEditModel.Delete.Add(false); } } if (Request.Method == "POST") { if (ModelState.IsValid) { List <int> ChangedYears = new List <int>(); for (int i = rankingsEditModel.Delete.Count - 1; i >= 0; i--) { if (rankingsEditModel.Delete[i] == true) { if (db.Ranking.Find(rankingsEditModel.Rankings[i].Id) != null) { db.Ranking.Remove(db.Ranking.Find(rankingsEditModel.Rankings[i].Id)); } rankingsEditModel.Rankings.RemoveAt(i); } } foreach (Ranking Ranking in rankingsEditModel.Rankings) { Ranking.Player = db.Player.Find(id); Ranking NewRanking = new Ranking(); if (Ranking.Id > 0) { NewRanking = db.Ranking.Find(Ranking.Id); db.Entry(NewRanking).CurrentValues.SetValues(Ranking); db.Entry(NewRanking).State = EntityState.Modified; } if (Ranking.Id == 0) { db.Ranking.Add(Ranking); } ChangedYears.Add(Ranking.Year.Value); } if (ChangedYears.Distinct().Count() == ChangedYears.Count()) { db.SaveChanges(); foreach (int Year in ChangedYears) { RankingsUpdate.ResetRank(Year, rankingsEditModel.Player.Gender, db); } db.SaveChanges(); return(RedirectToAction("Details", "Players", new { id })); } else { var DuplicateYears = ChangedYears.GroupBy(y => y); var KeyList = ModelState.Keys.Select(k => k).Where(k => k.EndsWith(".Year")); foreach (var y in DuplicateYears) { if (y.Count() > 1) { foreach (var k in KeyList) { if (ModelState[k].AttemptedValue == y.Key.ToString()) { ModelState.AddModelError(k, "Maximum 1 ranking per year"); } } } } } } } if (rankingsEditModel.Delete.Count == rankingsEditModel.Rankings.Count) { Ranking Ranking = new Ranking(); Ranking.PlayerId = id.Value; rankingsEditModel.Rankings.Add(Ranking); } return(View(rankingsEditModel)); }