public void UpdateArticle(ArticleState articleState)
 {
     try
     {
         using (ISession session = SessionHelper.OpenSession())
         {
             using (ITransaction transaction = session.BeginTransaction())
             {
                 var criteria = session.CreateCriteria<ArticleState>().SetFetchMode("Comments", FetchMode.Eager);
                 criteria.Add(Restrictions.Eq("Id", articleState.Id));
                 var article = criteria.List<ArticleState>().FirstOrDefault();
                 foreach (CommentState comment in article.Comments)
                 {
                     CommentState existingComment = articleState.Comments.FirstOrDefault(c => c.Id == comment.Id);
                     if (existingComment == null)
                     {
                         session.Delete(comment);
                     }
                 }
                 foreach (CommentState comment in articleState.Comments)
                 {
                     comment.ArticleState = article;
                     session.Merge(comment);
                 }
                 ArticleState loadedState = session.Merge(articleState);
                 session.Update(loadedState);
                 transaction.Commit();
             }
         }
     }
     catch (HibernateException ex)
     {
         throw new RepositoryException("Ошибка репозитория", ex);
     }
 }
Example #2
0
 public static Article Create(ArticleState articleState)
 {
     return new Article
     {
         State = articleState
     };
 }
Example #3
0
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     var dto = (ArticleDto)value;
     var article = new ArticleState
     {
         Created = dto.Created,
         Description = dto.Description,
         Text = dto.Text,
         Title = dto.Title,
         Id = dto.Id
     };
     if (dto.Comments != null)
     {
         article.Comments = (from c in dto.Comments
                             select
                                 new CommentState
                                 {
                                     ArticleState = article,
                                     Id = c.Id,
                                     Text = c.Text,
                                     Created = c.Created
                                 }).ToList();
     }
     return article;
 }
 public void CreateArticle(ArticleState articleState)
 {
     try
     {
         using (ISession session = SessionHelper.OpenSession())
         {
             using (ITransaction transaction = session.BeginTransaction())
             {
                 session.Save(articleState);
                 transaction.Commit();
             }
         }
     }
     catch (HibernateException ex)
     {
         throw new RepositoryException("Ошибка репозитория", ex);
     }
 }
Example #5
0
 public void GivenIHaveArticleWithValidIdInStore()
 {
     ArticleState article = NinjectServiceLocator.Kernel.Get<StandardQueryRepository>().GetArticle(_articleId);
     if (article != null)
     {
         NinjectServiceLocator.Kernel.Get<StandardCommandRepository>().DeleteArticle(_articleId.ToString());
     }
     article = new ArticleState
     {
         Id = _articleId,
         Title = "article title",
         Text = "article text",
         Created = DateTime.Now
     };
     article.Comments.Add(new CommentState
     {
         ArticleState = article,
         Created = DateTime.Now,
         Id = Guid.Parse(Metadata.AvailableCommentId),
         Text = "commentText"
     });
     NinjectServiceLocator.Kernel.Get<StandardCommandRepository>().CreateArticle(article);
 }