private async Task <BlogModel> GetCustomers(int currentPage)
        {
            int maxRows = 100;

            BlogModel blogModel = new BlogModel();
            IEnumerable <CommentPostVM> comments = null;

            using (var client = new HttpClient())
            {
                PagingParam pagingParam = new PagingParam();
                pagingParam.pageSize   = maxRows;
                pagingParam.pageNumber = currentPage;
                StringContent content = new StringContent(JsonConvert.SerializeObject(pagingParam), Encoding.UTF8, "application/json");
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(scheme: "Bearer", parameter: "Provide token after login");
                string endpoint = apiBaseUrl + "Comments/Display";

                using (var Response = await client.PostAsync(endpoint, content))
                {
                    if (Response.IsSuccessStatusCode)
                    {
                        var readTask = Response.Content.ReadAsAsync <BlogModel>();
                        blogModel = readTask.Result;

                        List <CommentPostVM> list = new List <CommentPostVM>();
                        var post = blogModel.CommentPostVM.GroupBy(x => x.PostID).ToList();
                        foreach (var item in post)
                        {
                            var comnt = blogModel.CommentPostVM.Where(x => x.PostID == item.Key).GroupBy(x => x.Id).ToList();
                            foreach (var c in comnt)
                            {
                                CommentPostVM entity = new CommentPostVM();
                                entity.PostMessage    = item.FirstOrDefault().PostMessage;
                                entity.PostedDate     = item.FirstOrDefault().PostedDate;
                                entity.PostBy         = item.FirstOrDefault().PostBy;
                                entity.CommentMessage = c.FirstOrDefault().CommentMessage;
                                entity.CommentedDate  = c.FirstOrDefault().CommentedDate;
                                entity.CommentBy      = c.FirstOrDefault().CommentBy;
                                entity.Likes          = blogModel.CommentPostVM.Where(x => x.PostID == item.Key && x.ReactStatus == 'L' && x.Id == c.Key).Count();
                                entity.Dislikes       = blogModel.CommentPostVM.Where(x => x.PostID == item.Key && x.ReactStatus == 'D' && x.Id == c.Key).Count();
                                list.Add(entity);
                            }
                        }
                        blogModel.CommentPostVM = list;
                    }
                }

                return(blogModel);
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <IEnumerable <CommentPostVM> > > Get()
        {
            List <CommentPostVM> list = new List <CommentPostVM>();
            var comments = _context.Comments.ToList();

            foreach (var item in comments)
            {
                var post = _context.Posts.Where(x => x.Id == item.PostID).FirstOrDefault();
                if (post != null)
                {
                    CommentPostVM entity = new CommentPostVM();
                    entity.PostID         = post.Id;
                    entity.PostMessage    = post.PostMessage;
                    entity.PostedDate     = post.PostedDate;
                    entity.PostBy         = post.PostBy;
                    entity.Id             = item.Id;
                    entity.CommentBy      = item.CommentBy;
                    entity.CommentMessage = item.CommentMessage;
                    entity.CommentedDate  = item.PostedDate;
                    list.Add(entity);
                }
            }
            return(list);
        }