public override async Task <GetPostsResponse> GetPublishedPostsByCategory(GetPostsByCategoryRequest request, ServerCallContext context) { var response = new GetPostsResponse(); var categoryEntity = await _blogDbContext.Categories .SingleOrDefaultAsync(category => category.Slug == request.CategorySlug && category.IsActive && !category.IsDeleted); if (categoryEntity == null) { return(response); } var postEntities = await _blogDbContext.Posts .Where(post => post.CategoryId == categoryEntity.Id) .Where(post => post.IsActive && !post.IsDeleted) .Where(post => post.Status == PostStatus.Published) .OrderByDescending(post => post.PublishedDate) .Skip((int)request.Offset) .Take((int)request.Limit) .ToListAsync(); response.Posts.AddRange(postEntities.Select(postEntity => _postMapper.Map(postEntity, categoryEntity))); return(response); }
public GetPostsResponse GetAllByCategory(GetAllPostsByCategoryRequest request) { GetPostsResponse response = new GetPostsResponse(); response.StatusCode = 200; response.Errors = new List <string>(); response.Posts = new List <PostDTO>(); var cat = _categoryRepository.GetCategoryByUrl(request.CategoryURL); if (cat == null) { response.StatusCode = 404; response.Errors.Add("Category not found"); return(response); } var posts = _postRepository.GetByCategory(cat.Id); foreach (var post in posts) { var tags = new List <string>(); var tagEntities = _tagRepository.GetByPostId(post.Id); foreach (var tag in tagEntities) { tags.Add(tag.Content); } PostDTO postWithTags = new PostDTO() { Post = post, Tags = tags, AuthorUsername = _userRepository.GetUserById(post.UserId).Username, }; response.Posts.Add(postWithTags); } return(response); }
public GetPostsResponse GetAll(GetAllPostsRequest request) { GetPostsResponse response = new GetPostsResponse(); response.StatusCode = 200; response.Errors = new List <string>(); response.Posts = new List <PostDTO>(); var posts = _postRepository.GetAll(); foreach (var post in posts) { var tags = new List <string>(); var tagEntities = _tagRepository.GetByPostId(post.Id); foreach (var tag in tagEntities) { tags.Add(tag.Content); } PostDTO postWithTags = new PostDTO() { Post = post, Tags = tags, AuthorUsername = _userRepository.GetUserById(post.UserId).Username, }; response.Posts.Add(postWithTags); } return(response); }
public async Task <GetPostsResponse> GetPostsAsync(string requesterId = null) { var postEntities = await _postRepository.GetPostsHomeScreenAsync(); var response = new GetPostsResponse { StatusCode = (int)HttpStatusCode.OK, Posts = postEntities.Select(p => _postMapper.ToModel(p, requesterId)).ToList() }; return(response); }
public async Task <GetPostsResponse> GetPostsFromLocationAsync(double latitude, double longitude, string requesterId) { var postEntities = await _postRepository.GetPostsFromLocationAsync(latitude, longitude); var response = new GetPostsResponse { StatusCode = (int)HttpStatusCode.OK, Posts = postEntities.Select(p => _postMapper.ToModel(p, requesterId)).ToList() }; return(response); }
private static List <PostViewModel> MapToViewModel(GetPostsResponse response) { return(response.Posts.Select(post => new PostViewModel() { Id = post.Id, Title = post.Title, Slug = post.Slug, Description = post.Description, PublishedDate = post.PublishedDate.ToDateTime(), CoverImageUrl = post.CoverImageUrl, CategoryName = post.CategoryName, CategorySlug = post.CategorySlug }).ToList()); }
public async Task <List <PostDTO> > GetPostsAsync(string authorshortId = "", string pageId = "", CancellationToken cancellationToken = default(CancellationToken)) => await Task.Run(async() => { if (!CrossConnectivity.Current.IsConnected) { throw new InvalidOperationException(AppConsts.ERROR_INTERNET_CONNECTION); } List <PostDTO> posts = new List <PostDTO>(); GetPostsRequest getPostsRequest = new GetPostsRequest { Url = string.Format(GlobalSettings.Instance.Endpoints.PostEndpoints.GetPostsPoint, authorshortId, pageId, GlobalSettings.Instance.UserProfile.Id, GlobalSettings.Instance.UserProfile.ProfileType.ToString()), AccessToken = GlobalSettings.Instance.UserProfile.AccesToken }; GetPostsResponse getPostsResponse = null; try { getPostsResponse = await _requestProvider.GetAsync <GetPostsRequest, GetPostsResponse>(getPostsRequest); if (getPostsResponse.Posts != null) { posts = getPostsResponse.Posts.Select <PostDTO, PostDTO>(pDTO => { pDTO.PublishTime = pDTO.PublishTime.ToLocalTime(); pDTO.Comments.ForEach <CommentDTO>(cDTO => cDTO.CreationTime = cDTO.CreationTime.ToLocalTime()); pDTO.Text = NormalizeTextMessage(pDTO.Text); return(pDTO); }).ToList(); } } catch (ServiceAuthenticationException exc) { _identityUtilService.RefreshToken(); throw exc; } catch (Exception ex) { Crashes.TrackError(ex); Debug.WriteLine($"ERROR:{ex.Message}"); throw new Exception(ex.Message); } return(posts); }, cancellationToken);
public override Task <GetPostsResponse> GetPosts(GetPostsRequest request, ServerCallContext context) { try { var post = new PostComment.Post(); var returnValue = post.GetAllPosts(); var response = new GetPostsResponse(); response.Values.AddRange(returnValue.Select(x => (Post)x)); return(Task.FromResult(response)); } catch (Exception ex) { _logger.LogError(ex, "Error invoking GetPosts"); throw new RpcException(new Status(StatusCode.Internal, ex.Message)); } }
public override async Task <GetPostsResponse> GetPublishedPosts(GetPostsRequest request, ServerCallContext context) { var posts = await _blogDbContext.Posts .Where(post => post.IsActive && !post.IsDeleted) .Where(post => post.Status == PostStatus.Published) .OrderByDescending(post => post.PublishedDate) .Skip((int)request.Offset) .Take((int)request.Limit) .ToListAsync(); var response = new GetPostsResponse(); response.Posts.AddRange(posts.Select(_postMapper.Map)); return(response); }
private Task GetPosts() { DisableGui(); Posts.Clear(); return(Task.Run(async() => { GetPostsResponse response = await Connector.GetLatestPosts(); response.SaveToJson("Latest posts.json"); Dispatcher.Invoke(() => { foreach (Post p in response.Posts) { Posts.Add(p); } EnableGui(); }); })); }