public void RemoveItem(IGroupModel model, ITextOrGroupModel item) { string parentName = model.GetName(); Grouping parent = _db.Groupings.FirstOrDefault(x => x.Name == parentName); if (parent == null) { throw new ArgumentException("Cannot add to group since the group does not exist."); } string childName = item.GetName(); Text childText = _db.Texts.FirstOrDefault(x => x.Name == childName); Grouping childGroup = _db.Groupings.FirstOrDefault(x => x.Name == childName); if (childText == null) { if (childGroup == null) { throw new ArgumentException("Cannot remove item from group since the item does not exist."); } Grouping_Grouping association = _db.Grouping_Grouping.FirstOrDefault(x => x.ParentId == parent.Id && x.ChildId == childGroup.Id); if (association == null) { throw new ArgumentException("Cannot remove item from group since it is not a member of that group."); } _db.Grouping_Grouping.Remove(association); _db.SaveChanges(); } else { if (childGroup != null) { throw new InvalidOperationException("The database contains both a text and a group with the same name. That's not good."); } Text_Grouping assocation = _db.Text_Grouping.FirstOrDefault(x => x.TextId == childText.Id && x.GroupingId == parent.Id); if (assocation == null) { throw new ArgumentException("Cannot remove item from group since it is not a member of that group."); } _db.Text_Grouping.Remove(assocation); _db.SaveChanges(); } }
public void AddItem(IGroupModel model, ITextOrGroupModel item) { string parentName = model.GetName(); Grouping parent = _db.Groupings.FirstOrDefault(x => x.Name == parentName); if (parent == null) { throw new ArgumentException("Cannot add to group since the group does not exist."); } string childName = item.GetName(); Text childText = _db.Texts.FirstOrDefault(x => x.Name == childName); Grouping childGroup = _db.Groupings.FirstOrDefault(x => x.Name == childName); if (childText == null) { if (childGroup == null) { throw new ArgumentException("Cannot add item to group since the item does not exist."); } if (childGroup.Id == parent.Id) { throw new ArgumentException("Cannot add a group to itself."); } if (item is IGroupModel) { if (Contains((IGroupModel)item, model)) { throw new ArgumentException("Cannot add item to its own child or a child of its child or ..."); } } else { throw new ArgumentException("The item's name corresponds to a group in the db, but the item is not a group model. Something is wrong."); } if (_db.Grouping_Grouping.Any(x => x.ParentId == parent.Id && x.ChildId == childGroup.Id)) { throw new ArgumentException("Cannot add item to group since it is already a member."); } Grouping_Grouping association = new Grouping_Grouping() { ParentId = parent.Id, ChildId = childGroup.Id }; _db.Grouping_Grouping.Add(association); _db.SaveChanges(); } else { if (childGroup != null) { throw new InvalidOperationException("The database contains both a text and a group with the same name. That's not good."); } if (_db.Text_Grouping.Any(x => x.TextId == childText.Id && x.GroupingId == parent.Id)) { throw new ArgumentException("Cannot add item to group since it is already a member."); } Text_Grouping assocation = new Text_Grouping() { TextId = childText.Id, GroupingId = parent.Id }; _db.Text_Grouping.Add(assocation); _db.SaveChanges(); } }