Exemple #1
0
        public async Task AddCommentAsync(string articleId, CommentModel comment)
        {
            if (comment == null || articleId == null)
            {
                throw new ArgumentNullException();
            }
            using (var trans = _dBContext.Database.BeginTransaction())
            {
                try
                {
                    var commentEntity = ModelEntityHelper.CommentM2E(comment);
                    var articleEntity = await _dBContext.Articles.Where(a => a.Id == int.Parse(articleId)).SingleOrDefaultAsync();

                    if (articleEntity == null)
                    {
                        throw new NotFoundException($"没找到id为:{articleId}的文章");
                    }
                    articleEntity.Comment++;
                    commentEntity.ArticleId = (articleId != null) ? int.Parse(articleId) : 0;
                    _dBContext.Comments.Add(commentEntity);
                    await _dBContext.SaveChangesAsync();

                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                }
            }
        }
Exemple #2
0
        public async Task UpdateArticleAsync(ArticleInfoModel article, ArticleContentModel articleContentModel)
        {
            if (article == null || articleContentModel == null)
            {
                throw new ArgumentNullException();
            }
            var ae = ModelEntityHelper.ArticleM2E(article);

            _dBContext.Articles.Update(ae);
            await _dBContext.SaveChangesAsync();
        }
Exemple #3
0
        public async Task <CommentModel> GetCommentAsync(string id)
        {
            if (id == null)
            {
                throw new ArgumentNullException();
            }
            var comment = await _dBContext.Comments.Where(c => c.Id == int.Parse(id)).SingleOrDefaultAsync();

            if (comment == null)
            {
                throw new NotFoundException($"没找到id为:{id}的评论");
            }
            return(ModelEntityHelper.CommentE2M(comment));
        }
Exemple #4
0
        public async Task <IList <ArticleInfoModel> > GetArticleInfosAsync()
        {
            var articles = await _dBContext.Articles
                           .Include(a => a.Category)
                           .OrderByDescending(a => a.Id)
                           .ToListAsync();

            IList <ArticleInfoModel> result = new List <ArticleInfoModel>();

            foreach (ArticleEntity ae in articles)
            {
                var temp = ModelEntityHelper.ArticleE2M(ae);
                result.Add(temp);
            }
            return(result);
        }
Exemple #5
0
        public async Task <ArticleInfoModel> GetArticleInfoAsync(string id)
        {
            if (id == null)
            {
                throw new ArgumentNullException();
            }
            try
            {
                var article = await _dBContext.Articles.Where(c => c.Id == int.Parse(id)).SingleOrDefaultAsync();

                if (article == null)
                {
                    throw new NotFoundException($"没找到id为:{id} 的文章");
                }
                return(ModelEntityHelper.ArticleE2M(article));
            }
            catch
            {
                throw;
            }
        }
Exemple #6
0
        public async Task <IList <CommentModel> > GetCommentsAsync(string articleId, int start, int count)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException();
            }
            IList <CommentModel> commentModels = new List <CommentModel>();
            var commentsByArticleId            = await _dBContext.Comments.Where(c => c.ArticleId == int.Parse(articleId)).ToListAsync();

            if (commentsByArticleId == null)
            {
                throw new NotFoundException($"没找到id为:{articleId} 的文章");
            }
            commentsByArticleId = commentsByArticleId.OrderByDescending(c => c.Id).Skip(start).Take(count).ToList();
            foreach (CommentEntity ce in commentsByArticleId)
            {
                var cm = ModelEntityHelper.CommentE2M(ce);
                commentModels.Add(cm);
            }
            return(commentModels);
        }
Exemple #7
0
        public async Task AddArticleAsync(ArticleInfoModel article, ArticleContentModel articleContentModel)
        {
            if (article == null || articleContentModel == null)
            {
                throw new ArgumentNullException();
            }
            using (var trans = _dBContext.Database.BeginTransaction())
            {
                try
                {
                    string category       = article.Category;
                    var    categoryEntity = await _dBContext.Categories.Where(c => c.CategoryName == category).SingleOrDefaultAsync();

                    if (categoryEntity == null)
                    {
                        categoryEntity = new CategoryEntity
                        {
                            CategoryName = category
                        };
                    }
                    var entity  = ModelEntityHelper.ArticleM2E(article);
                    var content = ModelEntityHelper.ContentM2E(articleContentModel);
                    entity.ArticleContent = content;
                    entity.Category       = categoryEntity;

                    await _dBContext.Articles.AddAsync(entity);

                    await _dBContext.SaveChangesAsync();

                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                }
            }
        }