예제 #1
0
        public void AddItemToGroup(string groupName, string itemName)
        {
            if (groupName == itemName)
            {
                throw new ArgumentException("Cannot add a group to itself.");
            }
            IGroupModel parent = _groupStore.GetOne(x => x.Name == groupName);

            if (parent == null)
            {
                throw new ArgumentException($"Can't add item to group named {groupName} since it doesn't exist.");
            }
            ITextModel  childText  = _textStore.GetOne(x => x.Name == itemName);
            IGroupModel childGroup = _groupStore.GetOne(x => x.Name == itemName);

            if (childText == null)
            {
                if (childGroup == null)
                {
                    throw new ArgumentException($"Can't add item named {itemName} to group since it doesn't exist.");
                }
                if (_groupStore.Contains(childGroup, parent))
                {
                    throw new ArgumentException("Can't add a group as a child to its own parent or a parent of its parent or ...");
                }
                if (_groupStore.Contains(parent, childGroup))
                {
                    throw new ArgumentException("Cannot add item to group since it is already a member of that group.");
                }
                parent.Add(childGroup);
                _groupStore.AddItem(parent, childGroup);
            }
            else
            {
                if (childGroup != null)
                {
                    throw new ArgumentException("The database has a text and group with the same name. That shouldn't happen.");
                }
                if (_groupStore.Contains(parent, childText))
                {
                    throw new ArgumentException("Cannot add item to group since it is already a member of that group.");
                }
                parent.Add(childText);
                _groupStore.AddItem(parent, childText);
            }
        }
예제 #2
0
        public IGroupModel GetOne(Expression <Func <Grouping, bool> > criteria)
        {
            Grouping group = _db.Groupings.FirstOrDefault(criteria);

            if (group == null)
            {
                return(null);
            }
            IGroupModel      output  = _modelFactory.GetGroupModel(group.Name, UniversalConstants.CountSize);
            IQueryable <int> TextIds = _db.Texts.Join(
                _db.Text_Grouping,
                text => text.Id,
                text_grouping => text_grouping.TextId,
                (text, text_grouping) => new { TextId = text_grouping.TextId, GroupingId = text_grouping.GroupingId }
                ).Where(x => x.GroupingId == group.Id)
                                       .Select(x => (int)x.TextId);

            foreach (Text text in _db.Texts.Where(x => TextIds.Contains((int)x.Id)))
            {
                ITextModel textModel = _textStore.GetOne(x => x.Id == text.Id);
                if (textModel == null)
                {
                    throw new ArgumentException("Cannot get group since it contains a reference to a nonexistant text.");
                }
                output.Add(textModel);
            }
            foreach (Grouping_Grouping groupGroup in _db.Grouping_Grouping.Where(x => x.ParentId == group.Id))
            {
                IGroupModel groupModel = GetOne(x => x.Id == groupGroup.ChildId);
                if (groupModel == null)
                {
                    throw new ArgumentException("Cannot get group since it contains a reference to a nonexistant child group.");
                }
                output.Add(groupModel);
            }
            return(output);
        }
 public void AddGroupToItself()
 {
     groupOne.Add(groupOne);
 }