public void AddArticle(CreateArticleDTO articleDto) { var article = Mapper.Map <CreateArticleDTO, Article>(articleDto); var tagUtil = new TagUtil(UnitOfWork.TagRepository); article.Tags = tagUtil.GetTags(article); UnitOfWork.ArticleRepository.Add(article); UnitOfWork.Commit(); }
public static Filter ParseFromStatement(string filterStatement) { string errMsg = "Something about this filter is not correct! Please check to make sure its formatted correctly."; List <string> terms = SplitStatement(filterStatement); if (terms.Count < 1) { throw new SyntaxException(errMsg); } if (terms.Count == 1) { Filter f = new Filter((o) => { var tags = TagUtil.GetTags(o); return(tags.Contains(terms[0])); }); f._filterText = terms[0]; return(f); } Filter filter; int i; if (Operator.ByName(terms[0]) == Operator.NOT) { filter = Invert(ParseFromStatement(terms[1])); i = 2; } else { filter = ParseFromStatement(terms[0]); i = 1; } try { while (i < terms.Count - 1) { if (!Operator.IsOperator(terms[i]) || Operator.ByName(terms[i]) == Operator.NOT) { throw new SyntaxException(errMsg); } var op = Operator.ByName(terms[i]); if (Operator.ByName(terms[i + 1]) == Operator.NOT) { filter = Combine(filter, Invert(ParseFromStatement(terms[i + 2])), op); i += 3; } else { filter = Combine(filter, ParseFromStatement(terms[i + 1]), op); i += 2; } } } catch (IndexOutOfRangeException e) { throw new SyntaxException(errMsg + ":\n" + e.Message); } return(filter); }
/// <summary> /// writes the given object to the StreamWriter /// /// throws /// - ArgumentException if the gedcomLine is a GedcomLine and no tagStr (gedcomLine.tagStr) is given /// </summary> /// <param name="gedcomLine">the gedcom line that should be written</param> /// <param name="tagStr"></param> /// <param name="streamWriter">the stream writer that the line should be written to</param> /// <param name="level">the level of the line</param> /// <param name="path">the path for the current object</param> private void WriteObject(object gedcomLine, string tagStr, StreamWriter streamWriter, int level, string path) { var lineV = new Line { Level = level, TagName = tagStr }; if (gedcomLine is GedcomLine) { try { var lineValueProperty = TagUtil.GetMember(gedcomLine, "", true, false); var obj = lineValueProperty.GetValue(gedcomLine, null); if (obj == null) { lineV.LineValue = ""; } else { lineV.LineValue = obj.ToString(); } if (lineV.LineValue.Length > LengthUtil.GetMaximumLength(lineValueProperty)) { reporting.Warn("line-value too long"); } if (lineV.LineValue.Length < LengthUtil.GetMinimumLength(lineValueProperty)) { reporting.Warn("line-value too short"); } } catch (MemberNotFoundException) { lineV.LineValue = ""; } if (gedcomLine is Record record) { lineV.XRefId = record.XRef; } } else if (gedcomLine.GetType().IsEnum) { lineV.LineValue = EnumTagUtil.GetFirstTagName(gedcomLine as ValueType); } else { lineV.LineValue = gedcomLine.ToString(); } WriteLine(lineV, streamWriter); foreach (var type in TagUtil.GetTags(gedcomLine.GetType())) { var prop = type.Key; var defaultTag = TagUtil.GetTagName(prop); if (!prop.CanRead) { continue; } var minOccur = QuantityUtil.GetMinimum(prop); var maxOccur = QuantityUtil.GetMaximum(prop); var obj = prop.GetValue(gedcomLine, null); if (TagUtil.IsDefaultValue(prop, obj)) { if (minOccur > 0) { reporting.Warn( $"object {prop.Name} in {path} must occur at least {minOccur} time(s), but was not set"); } continue; } // use a loop, event its just a single object to avoid redundant code var tmpList = ImplementsIList(prop.PropertyType) ? obj as IList : new[] { obj }; var occurrences = 0; foreach (var o in tmpList) { occurrences++; string tag; if (o is GedcomLine) { var line = o as GedcomLine; line.Tag = GetTag(type.Value, line); tag = line.Tag; } else { tag = defaultTag; } if (tag == "") { continue; } try { WriteObject(o, tag, streamWriter, level + 1, path + "/" + tag); } catch (Exception) { continue; } } if (occurrences < minOccur) { reporting.Warn( $"object {prop.Name} in {path} must occur at least {minOccur} time(s), but occured only {occurrences} time(s)"); } if (occurrences > maxOccur) { reporting.Warn( $"object {prop.Name} in {path} can only occur {maxOccur} time(s), but occured {occurrences} time(s)"); } } }
public void GetTagsIfDbTagsNotExits_Test() { var articlesTags = _tagUtil.GetTags(article); Assert.AreEqual(_fakeDbTags.Count, articlesTags.Count); }