public ActionResult SaveClimber(long compId, long regionId, String divId, ClimberApplication model) { var comp = db.Competitions.Find(compId); ViewBag.Comp = comp; ViewBag.DivId = divId; ViewBag.RegionId = regionId; ViewBag.DisplayValidation = true; ViewBag.AllowEditName = comp.AllowedToAdd(User.GetDbUser(db, false)); if (!ModelState.IsValid) { return(PartialView("Climber", model)); } foreach (var s in model.Validate(User.GetDbUser(db, false), db, ViewBag.Comp)) { ModelState.AddModelError(String.Empty, s); } if (!ModelState.IsValid) { return(PartialView("Climber", model)); } return(PartialView("ConfirmClimber", model)); }
private String[] SaveClimberApp(UserProfileModel user, long compId, ClimberApplication model, bool saveChanges) { if (model == null) { return new String[] { "Модель не загружена" } } ; var comp = db.Competitions.Find(compId); if (comp == null) { return new String[] { "Неверные соревнования" } } ; String[] empty = new String[0]; PersonModel personToApply; Comp_ClimberTeam existingApp; var retVal = model.Validate(user, db, comp, out personToApply, out existingApp); if (retVal.Length > 0) { return(retVal); } //если надо улалить заявку или заменить участника if (model.IsDel || (existingApp != null && (personToApply == null || personToApply.Iid != existingApp.Climber.PersonId))) { if (model.IsDel && existingApp == null) { return(empty); } //удаляем текущую заявку RemoveApp(existingApp); if (model.IsDel) { //если надо только удалить то выйдем из функции if (saveChanges) { db.SaveChanges(); } return(empty); } existingApp = null; } //создадим заявку var climber = existingApp != null ? existingApp.Climber : null; if (climber == null) { //добавим человека if (personToApply == null) { var newIid = GenerateLicenseId(model.TeamId.Value); personToApply = new PersonModel { Surname = model.Surname, Name = model.Name, GenderProperty = model.GenderP.Value, DateOfBirth = new DateTime(model.YearOfBirth.Value, 01, 05), Competitions = new List <Comp_CompetitiorRegistrationModel>(), Coach = String.Empty, Email = String.Empty, HomeAddress = String.Empty, Patronymic = String.Empty }; if (newIid != null) { personToApply.Iid = newIid.Value; } db.People.Add(personToApply); } //найдем его заявку на данные соревы (если заявки нет - то создадим) climber = personToApply.Competitions.FirstOrDefault(c => c.CompId == comp.Iid); if (climber == null) { climber = new Comp_CompetitiorRegistrationModel { CompId = comp.Iid, Teams = new List <Comp_ClimberTeam>() }; personToApply.Competitions.Add(climber); } } //обновим заявку climber.Boulder = model.Boulder; climber.GroupId = model.GroupId; climber.Lead = model.Lead; climber.Speed = model.Speed; climber.Qf = model.Qf.Value; //добавим команды if (existingApp == null) { int pos = 1; for (; climber.Teams.Count(pt => pt.RegionOrder == pos) > 0; pos++) { ; } climber.Teams.Add(new Comp_ClimberTeam { RegionId = model.TeamId.Value, RegionOrder = pos }); } if (saveChanges) { db.SaveChanges(); } return(empty); }