示例#1
0
        public async Task <IActionResult> AddComment([FromBody] CommentForCreateDto commentForCreate)
        {
            if (commentForCreate == null)
            {
                return(BadRequest("input was not valid"));
            }

            var comment = new Comment
            {
                NewsId   = commentForCreate.NewsId,
                AuthorId = commentForCreate.AuthorId,
                Content  = commentForCreate.Content
            };

            var x = _dbContext.Add <Comment>(comment);
            await _dbContext.SaveChangesAsync();

            var commentsFromRepo = await _dbContext.Comments.Include(a => a.Author)
                                   .Where(n => n.NewsId == comment.NewsId).ToListAsync();

            var commentFromRepo = commentsFromRepo.TakeLast(1).FirstOrDefault();

            var commentToReturn = CommentDto.Map(commentFromRepo);

            return(Ok(commentToReturn));
        }
        public async Task <IActionResult> GetNews(long id)
        {
            News news = await _dbContext.News.Include(u => u.Author).Include(p => p.Photo)
                        .FirstOrDefaultAsync(n => n.Id == id);

            if (news != null)
            {
                var newsToReturn = NewsForDetailsDto.Map(news);

                List <Comment> comments = await _dbContext.Comments.Include(a => a.Author)
                                          .Where(c => c.NewsId == id).ToListAsync();

                var commentsToReturn = CommentDto.Map(comments);
                newsToReturn.Comments = commentsToReturn;

                List <News> x = await _dbContext.News.Include(p => p.Photo)
                                .Where(s => s.Section == newsToReturn.Section && s.Id != id)
                                .OrderByDescending(z => z.AddedAt).ToListAsync();

                List <LatestNewsDto> otherInfotoReturn = LatestNewsDto.Map(x.Take(5).ToList());

                return(Ok(new DataForNewsDetailsComponentDto
                {
                    NewsForDetails = newsToReturn,
                    NewsForOtherInfos = otherInfotoReturn
                }));
            }
            else
            {
                return(NoContent());
            }
        }