static ApiAgeGroup GetApiAgeGroup(AgeGroupOnCompetition g) { return(new ApiAgeGroup { AgeGroupInCompId = g.Iid, FullName = g.AgeGroup.FullName, Name = g.AgeGroup.ShortName, YearOld = g.Competition.CompetitionYear - g.AgeGroup.AgeOld, YearYoung = g.Competition.CompetitionYear - g.AgeGroup.AgeYoung, Gender = g.AgeGroup.Gender }); }
public JsonResult <ApiAgeGroup[]> SaveAgeGroups(String id, string password, Boolean refreshAll, IEnumerable <ApiAgeGroup> ageGroupsToSave) { if (!this.CheckCompetitionsPassword(id, password)) { return(Json(new ApiAgeGroup[0])); } var comp = Context.Competitions.FirstOrDefault(c => c.Iid == id); if (ageGroupsToSave == null || comp == null || ageGroupsToSave.Count() < 1) { return(Json(new ApiAgeGroup[0])); } HashSet <String> passed = new HashSet <string>(); foreach (var ag in ageGroupsToSave) { var localAG = string.IsNullOrEmpty(ag.AgeGroupInCompId) ? null : Context.AgeGroupsOnCompetition.FirstOrDefault(g => g.Iid == ag.AgeGroupInCompId && g.CompId == id); if (localAG != null) { localAG.AgeGroup.ShortName = ag.Name ?? string.Empty; passed.Add(localAG.Iid); continue; } int ageOld = comp.CompetitionYear - ag.YearOld; int ageYoung = comp.CompetitionYear - ag.YearYoung; var genderC = ag.Gender.GetFirstLetter(); var globalAG = Context.AgeGroups.FirstOrDefault(g => g.AgeOld == ageOld && g.AgeYoung == ageYoung && g.GenderC == genderC); if (globalAG == null) { globalAG = Context.AgeGroups.FirstOrDefault(g => g.FullName == (string.IsNullOrEmpty(ag.FullName) ? ag.Name : ag.FullName)); } if (globalAG == null) { globalAG = Context.AgeGroups.Add(new ClimbingEntities.AgeGroups.AgeGroup(Context) { AgeOld = ageOld, AgeYoung = ageYoung, Gender = ag.Gender, ShortName = ag.Name, FullName = string.IsNullOrEmpty(ag.FullName) ? ag.Name : ag.FullName, AgeGroupAppearances = new List <AgeGroupOnCompetition>() }); } else { globalAG.ShortName = ag.Name ?? string.Empty; } localAG = globalAG.AgeGroupAppearances.FirstOrDefault(g => g.CompId == id); if (localAG == null) { globalAG.AgeGroupAppearances.Add(localAG = new AgeGroupOnCompetition(Context) { AgeGroup = globalAG, Competition = comp }); } ag.AgeGroupInCompId = localAG.Iid; passed.Add(localAG.Iid); } if (refreshAll) { foreach (var lAG in Context.AgeGroupsOnCompetition.Where(g => g.CompId == id && !passed.Contains(g.Iid)).ToList()) { lAG.RemoveObject(Context); } } Context.SaveChanges(); return(Json(ageGroupsToSave.ToArray())); }