Example #1
0
        public bool CreateArticle(string title, string content, Guid[] categoryIds, Guid userId)
        {
            Article article = new Article()
            {
                ArticleId             = Guid.NewGuid(),
                ArticleCreateDateTime = DateTime.Now,
                ArticleIsRemoved      = 0,
                ArticleTitle          = title,
                ArticleContent        = content,
                ArticleUserId         = userId,
                ArticleLikeCount      = 0,
                ArticleUnlikeCount    = 0
            };

            // create an artile
            bool articleCreated = _articleDAL.CreateEntity(article);

            // add relationship between the article and categories
            bool articleToCategoryCreated = false;

            foreach (Guid categoryId in categoryIds)
            {
                articleToCategoryCreated = _articleToCategoryDAL.CreateEntity(new ArticleToCategory()
                {
                    ArticleToCategoryId             = Guid.NewGuid(),
                    ArticleToCategoryCreateDateTime = DateTime.Now,
                    ArticleToCategoryIsRemoved      = 0,
                    ArticleToCategoryCategoryId     = categoryId,
                    ArticleToCategoryArticleId      = article.ArticleId
                });

                if (!articleToCategoryCreated)
                {
                    articleToCategoryCreated = false;
                    break;
                }
            }
            if (articleCreated && articleToCategoryCreated)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }