public ActionResult SaveData(CompetitionEditModel model) { var dbUser = User.GetDbUser(db); ViewBag.Regions = new SelectList(proc.GetRegionsToCreateCompetition(dbUser), "Iid", "Name", model.RegionId); if (model.Iid > 0) { var oldComp = db.Competitions.Find(model.Iid); if (oldComp == null || !dbUser.IsInRoleRegion(RoleEnum.Admin, oldComp.Region.IidParent)) { ModelState.AddModelError(String.Empty, "У вас нет прав на редактирование этих соревнований"); } } if (!ModelState.IsValid) { return(View("Edit", model)); } long newIid; var saveResults = proc.SaveCompetition(model, out newIid); if (saveResults.Length > 0) { foreach (var s in saveResults) { ModelState.AddModelError(String.Empty, s); } return(View("Edit", model)); } else { ViewBag.Message = "Данные сохранены"; return(RedirectToAction("Edit", new { id = newIid, scs = true })); } }
public String[] SaveCompetition(CompetitionEditModel model, out long newIid) { newIid = model.Iid; List <String> errors = new List <string>(); CompetitionModel comp; if (model.Iid > 0) { comp = db.Competitions.Find(model.Iid); foreach (var agr in model.ageGroups.Where(g => !g.Confirmed)) { var oldGRREg = comp.AgeGroups.FirstOrDefault(g => g.AgeGroupId == agr.GroupId); if (oldGRREg == null) { continue; } if (oldGRREg.Competitiors.Count > 0) { errors.Add(String.Format("Нельзя удалить группу {0}. В ней есть заявленные участники", oldGRREg.AgeGroup.FullName)); } else { db.CompetitionAgeGroups.Remove(oldGRREg); } } } else { comp = new CompetitionModel { AgeGroups = new List <Comp_AgeGroupModel>(), Parameters = new List <CompetitionParameterModel>() }; if (comp.AgeGroups == null) { comp.AgeGroups = new List <Comp_AgeGroupModel>(); } db.Competitions.Add(comp); } var reg = db.Regions.Find(model.RegionId); if (reg == null) { errors.Add(String.Format("Регион с ID={0} не найден", model.RegionId)); } comp.ApplicationsEnd = model.ApplicationsEndDate.Value; comp.ApplicationsEditEnd = model.ApplicationsEditEndDate.Value; comp.AllowLateAppl = model.AllowLateApps; comp.SignApplications = model.RequireSignature; comp.AllowMultipleTeams = model.AllowMultipleTeams; comp.Boulder = model.Boulder; comp.End = model.StopDate.Value; comp.Lead = model.Lead; comp.Name = model.FullName; comp.RegionId = model.RegionId.Value; comp.ShortName = model.ShortName; comp.Speed = model.Speed; comp.Start = model.StartDate.Value; comp.Rank = (reg.IidParent == null) ? CompetitionLevel.National : CompetitionLevel.Regional; foreach (var agr in model.ageGroups.Where(a => a.Confirmed)) { if (comp.AgeGroups.FirstOrDefault(g => g.AgeGroupId == agr.GroupId) == null) { comp.AgeGroups.Add(new Comp_AgeGroupModel() { AgeGroupId = agr.GroupId }); } } if (errors.Count > 0) { return(errors.ToArray()); } try { db.SaveChanges(); newIid = comp.Iid; } catch (Exception ex) { return(new String[] { String.Format("Ошибка сохранения:{0}{1}", Environment.NewLine, ex.ToString()) }); } return(new String[0]); }