internal ComparisonForUser(GroupComparison groupComparison) : this(groupComparison.Title, groupComparison.Description) { Id = groupComparison.Id; SortInfo = new SortInfo(groupComparison); FillAdditionalInfo(groupComparison.AdditionalInfo); }
private GroupComparison GetOrCreateGroupComparison(ComparisonForUser comparisonForUser, StudyLanguageContext c) { GroupComparison groupComparison = c.GroupComparison.FirstOrDefault(e => e.Title == comparisonForUser.Title && e.LanguageId == _languageId); if (groupComparison == null) { groupComparison = new GroupComparison { Title = comparisonForUser.Title, LanguageId = _languageId }; c.GroupComparison.Add(groupComparison); } groupComparison.AdditionalInfo = comparisonForUser.GetAdditionalInfo(); groupComparison.Description = comparisonForUser.Description; groupComparison.LastModified = DateTime.Now; c.SaveChanges(); return(groupComparison); }
public ComparisonForUser GetOrCreate(ComparisonForUser comparisonForUser) { if (!comparisonForUser.IsValid()) { return(null); } bool isSuccess = true; ComparisonForUser result = null; Adapter.ActionByContext(c => { GroupComparison groupComparison = GetOrCreateGroupComparison(comparisonForUser, c); if (IdValidator.IsInvalid(groupComparison.Id)) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "ComparisonsQuery.GetOrCreate не удалось создать! Название: {0}, описание: {1}", comparisonForUser.Title, comparisonForUser.Description); isSuccess = false; return; } result = new ComparisonForUser(groupComparison); int orderItem = 1; foreach (ComparisonItemForUser comparisonItemForUser in comparisonForUser.Items) { ComparisonItem comparisonItem = GetOrCreateComparisonItem(comparisonItemForUser, groupComparison.Id, orderItem++, c); if (IdValidator.IsInvalid(comparisonItem.Id)) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "ComparisonsQuery.GetOrCreate не удалось создать пункт для сравнения! " + "Id сравнения: {0}, название {1}, перевод названия {2}, описание {3}", groupComparison.Id, comparisonItemForUser.Title, comparisonItemForUser.TitleTranslated, comparisonItemForUser.Description); isSuccess = false; continue; } var newComparisonItemForUser = new ComparisonItemForUser(comparisonItem); result.AddItem(newComparisonItemForUser); int orderRule = 1; foreach (ComparisonRuleForUser comparisonRuleForUser in comparisonItemForUser.Rules) { ComparisonRule comparisonRule = GetOrCreateComparisonRule(comparisonRuleForUser, comparisonItem.Id, orderRule++, c); long ruleId = comparisonRule.Id; if (IdValidator.IsInvalid(ruleId)) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "ComparisonsQuery.GetOrCreate не удалось создать правило для сравнения! " + "Id пункта сравнения: {0}, описание {1}", comparisonItem.Id, comparisonRule.Description); isSuccess = false; continue; } var newComparisonRuleForUser = new ComparisonRuleForUser(comparisonRule); newComparisonItemForUser.AddRule(newComparisonRuleForUser); isSuccess = EnumerableValidator.IsNotEmpty(comparisonRuleForUser.Examples) && CreateExamples(comparisonRuleForUser.Examples, newComparisonRuleForUser, c); } } if (isSuccess) { //удалить пункты, правила, примеры, которые не были переданы в этот раз DeleteOldInfos(c, result); } }); return(isSuccess && result != null && result.IsValid() ? result : null); }
internal SortInfo(GroupComparison groupComparison) { LastModified = groupComparison.LastModified; Rating = groupComparison.Rating ?? DEFAULT_RATING; Name = groupComparison.Title; }