public async Task <PostsResponse> GetPosts() { PostsResponse postsResponse = new PostsResponse(); string endpoint = Configurations.PostsEndPoint; var response = await HttpClientBaseService.GetAsync(endpoint); if (response.Content == null) { return((PostsResponse)Convert.ChangeType(response, typeof(PostsResponse))); } else { string data = await response.Content.ReadAsStringAsync(); if (string.IsNullOrEmpty(data)) { return(new PostsResponse { }); } var result = JsonConvert.DeserializeObject <IList <Post> >(data); postsResponse.Posts = result; return(postsResponse); } }
public PostsResponse GetById(int id) { PostsResponse response = new PostsResponse(); if (id == 0) { response.Success = false; response.Message = "Id value was not passed in."; return(response); } try { response = repo.GetById(id); if (response.Posts.Count == 0 || response.Posts.First() == null) { response.Success = false; response.Message = "No posts found"; } response.Success = true; } catch (Exception ex) { response.Success = true; response.Message = ex.Message; } return(response); }
static async Task Main(string[] args) { var httpClientHandler = new HttpClientHandler(); httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; var httpClient = new HttpClient(httpClientHandler); var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions { HttpClient = httpClient }); var client = new gRPCPostCommentService.gRPCPostCommentServiceClient(channel); await client.SubmitPostAsync(new PostMessage() { Description = "TestDescription", Domain = "TestDomain", Date = "TestDate" }); PostsResponse postsResponse = await client.GetAllPostsAsync(new NoParamsMessage()); Console.WriteLine("All posts: "); foreach (PostMessage postMessage in postsResponse.Posts) { Console.WriteLine(postMessage); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public PostsResponse GetByApproval(bool isApproved) { PostsResponse response = new PostsResponse(); using (var context = new PersonalBlogEntities()) { try { response.Posts = context.Posts .Include("Tags") .Where(p => p.IsApproved == isApproved) .ToList(); if (response.Posts.Count == 0) { response.Success = false; response.Message = "Nothing found."; return(response); } response.Success = true; } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } } return(response); }
public PostsResponse Edit(Post post) { PostsResponse response = new PostsResponse(); try { using (var context = new PersonalBlogEntities()) { var toEdit = context.Posts.Where(p => p.PostId == post.PostId).First(); context.Entry(toEdit).State = System.Data.Entity.EntityState.Modified; toEdit.CreatedDate = post.CreatedDate; toEdit.ImageFileName = post.ImageFileName; toEdit.IsApproved = post.IsApproved; toEdit.CategoryId = post.CategoryId; toEdit.PostBody = post.PostBody; toEdit.Tags.Clear(); toEdit.PostTitle = post.PostTitle; context.SaveChanges(); toEdit.Tags = context.Tags.AsEnumerable().Where(t => post.Tags.Any(postTag => postTag.TagId == t.TagId)).ToList(); context.SaveChanges(); response.Success = true; } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public PostsResponse Edit(Post post) { var context = new PersonalBlogEntities(); var response = new PostsResponse(); if (string.IsNullOrEmpty(post.PostTitle)) { response.Success = false; response.Message = "The post title cannot be left blank."; } else if (!post.IsApproved) { response.Success = false; response.Message = "This post has content that violates our blogging policy."; } else if (string.IsNullOrEmpty(post.PostBody)) { response.Success = false; response.Message = "The post body cannot be left blank."; } else if (context.Categories.FirstOrDefault(c => c.CategoryId == post.CategoryId) == null) { response.Success = false; response.Message = "That category is invalid"; } else { response = repo.Edit(post); response.Message = $"Your changes to \"{post.PostTitle}\" have been saved."; } return(response); }
public PostsResponse Delete(int postId) { PostsResponse response = new PostsResponse(); try { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "DeletePost"; cmd.Parameters.AddWithValue("@PostId", postId); conn.Open(); cmd.ExecuteNonQuery(); } response.Success = true; } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public PostsResponse SearchPosts(int tagId, int catId) { PostsResponse response = new PostsResponse(); PostsResponse catSearchResult = repo.GetByCategory(catId); PostsResponse tagSearchResult = repo.GetByTag(tagId); if (!(catSearchResult.Success && tagSearchResult.Success)) { response.Success = false; response.Message = "No results found for either entered tags or category"; return(response); } response.Posts = catSearchResult.Posts; foreach (var post in response.Posts) { if (!tagSearchResult.Posts.Contains(post)) { tagSearchResult.Posts.Remove(post); } } foreach (var post in tagSearchResult.Posts) { response.Posts.Add(post); } response.Success = true; return(response); }
public void GetPostByTitle(string title, int postId, bool expected) { PostsResponse response = repo.GetByTitle(title); Assert.AreEqual(expected, response.Success); if (expected == true) { Assert.AreEqual(postId, response.Posts.First().PostId); } }
public void GetPostByApproval(int postCount, bool isApproved, bool expected) { PostsResponse response = repo.GetByApproval(isApproved); Assert.AreEqual(expected, response.Success); if (expected == true) { Assert.AreEqual(postCount, response.Posts.Count()); } }
public void GetPostByTag(int postCount, int tagId, bool expected) { PostsResponse response = repo.GetByTag(tagId); Assert.AreEqual(expected, response.Success); if (expected == true) { Assert.AreEqual(postCount, response.Posts.Count()); } }
public void GetPostByCat(int catId, int postCount, bool expected) { PostsResponse response = repo.GetByCategory(catId); Assert.AreEqual(expected, response.Success); if (expected == true) { Assert.AreEqual(postCount, response.Posts.Count()); } }
public void AddPost() { Post test = repo.GetByCategory(1).Posts.First(); test.PostId = 0; test.PostTitle = "HurrMurmuhFur"; PostsResponse response = repo.Add(test); Assert.AreEqual(true, response.Success); }
public void EditPost() { Post test = repo.GetByCategory(1).Posts.First(); test.PostTitle = "HurrMurmuhFur"; PostsResponse response = repo.Edit(test); Post edited = repo.GetByCategory(1).Posts.First(); Assert.AreEqual(true, response.Success); Assert.AreEqual("HurrMurmuhFur", edited.PostTitle); }
private void LoadPostsResponse(PostsResponse posts) { this.Posts = posts.Data.Children.Select(x => new Post() { Title = x.Data.Title, LinkUrl = x.Data.Url, Id36 = x.Data.Id, NumComments = x.Data.NumComments, }).ToList(); this.after = posts.Data.After; this.before = posts.Data.Before; }
public PostsResponse GetPosts(int page, string orderField, string orderType) { var countPages = _dbService.GetCountPosts(); var response = new PostsResponse() { IsSuccess = true, CountPages = countPages / PAGE_SIZE + (countPages % PAGE_SIZE > 0 ? 1 : 0), Posts = _dbService.GetPosts(PAGE_SIZE * page, PAGE_SIZE, orderField, orderType) }; return(response); }
public override async Task <PostsResponse> GetPosts(Empty request, ServerCallContext context) { var posts = await RunOnDb(c => c.Posts.ToListAsync()); var response = new PostsResponse(); response.Posts.AddRange(posts.Select(p => new PostDto { Title = p.Title, Content = p.Content })); return(response); }
public IActionResult Index() { var client = new ButterCMSClient(apiToken); PostsResponse responses = client.ListPosts(1, 10); List <Post> posts = new List <Post>(); foreach (Post postResp in responses.Data) { posts.Add(postResp); } ViewData["allPosts"] = posts; return(View()); }
public PostsResponse Delete(int id) { var context = new PersonalBlogEntities(); if (context.Posts.FirstOrDefault(p => p.PostId == id) == null) { var response = new PostsResponse(); response.Success = false; response.Message = "There is no post in our database that matches the delete criteria."; return(response); } return(repo.Delete(id)); }
public async Task GetPostsAsync() { try { PostsResponse _postsResponse = await _PostsManager.GetPosts(_Person.Id); if (_postsResponse != null && _postsResponse.Posts != null && _postsResponse.Posts.Any()) { DrawPosts(_postsResponse.Posts); } } catch (Exception exception) { Console.Write($" : {exception}"); } }
public void PostsEndPointDeserializationTest() { // arrange RestClient client = RestOperations.ReturnRestClient(baseURL); RestRequest request = RestOperations.ReturnPostRequest(postsEndPoint, firstId); // act IRestResponse response = RestOperations.ExecuteRequest(client, request); PostsResponse postResponse = DesrializationOperation.PostDeserialize(response); // assert Assert.That(postResponse.ID, Is.EqualTo(firstId)); }
public PostsResponse Add(Post post) { var context = new PersonalBlogEntities(); var response = new PostsResponse(); if (string.IsNullOrEmpty(post.PostTitle)) { response.Success = false; response.Message = "The post title cannot be left blank."; } else if (post.CreatedDate < DateTime.Today.AddDays(1)) { response.Success = false; response.Message = "The post cannot have a creation date before the current date."; } //else if (!post.IsApproved) //{ // response.Success = false; // response.Message = "This post has content that violates our blogging policy."; //} else if (string.IsNullOrEmpty(post.PostBody)) { response.Success = false; response.Message = "The post body cannot be left blank."; } else if (context.Categories.FirstOrDefault(c => c.CategoryId == post.CategoryId) == null) { response.Success = false; response.Message = "That category is invalid"; } else { TagsRepo tagsRepo = new TagsRepo(); List <Tag> allTags = tagsRepo.GetAll().Tags.ToList(); List <Tag> tagsToAdd = post.Tags.AsEnumerable().Where(t => post.Tags.Any(postTag => postTag.TagName != t.TagName)).ToList(); foreach (Tag t in tagsToAdd) { tagsRepo.Add(t); } response = repo.Add(post); response.Message = $"The post \"{post.PostTitle}\" has been added to the database."; } return(response); }
public PostsResponse GetByCategory(string category) { var context = new PersonalBlogEntities(); var possibleCategory = context.Categories.FirstOrDefault(c => c.CategoryName.ToLower() == category.ToLower()); if (possibleCategory == null) { var response = new PostsResponse(); response.Success = false; response.Message = $"{category} is not a valid category."; return(response); } else { return(repo.GetByCategory(possibleCategory.CategoryId)); } }
public async Task <PostsResponse> UpdatePostText(long postId, [FromBody] string text) { PostsResponse response; try { await _postService.UpdatePostText(postId, text); return(new PostsResponse(true)); } catch (Exception e) { //Todo: Write exception details to log response = new PostsResponse($"An error was occurred on {nameof(UpdatePostText)}, please call admin."); } return(response); }
public async Task <PostsResponse> CreatePost([FromBody] Post p) { PostsResponse response; try { await _postService.CreatePost(p); return(new PostsResponse(true)); } catch (Exception e) { //Todo: Write exception details to log response = new PostsResponse($"An error was occurred on {nameof(CreatePost)}, please call admin."); } return(response); }
public async Task <PostsResponse> DeletePost(long postId) { PostsResponse response; try { await _postService.DeletePost(postId); return(new PostsResponse(true)); } catch (Exception e) { //Todo: Write exception details to log response = new PostsResponse($"An error was occurred on {nameof(DeletePost)}, please call admin."); } return(response); }
public PostsResponse GetByTitle(string title) { var context = new PersonalBlogEntities(); if (string.IsNullOrEmpty(title)) { return(repo.GetByTitle(title)); } if (context.Posts.FirstOrDefault(p => p.PostTitle == title) == null) { var response = new PostsResponse(); response.Success = false; response.Message = $"There are no posts that have the name of {title}"; return(response); } return(repo.GetByTitle(title)); }
public PostsResponse GetByTag(int tagId) { var context = new PersonalBlogEntities(); if (tagId == 0) { return(repo.GetByTag(tagId)); } if (context.Tags.FirstOrDefault(t => t.TagId == tagId) == null) { var response = new PostsResponse(); response.Success = false; response.Message = "That is not a valid tag."; return(response); } return(repo.GetByTag(tagId)); }
Task<PostsResponse> getPostsAsync(SocialNetworkDbContext context, List<String> following, int currentPage) { return Task.Factory.StartNew(() => { var filterIn = Builders<Post>.Filter.In(x => x.user, following); PostsResponse res = new PostsResponse(); var posts = context.Posts.Find(filterIn) .Skip((currentPage - 1) * Constants.pageSizePosts) .Limit(Constants.pageSizePosts) .SortByDescending(x => x.createAt) .ToListAsync().Result; var total = context.Posts.Find(filterIn).CountDocumentsAsync().Result; FillPosts(context, posts).ContinueWith(response => { res.posts = response.Result; res.pagination = new Pagination(CurrentUrlWithQuery, currentPage, Constants.pageSizePosts, total); }).Wait(); return res; }); }
public PostsResponse Add(Post post) { PostsResponse response = new PostsResponse(); try { using (var context = new PersonalBlogEntities()) { context.Posts.Add(post); context.SaveChanges(); response.Success = true; } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }