public async Task <PagedResultDTO <PostDTO> > GetPageAsync(int id, int page, int pageSize, bool justMyPosts, string srchStr = null) { var pagedResult = await _db.Posts.GetPageAsync(id, page, pageSize, justMyPosts, srchStr); pagedResult.Data.ForEach(p => p.PostFiles = p.PostFiles.Count > 0 ? new List <PostFile>() { p.PostFiles.First() } : new List <PostFile>() ); Func <Post, PostDTO> func = post => { var postDTO = PostMapper.ConvertToPostDTO(post); postDTO.Files = FileMapper.ConvertToFileInfoDTOCollection(post.PostFiles); postDTO.UserDTO = UserMapper.ConvertToUserDTO(post.User); postDTO.CommentsDTO = CommentMapper.ConvertToCommentDTOCollection(post.Comments); var usersDTOLikes = new List <UserDTO>(); post.PostLikes.ToList().ForEach(pl => { var userDTO = UserMapper.ConvertToUserDTO(pl.User); userDTO.RoleDTO = RoleMapper.ConvertToRoleDTO(pl.User.Role); usersDTOLikes.Add(userDTO); }); postDTO.UsersLikes = usersDTOLikes; return(postDTO); }; var pageResultDTO = PagedResultMapper.MapToDTO(pagedResult, func); return(pageResultDTO); }
public async Task <IEnumerable <PostDTO> > GetPostsAsync() { var posts = await _db.Posts.GetAllAsync(); var postsDTO = new List <PostDTO>(); //var currentDateTime = DateTime.Now; posts.ForEach(p => { var postDTO = PostMapper.ConvertToPostDTO(p); postDTO.Files = FileMapper.ConvertToFileInfoDTOCollection(p.PostFiles); postsDTO.Add(postDTO); }); return(postsDTO); }