public static Tag ToModel(this DalTagEntity item) { if (item == null) return null; Tag tagEntity = new Tag { Id = item.Id, Name = item.Name, ArticleId = item.Article.Id }; return tagEntity; }
public static void CopyFieldsTo(this DalTag source, Tag target) { if (source == null) { throw new ArgumentNullException("source"); } if (target == null) { throw new ArgumentNullException("target"); } target.Name = source.Name; }
/// <summary> /// Change properties for specified article and set new tags. /// </summary> /// <param name="article"></param> /// <param name="tags"></param> public void Edit(DalArticle article, string[] tags) { if (article == null) throw new ArgumentNullException(nameof(article)); if (tags == null) tags = new string[0]; Article ormArticle = context.Set<Article>().Find(article.Id); if (article.Content != null) ormArticle.Content = article.Content; if (article.Title != null) ormArticle.Title = article.Title; DbSet<Tag> tagSet = context.Set<Tag>(); foreach (Tag tag in ormArticle.Tags) { //TODO optimize here //tag.Articles.Remove(ormArticle); var t = tag.Articles.ToList(); t.Remove(ormArticle); tag.Articles = t; } ormArticle.Tags = new List<Tag>(); foreach (string tag in tags) { Tag ormTag = tagSet.FirstOrDefault(x => x.Value == tag); if (ormTag == null) { ormTag = new Tag() { Value = tag }; tagSet.Add(ormTag); } //TODO optimize here var list = ormTag.Articles.ToList(); list.Add(ormArticle); ormTag.Articles = list; //tagSet.Add(ormTag); ormArticle.Tags.Add(ormTag); } }