public IHttpActionResult Get(string id, string city, string name) { DateTime d; if (!DateTime.TryParse(id, out d)) return BadRequest("parameter 1 is not valid"); using (var ctx = new CupDbContext()) { var l = ctx.Cups.Where(x => x.Name == name && x.City == city && x.Start.Value.Year == d.Year && x.Start.Value.Month == d.Month && x.Start.Value.Day == d.Day).Select(x => new { x.Name, Date = x.Start.HasValue ? x.Start.Value.Day.ToString() : "", Month = x.Start.HasValue ? x.Start.Value.Month.ToString() : "", x.Starttime, x.Endtime, Location = x.City, x.Organizer, x.NumberOfTeams, x.Id, x.Generated, QuarterFinals = x.QuarterFinals.Select(y => new { HomeTeamGroupId = y.Group1.Id, VisitorTeamGroupId = y.Group2.Id, y.Name, y.Description, HomeColor = y.Group1.Color, VisitorColor = y.Group2.Color, y.ScoreHome, y.ScoreVistor }), SemiFinals = x.SemiFinals.Select(y => new { HomeTeamGroupId = y.QuarterFinal1.Id, VisitorTeamGroupId = y.QuarterFinal2.Id }), Finals = x.Finals.Select(y => new { HomeTeamGroupId = y.SemiFinal1.Id, VisitorTeamGroupId = y.SemiFinal2.Id }), Groups = x.Groups.Select(y => new { y.Id, y.Name, y.Color, Games = y.GroupGames.Select(g => new { HomeTeam = g.TeamHome.Name, VisitorTeam = g.TeamVisitor.Name, g.ScoreHome, g.ScoreVistor, g.Id }), Teams = y.Teams.Select(q => new { q.Id, q.Name, PlayedGames = q.GroupGames.Count(a => a.End > DateTime.Now && (a.TeamHome.Id == q.Id || a.TeamVisitor.Id == q.Id)), Points = q.GroupGames.Where(a => a.End > DateTime.Now).Count(a => a.ScoreHome > a.ScoreVistor && a.TeamHome.Id == q.Id) * 3 + q.GroupGames.Where(a => a.End > DateTime.Now).Count(a => a.ScoreVistor > a.ScoreHome && a.TeamVisitor.Id == q.Id) * 3 + q.GroupGames.Where(a => a.End > DateTime.Now).Count(a => a.ScoreVistor == a.ScoreHome) * 1, Goals = q.GroupGames.Any() ? q.GroupGames.Where(a => a.TeamHome.Id == q.Id).Sum(a => a.ScoreHome) + q.GroupGames.Where(a => a.TeamVisitor.Id == q.Id).Sum(a => a.ScoreVistor) : 0, BackGoals = q.GroupGames.Any() ? q.GroupGames.Where(a => a.TeamHome.Id == q.Id).Sum(a => a.ScoreVistor) + q.GroupGames.Where(a => a.TeamVisitor.Id == q.Id).Sum(a => a.ScoreHome) : 0, }) .OrderBy(a => a.Points).ThenBy(a => a.Goals) }), Teams = x.Teams.Where(q => q.Group == null).Select(q => new { q.Id, q.Name }) }).FirstOrDefault(); if (l == null) return BadRequest(); return Ok(l); //return Ok(o); } }
private dynamic GetTeamsData(int id) { using (var ctx = new CupDbContext()) { var teamGames = ctx.GroupGames.Where(x => x.TeamHome.Id == id || x.TeamVisitor.Id == id).ToList(); var points = ((teamGames.Count(a => a.ScoreHome > a.ScoreVistor && a.TeamHome.Id == id) + teamGames.Count(a => a.ScoreVistor > a.ScoreHome && a.TeamVisitor.Id == id)) * 3) + (teamGames.Count(a => a.ScoreVistor == a.ScoreHome) * 1); var goals = teamGames.Where(x => x.TeamHome.Id == id).Sum(x => x.ScoreHome) + teamGames.Where(x => x.TeamVisitor.Id == id).Sum(x => x.ScoreVistor); var backGoals = teamGames.Where(x => x.TeamHome.Id == id).Sum(x => x.ScoreVistor) + teamGames.Where(x => x.TeamVisitor.Id == id).Sum(x => x.ScoreHome); return new { Points = points, Goals = goals, BackGoals = backGoals }; } }
public void Post(Cup c) { if (c == null) return; using (var ctx = new CupDbContext()) { //DateTime d; //if (DateTime.TryParse(c.Date + " " + c.Starttime, out d)) // c.Start = d; //if (DateTime.TryParse(c.Date + " " + c.Endtime, out d)) // c.End = d; ctx.Cups.Add(c); if (c.Start != null && c.Start.Value.ToShortTimeString() != "00:00") c.Starttime = c.Start.Value.ToShortTimeString(); if (c.End != null && c.End.Value.ToShortTimeString() != "00:00") c.Endtime = c.End.Value.ToShortTimeString(); c.User = ctx.Users.FirstOrDefault(x=>x.Id==c.User.Id); if (c.AutoGenerateCup) { for (var i = 1; i < (c.NumberOfTeams <= 10 ? 2 : c.NumberOfTeams > 20 ? 8 : 4) + 1; i++) { var l = GetLetter(i); ctx.Groups.Add(new Group { Cup = c, Name = "Grupp " + l.Item1, Color = l.Item2 }); } var quarterfinal1 = new QuarterFinal { Cup = c }; var quarterfinal2 = new QuarterFinal { Cup = c }; var quarterfinal3 = new QuarterFinal { Cup = c }; var quarterfinal4 = new QuarterFinal { Cup = c }; if (c.Groups.Count == 2) { var group1 = c.Groups.OrderBy(x => x.Id).First(); var group2 = c.Groups.OrderBy(x => x.Id).Last(); quarterfinal1.Description = "2:an i " + group2.Name + " - 3:an i " + group1.Name; quarterfinal1.Name = "Kvartsfinal 1"; quarterfinal1.Group1 = group2; //2:an i grupp 2 quarterfinal1.Group2 = group1; //3:an i grupp 1 quarterfinal2.Description = "2:an i " + group1.Name + " - 3:an i " + group2.Name; quarterfinal2.Name = "Kvartsfinal 2"; quarterfinal2.Group1 = group1; //2:an i grupp 1 quarterfinal2.Group2 = group2; //3:an i grupp 2 quarterfinal3.Description = "1:an i " + group1.Name + " - 4:an i " + group2.Name; quarterfinal3.Name = "Kvartsfinal 3"; quarterfinal3.Group1 = group1; //1:an i grupp 1 quarterfinal3.Group2 = group2; //4:an i grupp 2 quarterfinal4.Description = "1:an i " + group2.Name + " - 4:an i " + group1.Name; quarterfinal4.Name = "Kvartsfinal 4"; quarterfinal4.Group1 = group2; //1:an i grupp 2 quarterfinal4.Group2 = group1; //4:an i grupp 1 } if (c.Groups.Count == 4) { var group1 = c.Groups.OrderBy(x => x.Id).First(); var group2 = c.Groups.OrderBy(x => x.Id).Skip(1).First(); var group3 = c.Groups.OrderBy(x => x.Id).Skip(2).First(); var group4 = c.Groups.OrderBy(x => x.Id).Last(); quarterfinal1.Description = "1:an i " + group1.Name + " - 2:an i " + group2.Name; quarterfinal1.Name = "Kvartsfinal 1"; quarterfinal1.Group1 = group1; //1:an i grupp 1 quarterfinal1.Group2 = group2; //2:an i grupp 2 quarterfinal2.Description = "1:an i " + group2.Name + " - 2:an i " + group1.Name; quarterfinal3.Name = "Kvartsfinal 2"; quarterfinal2.Group1 = group2; //1:an i grupp 2 quarterfinal2.Group2 = group1; //2:an i grupp 1 quarterfinal3.Description = "1:an i " + group3.Name + " - 2:an i " + group4.Name; quarterfinal3.Name = "Kvartsfinal 3"; quarterfinal3.Group1 = group3; //1:an i grupp 3 quarterfinal3.Group2 = group4; //2:an i grupp 4 quarterfinal4.Description = "1:an i " + group4.Name + " - 2:an i " + group3.Name; quarterfinal4.Name = "Kvartsfinal 4"; quarterfinal4.Group1 = group4; //1:an i grupp 4 quarterfinal4.Group2 = group3; //2:an i grupp 3 } if (c.Groups.Count == 8) { var group1 = c.Groups.OrderBy(x => x.Id).First(); var group2 = c.Groups.OrderBy(x => x.Id).Skip(1).First(); var group3 = c.Groups.OrderBy(x => x.Id).Skip(2).First(); var group4 = c.Groups.OrderBy(x => x.Id).Skip(3).First(); var group5 = c.Groups.OrderBy(x => x.Id).Skip(4).First(); var group6 = c.Groups.OrderBy(x => x.Id).Skip(5).First(); var group7 = c.Groups.OrderBy(x => x.Id).Skip(6).First(); var group8 = c.Groups.OrderBy(x => x.Id).Last(); quarterfinal1.Description = "1:an i " + group1.Name + " - 1:an i " + group2.Name; quarterfinal1.Name = "Kvartsfinal 1"; quarterfinal1.Group1 = group1; //1:an i grupp 1 quarterfinal1.Group2 = group2; //1:an i grupp 2 quarterfinal2.Description = "1:an i " + group3.Name + " - 1:an i " + group4.Name; quarterfinal2.Name = "Kvartsfinal 2"; quarterfinal2.Group1 = group3; //1:an i grupp 3 quarterfinal2.Group2 = group4; //1:an i grupp 4 quarterfinal3.Description = "1:an i " + group5.Name + " - 1:an i " + group6.Name; quarterfinal3.Name = "Kvartsfinal 3"; quarterfinal3.Group1 = group5; //1:an i grupp 5 quarterfinal3.Group2 = group6; //1:an i grupp 6 quarterfinal1.Description = "1:an i " + group7.Name + " - 1:an i " + group8.Name; quarterfinal1.Name = "Kvartsfinal 4"; quarterfinal4.Group1 = group7; //1:an i grupp 7 quarterfinal4.Group2 = group8; //1:an i grupp 8 } ctx.QuarterFinals.Add(quarterfinal1); ctx.QuarterFinals.Add(quarterfinal2); ctx.QuarterFinals.Add(quarterfinal3); ctx.QuarterFinals.Add(quarterfinal4); var semifinal1 = new SemiFinal { Cup = c, QuarterFinal1 = quarterfinal1, QuarterFinal2 = quarterfinal2, Description = "Vinnare " + quarterfinal1.Name + " - Vinnare " + quarterfinal2.Name, Name = "Semifinal 1" }; var semifinal2 = new SemiFinal { Cup = c, QuarterFinal1 = quarterfinal3, QuarterFinal2 = quarterfinal4, Description = "Vinnare " + quarterfinal3.Name + " - Vinnare " + quarterfinal4.Name, Name = "Semifinal 2" }; ctx.SemiFinals.Add(semifinal1); ctx.SemiFinals.Add(semifinal2); var final1 = new Final { Cup = c, SemiFinal1 = semifinal1, SemiFinal2 = semifinal2, Description = "Vinnare " + semifinal1.Name + " - vinnare " + semifinal2.Name, Name = "Final" }; var final2 = new Final { Cup = c, SemiFinal1 = semifinal2, SemiFinal2 = semifinal1, Description = "Förloare " + semifinal1.Name + " - förlorare " + semifinal2.Name, Name = "Match" + " om 3:e pris" }; ctx.Finals.Add(final1); ctx.Finals.Add(final2); } ctx.SaveChanges(); } }
public void Delete(int id) { using (var ctx = new CupDbContext()) { var c = ctx.Cups.Find(id); if (c == null) return; ctx.Cups.Remove(c); ctx.SaveChanges(); } }
static void CreateMatches(CupDbContext ctx, Group @group, Cup cup) { int r; var n = group.Teams.Count; var nn = n + (n & 1); var m = nn >> 1; for (r = 0; r < nn - 1; r++) { for (var s = 0; s < m - 1; s++) ctx.GroupGames.Add(new GroupGame { Start = DateTime.Now, End = DateTime.Now, Group = group, Cup = cup, TeamHome = group.Teams.ElementAt(Mod(s + r, nn - 1)), TeamVisitor = group.Teams.ElementAt(Mod(-1 - s + r, nn - 1)) }); //Console.Write((s > 0 ? " ," : "") + Mod(s + r, nn - 1) + " - " + Mod(-1 - s + r, nn - 1)); if ((n & 1) == 1) { } //Console.Write(" || står över " + Mod(m - 1 + r, nn - 1)); else ctx.GroupGames.Add(new GroupGame { Start = DateTime.Now, End = DateTime.Now, Group = group, Cup = cup, TeamHome = group.Teams.ElementAt(Mod(m - 1 + r, nn - 1)), TeamVisitor = group.Teams.ElementAt(nn - 1) }); //Console.Write(" , " + Mod(m - 1 + r, nn - 1) + " - " + (nn - 1)); } }
public void Put(int id, Cup value, bool generateteamstogroups = false) { using (var ctx = new CupDbContext()) { var c = ctx.Cups.Find(id); if (c == null) return; if (generateteamstogroups) { var numberofTeamsPerGroup = 0; for (int i = 3; i < 24; i++) { if (c.NumberOfTeams % i != 0) continue; numberofTeamsPerGroup = i; break; } foreach (var group in c.Groups) { int i = 0; foreach (var team in c.Teams.Where(x => x.Group == null)) { if (i == numberofTeamsPerGroup) break; if (team.Group != null) continue; team.Group = group; group.Teams.Add(team); i++; } } foreach (var group in c.Groups.ToList()) { CreateMatches(ctx, group, c); } c.Generated = true; } else { c.Name = value.Name; c.BaseColor = value.BaseColor; c.NumberOfTeams = value.NumberOfTeams; } ctx.SaveChanges(); } }