Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 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());
 }
Exemplo n.º 7
0
        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);
Exemplo n.º 8
0
 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));
     }
 }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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();
                });
            }));
        }