public GroupSurveyOptionViewModel(Option option) { if (option != null) { Id = option.Id; Title = option.Title; Description = option.Description; VotesCount = option.SurveyBulletins.Count; VotesCountString = DeclinationService.OfNumber(VotesCount, "голос", "голоса", "голосов"); Position = option.Position; } }
public Survey UpdateSurvey(Survey survey, SurveyData data, Guid? userId) { if (survey.State != (byte)ContentState.Draft && survey.State != (byte)ContentState.Premoderated) throw new BusinessLogicException("Данный опрос нельзя изменять"); if (userId.HasValue) { if (survey.AuthorId != userId) throw new BusinessLogicException("Вы не являетесь автором опроса"); GroupService.UserInGroup(userId.Value, survey.Group, true); } if (data.VariantsCount >= data.Options.Count) throw new BusinessLogicException("Разрешено выбирать слишком много вариантов"); survey.Title = data.Title; survey.Text = data.Text; survey.IsPrivate = data.IsPrivate; survey.Duration = data.Duration; survey.VariantsCount = data.VariantsCount; survey.Tags.Clear(); foreach (var tag in TagsHelper.ConvertStringToTagList(data.Tags, survey.GroupId)) survey.Tags.Add(tag); var optionsForDelete = survey.Options.ToList(); foreach (var option in optionsForDelete) DataService.PerThread.OptionSet.DeleteObject(option); var options = new List<Option>(); byte optPos = 0; foreach (var o in data.Options) { var option = new Option { Position = optPos, Title = o.Title, Description = o.Description, SurveyId = survey.Id }; DataService.PerThread.OptionSet.AddObject(option); options.Add(option); optPos++; } survey.Options.Clear(); foreach (var option in options) survey.Options.Add(option); DataService.PerThread.SaveChanges(); return survey; }
public Survey CreateSurvey(SurveyData data, Guid? userId) { var group = DataService.PerThread.GroupSet.SingleOrDefault(x => x.Id == data.GroupId); if (group == null) throw new BusinessLogicException("Указан неверный идентификатор группы"); if (userId.HasValue) GroupService.UserInGroup(userId.Value, group, true); if (data.VariantsCount >= data.Options.Count) throw new BusinessLogicException("Разрешено выбирать слишком много вариантов"); var survey = new Survey { AuthorId = userId, Title = data.Title, Text = data.Text, IsPrivate = data.IsPrivate, Tags = TagsHelper.ConvertStringToTagList(data.Tags, data.GroupId), GroupId = data.GroupId, Duration = data.Duration, VariantsCount = data.VariantsCount, CreationDate = DateTime.Now, State = data.IsDraft ? (byte)ContentState.Draft : (byte)ContentState.Premoderated, HasOpenProtocol = data.HasOpenProtocol }; var options = new List<Option>(); byte optPos = 0; foreach (var o in data.Options) { var option = new Option { Position = optPos, Title = o.Title, Description = o.Description, SurveyId = survey.Id }; options.Add(option); optPos++; } survey.Options = options; DataService.PerThread.ContentSet.AddObject(survey); DataService.PerThread.SaveChanges(); return survey; }