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(); } }
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 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(); } }