public static void CreateArticle(long blogid, string mediaurl, long type, string text) { DataAccess.T_Article art = new DataAccess.T_Article() { CreationDate = DateTime.Now, BlogId = blogid, MediaTypeId = type, MediaUrl = mediaurl, Text = text }; DataAccess.ArticleCRUD.Create(art); }
public static Dbo.Article GetArticleDbo(long articleId) { try { using (Entities bdd = new Entities()) { T_Article article = bdd.T_Article.Include("T_Comment.T_User").Include("T_ArticleTag.T_Tag").Where(a => a.Id == articleId).FirstOrDefault(); Dbo.Article dboArticle = new Dbo.Article() { Id = article.Id, BlogId = article.BlogId, MediaUrl = article.MediaUrl, MediaTypeId = article.MediaTypeId, Caption = article.Text, CreationDate = article.CreationDate, Comments = new List <Dbo.Comment>(), Tags = new List <Dbo.Tag>() }; foreach (T_Comment comment in article.T_Comment) { dboArticle.Comments.Add(new Dbo.Comment() { Id = comment.Id, UserId = comment.UserId, ArticleId = comment.ArticleId, Content = comment.Comment, CreationDate = comment.CreationDate, UserName = comment.T_User.Login }); } foreach (T_ArticleTag aTag in article.T_ArticleTag) { Dbo.Tag tag = new Dbo.Tag(); tag.Id = aTag.T_Tag.Id; tag.Name = aTag.T_Tag.Name; dboArticle.Tags.Add(tag); } return(dboArticle); } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
public static void CreateAndAddTags(T_Article article, List<T_Tag> tags) { try { using (Entities bdd = new Entities()) { using (TransactionScope scope = new TransactionScope()) { bdd.T_Article.Add(article); bdd.SaveChanges(); foreach (T_Tag tag in tags) { T_Tag oldTag = bdd.T_Tag.Where(tg => tg.Name == tag.Name).FirstOrDefault(); if (oldTag == null) { bdd.T_Tag.Add(tag); bdd.SaveChanges(); T_ArticleTag at = new T_ArticleTag() { ArticleId = article.Id, TagId = tag.Id }; bdd.T_ArticleTag.Add(at); bdd.SaveChanges(); } else { T_ArticleTag at = new T_ArticleTag() { ArticleId = article.Id, TagId = oldTag.Id }; bdd.T_ArticleTag.Add(at); bdd.SaveChanges(); } } scope.Complete(); } } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
public static void CreateAndAddTags(T_Article article, List <T_Tag> tags) { try { using (Entities bdd = new Entities()) { using (TransactionScope scope = new TransactionScope()) { bdd.T_Article.Add(article); bdd.SaveChanges(); foreach (T_Tag tag in tags) { T_Tag oldTag = bdd.T_Tag.Where(tg => tg.Name == tag.Name).FirstOrDefault(); if (oldTag == null) { bdd.T_Tag.Add(tag); bdd.SaveChanges(); T_ArticleTag at = new T_ArticleTag() { ArticleId = article.Id, TagId = tag.Id }; bdd.T_ArticleTag.Add(at); bdd.SaveChanges(); } else { T_ArticleTag at = new T_ArticleTag() { ArticleId = article.Id, TagId = oldTag.Id }; bdd.T_ArticleTag.Add(at); bdd.SaveChanges(); } } scope.Complete(); } } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
public static void Create(T_Article article) { try { using (Entities bdd = new Entities()) { bdd.T_Article.Add(article); bdd.SaveChanges(); } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
public static void Update(long articleId, T_Article newT_Article) { try { using (Entities bdd = new Entities()) { T_Article article = Get(articleId); article = newT_Article; bdd.SaveChanges(); } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
public static void Delete(long articleId) { try { using (Entities bdd = new Entities()) { T_Article article = bdd.T_Article.Where(art => art.Id == articleId).FirstOrDefault(); if (article == null) { return; } bdd.T_ArticleTag.Where(aTag => aTag.ArticleId == articleId).ToList().ForEach(aTag => bdd.T_ArticleTag.Remove(aTag)); bdd.T_Article.Remove(article); bdd.SaveChanges(); } } catch (Exception e) { Trace.WriteLine(e.Message); throw; } }
/// <summary> /// Create an Article and insert it in the DB /// </summary> /// <param name="blogID">Id of the blog containing the T_Article.</param> /// <param name="mediaUrl">Link to the media includes into the article, or text if the media is a quote.</param> /// <param name="mediaTypeId">Type of the media.</param> /// <param name="text">A caption for the media, or the content of the article if there is no media.</param> /// <param name="tags">The tags, separated by a space</param> /// <returns>Nothing</returns> public static void Create(long blogID, String mediaUrl, long mediaTypeId, String text, String tags) { T_Article article = new T_Article() { BlogId = blogID, MediaUrl = mediaUrl, MediaTypeId = mediaTypeId, Text = text, CreationDate = DateTime.Now }; List<T_Tag> newtags = new List<T_Tag>(); if (!String.IsNullOrEmpty(tags)) { string[] tabTags = tags.Split(new Char[] { ' ' }); foreach (string tag in tabTags) { newtags.Add(new T_Tag() { Name = tag }); } } ArticleCRUD.CreateAndAddTags(article, newtags); }