public Response <bool> UpdateCampaighn(CampaighnEditModel model) { if ((model.StartLevel ?? 1) < 1 || (model.StartLevel ?? 1) > 100) { return(new Response <bool>(false, (int)ErrorEnum.Aplied, "Уровень должен быть в диапазоне 1-99")); } var transaction = _campaighnContext.StartTransaction(); var currentEntity = model.Id == null ? new Campaighn() : _campaighnContext.GetById((Guid)model.Id); var campaighnId = model.Id ?? Guid.NewGuid(); currentEntity.Name = model.Name; currentEntity.StartLevel = model.StartLevel ?? 1; IEnumerable <CampaighnRace> newRaces; if (!model.Id.Equals(null)) { var oldRaces = _campaighnRaceContext.GetList() .List() .Where(w => w.CampaighnId.Equals(model.Id) && !model.Subraces.Contains(w.SubraceId)); foreach (var oldRace in oldRaces) { _campaighnRaceContext.Delete(oldRace); } newRaces = model.Subraces.Where( w => !_campaighnRaceContext.GetList() .List() .Where(ww => ww.CampaighnId.Equals(model.Id)) .Select(ss => ss.SubraceId) .ToList() .Contains(w)).Select(s => new CampaighnRace { CampaighnId = campaighnId, SubraceId = s }); } else { newRaces = model.Subraces.Select(s => new CampaighnRace { CampaighnId = campaighnId, SubraceId = s }); } foreach (var newRace in newRaces) { _campaighnRaceContext.Upsert(newRace); } transaction.Commit(); return(new Response <bool>()); }
public Response <bool> UpdateAttribute(AttributeEditModel model) { if (model.Abbreviation.Equals("")) { return(new Response <bool>(false, (int)ErrorEnum.Aplied, "Сокращенное название обязательно для заполнения")); } if (model.Name.Equals("")) { return(new Response <bool>(false, (int)ErrorEnum.Aplied, "Наименование обязательно для заполнения")); } var currentEntity = model.Id == null ? new DictionaryAttribute { Id = Guid.NewGuid() } : _dictionaryAttrContext.GetById((Guid)model.Id); currentEntity.Abbreviation = model.Abbreviation.ToUpper().Replace(" ", "_"); currentEntity.AttributeFunction = AttributeFunction.GetFunction(model.AttributeFunction); currentEntity.AttributeTypeId = model.AttributeTypeId; currentEntity.Name = model.Name; var transaction = _dictionaryAttrContext.StartTransaction(); _dictionaryAttrContext.Upsert(currentEntity); transaction.Commit(); return(new Response <bool>()); }
public Response <bool> DeleteEntity <T>(Guid id, IEntityDb <T> context) where T : class, IEntity { try { var transaction = context.StartTransaction(); context.Delete(id); transaction.Commit(); } catch (Exception) { return(new Response <bool>(true, (int)ErrorEnum.HasReferences, "Удаление невозможно")); } return(new Response <bool>(true)); }
public Response <bool> UpdateRace(RaceEditModel updateModel) { var transaction = _raceContext.StartTransaction(); var id = updateModel.Id ?? Guid.NewGuid(); var currentEntity = updateModel.Id == null ? new Race { Id = id } : _raceContext.GetById((Guid)updateModel.Id); currentEntity.Name = updateModel.Name; currentEntity.Description = updateModel.Description ?? ""; _raceContext.Upsert(currentEntity); updateModel.Attributes.SaveAttributes(id, _raceAttributeContext); transaction.Commit(); return(new Response <bool>(true)); }