public ActionResult Upload(HttpPostedFileBase file, PlayersUploadModel playersUploadModel, int?id) { using (tennisDBEntities db = new tennisDBEntities()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } playersUploadModel.Player = db.players.Find(id.Value); if (playersUploadModel.Player == null) { return(HttpNotFound()); } if (playersUploadModel.Delete == true) { playersUploadModel.Player.photo = null; db.Entry(db.players.Find(id)).CurrentValues.SetValues(playersUploadModel.Player); db.Entry(db.players.Find(id)).State = EntityState.Modified; db.SaveChanges(); } if (file != null && file.ContentLength > 0) { Stream fs = file.InputStream; BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((Int32)fs.Length); playersUploadModel.Player.photo = bytes; db.Entry(db.players.Find(id)).CurrentValues.SetValues(playersUploadModel.Player); db.Entry(db.players.Find(id)).State = EntityState.Modified; db.SaveChanges(); } } return(View(playersUploadModel)); }
public ActionResult Edit(CountriesEditModel countriesEditModel, int?id) { using (tennisDBEntities db = new tennisDBEntities()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (Request.RequestType == "POST") { if (ModelState.IsValid) { country country = db.countries.Find(id); db.Entry(country).CurrentValues.SetValues(countriesEditModel.Country); db.Entry(country).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } countriesEditModel.Country = db.countries.Find(id); if (countriesEditModel.Country == null) { return(HttpNotFound()); } } return(View(countriesEditModel)); }
public ActionResult Create(PlayersCreateModel playersCreateModel) { using (tennisDBEntities db = new tennisDBEntities()) { if (Request.RequestType == "POST") { if (ModelState.IsValid) { playersCreateModel.Player.country = db.countries.Find(playersCreateModel.Player.country_id); db.players.Add(playersCreateModel.Player); db.SaveChanges(); return(RedirectToAction("Index")); } } int Year = DateTime.Now.Year; List <int?> Years = new List <int?>() { null }; for (int i = 40; i > 0; i--) { Years.Add(Year); Year--; } playersCreateModel.Years = new SelectList(Years); } return(View(playersCreateModel)); }
public ActionResult Create(CountriesCreateModel countriesCreateModel) { using (tennisDBEntities db = new tennisDBEntities()) { if (Request.RequestType == "POST") { if (ModelState.IsValid) { db.countries.Add(countriesCreateModel.Country); db.SaveChanges(); return(RedirectToAction("Index")); } } } return(View(countriesCreateModel)); }
public ActionResult Edit(PlayersEditModel playersEditModel, int?id) { using (tennisDBEntities db = new tennisDBEntities()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (Request.RequestType == "POST") { if (ModelState.IsValid) { player player = db.players.Find(id); db.Entry(player).CurrentValues.SetValues(playersEditModel.Player); db.Entry(player).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details", new { id })); } } playersEditModel.Player = db.players.Find(id.Value); if (playersEditModel.Player == null) { return(HttpNotFound()); } int Year = DateTime.Now.Year; List <int?> Years = new List <int?>() { null }; for (int i = 40; i > 0; i--) { Years.Add(Year); Year--; } playersEditModel.Years = new SelectList(Years); if (playersEditModel.Player.dob.HasValue) { playersEditModel.Dob = playersEditModel.Player.dob.Value.ToString("yyyy-MM-dd"); playersEditModel.Age = AgeFormatter.FormatAge(DateTime.Parse(playersEditModel.Dob)); } } return(View(playersEditModel)); }
public ActionResult Index(CountriesIndexModel countriesIndexModel) { using (tennisDBEntities db = new tennisDBEntities()) { if (countriesIndexModel.CountryToDelete != null) { country country = db.countries.Find(countriesIndexModel.CountryToDelete); if (country != null && country.players.Count == 0) { db.countries.Remove(country); db.SaveChanges(); } } countriesIndexModel.Countries = db.countries .Select(country => country) .Include("players") .OrderBy(c => c.name).ToList(); } return(View(countriesIndexModel)); }
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 Details(PlayersDetailsModel playersDetailsModel, int?id) { using (tennisDBEntities db = new tennisDBEntities()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } playersDetailsModel.Player = db.players.Include("country").Include("rankings").First(p => p.id == id); // .Find(id); if (playersDetailsModel.Player == null) { return(HttpNotFound()); } if (playersDetailsModel.DeletePlayer) { if (playersDetailsModel.Player.rankings.Count == 0 && playersDetailsModel.Player != null) { db.players.Remove(playersDetailsModel.Player); db.SaveChanges(); return(RedirectToAction("Index")); } } if (playersDetailsModel.Player.gender == "M") { playersDetailsModel.Gender = "Male"; } else if (playersDetailsModel.Player.gender == "F") { playersDetailsModel.Gender = "Female"; } if (playersDetailsModel.Player.handed != null) { if (playersDetailsModel.Player.handed == "L") { playersDetailsModel.Handed = "Left"; } else if (playersDetailsModel.Player.handed == "R") { playersDetailsModel.Handed = "Right"; } } if (playersDetailsModel.Player.height_feet != null && playersDetailsModel.Player.height_inches != null) { playersDetailsModel.Height = playersDetailsModel.Player.height_feet.ToString() + "\'" + playersDetailsModel.Player.height_inches.ToString() + "\""; } if (playersDetailsModel.Player.dob != null) { playersDetailsModel.Age = AgeFormatter.FormatAge(playersDetailsModel.Player.dob.Value); playersDetailsModel.Dob = playersDetailsModel.Player.dob.Value.ToLongDateString().TrimStart('0'); } } return(View(playersDetailsModel)); }
public ActionResult Edit(RankingsEditModel rankingsEditModel, int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } rankingsEditModel.Player = db.players.Find(id); if (rankingsEditModel.Player == null) { return(HttpNotFound()); } int year = DateTime.Now.Year; for (int i = 2000; i <= year; year--) { rankingsEditModel.Years.Add(year); } if (Request.RequestType == "GET") { rankingsEditModel.Rankings = db.rankings.Select(ranking => ranking).Where(r => r.player_id == id).OrderByDescending(r => r.year).ToList(); foreach (ranking item in rankingsEditModel.Rankings) { rankingsEditModel.Delete.Add(false); } } if (Request.RequestType == "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.rankings.Find(rankingsEditModel.Rankings[i].id) != null) { db.rankings.Remove(db.rankings.Find(rankingsEditModel.Rankings[i].id)); } rankingsEditModel.Rankings.RemoveAt(i); } } foreach (ranking Ranking in rankingsEditModel.Rankings) { Ranking.player = db.players.Find(id); ranking NewRanking = new ranking(); if (Ranking.id > 0) { NewRanking = db.rankings.Find(Ranking.id); db.Entry(NewRanking).CurrentValues.SetValues(Ranking); db.Entry(NewRanking).State = EntityState.Modified; } if (Ranking.id == 0) { db.rankings.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].Value.AttemptedValue == y.Key.ToString()) { ModelState.AddModelError(k, "Maximum 1 ranking per year"); } } } } } } } if (rankingsEditModel.Delete.Count == rankingsEditModel.Rankings.Count) { ranking Ranking = new ranking(); Ranking.player_id = id.Value; rankingsEditModel.Rankings.Add(Ranking); } return(View(rankingsEditModel)); }