public override Task <BlogResponse> UpdateBlogUnary(BlogRequest request, Grpc.Core.ServerCallContext context) { Console.WriteLine("Server received a normal blog request. Server will response normally!!!"); var filter = new FilterDefinitionBuilder <BsonDocument>().Eq("_id", new ObjectId(request.Blog.Id)); var result = _mongoCollection.Find(filter).FirstOrDefault(); if (result == null) { throw new RpcException(new Status(StatusCode.NotFound, String.Format("Blog Id {0} not found", request.Blog.Id))); } Blog blog = request.Blog; BsonDocument doc = new BsonDocument(); doc.Add("author_id", blog.AuthorId); doc.Add("title", blog.Title); doc.Add("content", blog.Content); _mongoCollection.ReplaceOne(filter, doc); return(Task.FromResult(new BlogResponse() { Blog = blog })); }
RequestNotMatchingAnyBlog_ButWithASingleBlogInSystemWithMatchingHostButDifferentSubfolder_RedirectsToOnlyBlog () { //arrange var onlyBlog = new Blog { Host = "example.com", Subfolder = "not-sub" }; var pagedCollection = new PagedCollection <Blog> { onlyBlog }; pagedCollection.MaxItems = 1; var repository = new Mock <ObjectProvider>(); repository.Setup(r => r.GetBlog("example.com", "sub")).Returns((Blog)null); repository.Setup(r => r.GetBlog("example.com", "not-sub")).Returns(onlyBlog); repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns( pagedCollection); var service = new BlogLookupService(repository.Object, new HostInfo { BlogAggregationEnabled = false }); var blogRequest = new BlogRequest("example.com", "sub", new Uri("http://example.com/Subtext.Web/sub/bar"), false, RequestLocation.Blog, "/Subtext.Web"); //act BlogLookupResult result = service.Lookup(blogRequest); //assert Assert.IsNull(result.Blog); Assert.AreEqual("http://example.com/Subtext.Web/not-sub/bar", result.AlternateUrl.ToString()); }
public async Task <BlogPost> SendBlogPost(BlogRequest blog) { List <string> tags = blog.TagList; var tag = new Tag(); List <Tag> list = new List <Tag>(); foreach (String s in tags) { tag = new Tag { TagName = s }; list.Add(tag); } //tagRepository.Insert(tag); var newBlog = new BlogPost { Title = blog.Title, Description = blog.Description, Body = blog.Body, UpdatedAt = DateTime.Now, TagList = list }; blogRepository.Insert(newBlog); return(newBlog); }
public async Task <bool> Post(BlogRequest request, IOutputPort <BlogResponse> response) { if (string.IsNullOrWhiteSpace(request.Title)) { response.Handle(new BlogResponse(new[] { new Error("error", "Blog Title Is Not Null!") })); return(false); } if (string.IsNullOrWhiteSpace(request.Content)) { response.Handle(new BlogResponse(new[] { new Error("error", "Blog Content Is Not Null!") })); return(false); } if (request.CreateUser <= 0) { response.Handle(new BlogResponse(new[] { new Error("error", "Failed To Get Session Information!") })); return(false); } var row = new BlgBlogPost() { Title = request.Title, Contents = request.Content, CreateUser = request.CreateUser, CreateDate = request.CreateDate }; repositories.BlgBlogPostRepository.Create(row); await repositories.BlgBlogPostRepository.SaveChangesAsync(); response.Handle(new BlogResponse(row, true)); return(true); }
public async Task <bool> Delete(BlogRequest request, IOutputPort <BlogResponse> response) { if (request.BlogPostId <= 0) { response.Handle(new BlogResponse(new[] { new Error("error", "Blog ID Is Not Null!") })); return(false); } if (request.UpdateUser <= 0) { response.Handle(new BlogResponse(new[] { new Error("error", "Failed To Get Session Information!") })); return(false); } var row = repositories.BlgBlogPostRepository.FindByCondition(p => p.Id == request.BlogPostId && !p.IsDeleted).Result; if (row == null) { response.Handle(new BlogResponse(new[] { new Error("error", "Failed To Get Blog Post Information!") })); return(false); } row.IsDeleted = true; row.UpdateUser = request.UpdateUser; row.UpdateDate = request.UpdateDate; repositories.BlgBlogPostRepository.Update(row); await repositories.BlgBlogPostRepository.SaveChangesAsync(); response.Response(new BlogResponse(true)); return(true); }
public void RequestWithSubfolderNotMatchingAnyBlog_ButWithAggregateBlogsEnabledAndMoreThanOneActiveBlogsInTheSystem_ReturnsNull() { //arrange var repository = new Mock <ObjectProvider>(); repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns((Blog)null); var blog1 = new Blog { Host = "example.com", Subfolder = "not-sub" }; var blog2 = new Blog { Host = "example.com", Subfolder = "not-sub-2" }; var pagedCollection = new PagedCollection <Blog> { blog1, blog2 }; pagedCollection.MaxItems = 2; repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns( pagedCollection); var aggregateBlog = new Blog(); var service = new BlogLookupService(repository.Object, new HostInfo { BlogAggregationEnabled = true, AggregateBlog = aggregateBlog }); var blogRequest = new BlogRequest("example.com", "blog1234", new Uri("http://example.com/foo/bar"), false); //act BlogLookupResult result = service.Lookup(blogRequest); //assert Assert.IsNull(result); }
public void ConvertRequestToBlogRequest_WithRequestForInstallationDirectory_ReturnsNullBlog() { //arrange var service = new Mock <IBlogLookupService>(); service.Setup(s => s.Lookup(It.IsAny <BlogRequest>())).Throws( new InvalidOperationException("Should not be called")); var httpResponse = new Mock <HttpResponseBase>(); httpResponse.Setup(r => r.Redirect(It.IsAny <string>(), true)).Throws( new InvalidOperationException("Method should not have been called")); Mock <HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/Install/Anything.aspx", true); httpRequest.Setup(r => r.FilePath).Returns("/Install/Anything.aspx"); var httpContext = new Mock <HttpContextBase>(); httpContext.Setup(c => c.Request).Returns(httpRequest.Object); httpContext.Setup(c => c.Response).Returns(httpResponse.Object); var module = new BlogRequestModule(service.Object); //act BlogRequest request = module.ConvertRequestToBlogRequest(httpContext.Object); //assert Assert.IsNull(request.Blog); }
RequestNotMatchingAnyBlog_ButWithASingleBlogInSystemWithLocalHostButNotMatchingSubfolder_ReturnsUpdatesItsHostThenRedirectsToSubfolder () { //arrange var onlyBlog = new Blog { Host = "localhost", Subfolder = "sub" }; var pagedCollection = new PagedCollection <Blog> { onlyBlog }; pagedCollection.MaxItems = 1; var repository = new Mock <ObjectProvider>(); repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns((Blog)null); repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns( pagedCollection); var service = new BlogLookupService(repository.Object, new HostInfo { BlogAggregationEnabled = false }); var blogRequest = new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false); //act BlogLookupResult result = service.Lookup(blogRequest); //assert Assert.IsNull(result.Blog); Assert.IsNotNull(result.AlternateUrl); Assert.AreEqual("http://example.com/sub/foo/bar", result.AlternateUrl.ToString()); Assert.AreEqual("example.com", onlyBlog.Host); repository.Verify(r => r.UpdateBlog(It.IsAny <Blog>())); }
public void ConvertRequestToBlogRequest_WithRequestForAlternateHost_RedirectsToPrimaryHost() { //arrange var service = new Mock <IBlogLookupService>(); service.Setup(s => s.Lookup(It.IsAny <BlogRequest>())).Returns(new BlogLookupResult(null, new Uri( "http://www.example.com/"))); var httpResponse = new Mock <HttpResponseBase>(); httpResponse.Stub(r => r.StatusCode); httpResponse.Stub(r => r.StatusDescription); httpResponse.Stub(r => r.RedirectLocation); Mock <HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/", true); var httpContext = new Mock <HttpContextBase>(); httpContext.Setup(c => c.Request).Returns(httpRequest.Object); httpContext.Setup(c => c.Response).Returns(httpResponse.Object); var module = new BlogRequestModule(service.Object); //act BlogRequest request = module.ConvertRequestToBlogRequest(httpContext.Object); //assert Assert.IsNull(request); httpResponse.Verify(r => r.End()); Assert.AreEqual(301, httpResponse.Object.StatusCode); Assert.AreEqual("301 Moved Permanently", httpResponse.Object.StatusDescription); Assert.AreEqual("http://www.example.com/", httpResponse.Object.RedirectLocation); }
public void ConvertRequestToBlogRequest_WithRequestForCorrectHost_ReturnsBlogRequest() { //arrange var service = new Mock <IBlogLookupService>(); service.Setup(s => s.Lookup(It.IsAny <BlogRequest>())).Returns( new BlogLookupResult(new Blog { IsActive = true }, null)); var httpResponse = new Mock <HttpResponseBase>(); httpResponse.Setup(r => r.End()).Throws( new InvalidOperationException("This method should not have been called")); Mock <HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/", true); var httpContext = new Mock <HttpContextBase>(); httpContext.Setup(c => c.Request).Returns(httpRequest.Object); httpContext.Setup(c => c.Response).Returns(httpResponse.Object); var module = new BlogRequestModule(service.Object); //act BlogRequest request = module.ConvertRequestToBlogRequest(httpContext.Object); //assert Assert.IsNotNull(request); }
public async Task <(string, bool)> UpdateBlogPost(int blogId, BlogRequest request) { try { var blog = await _context.Blogs.Where(p => p.Id == blogId).FirstOrDefaultAsync(); if (blog == null) { return("NOTFOUND", false); } if (blog.Deleted == true) { return("NOTFOUND", true); } blog.Title = request.Title; blog.Excerpt = request.Excerpt; blog.Content = request.Content; blog.CoverImagePath = request.CoverImagePath; blog.UpdatedAt = DateTime.Now; _context.Blogs.Update(blog); if (await _context.SaveChangesAsync() > 0) { return("OK", true); } return("DB ERROR", false); } catch (Exception ex) { return("SERVER ERROR", false); } }
public void ConvertRequestToBlogRequestForStaticImage_WithNoMatchingBlog_DoesNotRedirect() { //arrange var service = new Mock <IBlogLookupService>(); var result = new BlogLookupResult(null, new Uri("http://localhost/images/blog/services/identiconhandler.ashx")); service.Setup(s => s.Lookup(It.IsAny <BlogRequest>())).Returns(result); var httpResponse = new Mock <HttpResponseBase>(); httpResponse.Setup(r => r.Redirect(It.IsAny <string>(), true)).Throws(new InvalidOperationException("Should not redirect")); Mock <HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/images/services/identiconhandler.ashx", true); var httpContext = new Mock <HttpContextBase>(); httpContext.Setup(c => c.Request).Returns(httpRequest.Object); httpContext.Setup(c => c.Response).Returns(httpResponse.Object); var module = new BlogRequestModule(service.Object); //act BlogRequest request = module.ConvertRequestToBlogRequest(httpContext.Object); //assert Assert.IsNotNull(request); Assert.IsNull(request.Blog); Assert.AreEqual(RequestLocation.StaticFile, request.RequestLocation); }
public void Ctor_WithRequestForInstallation_SetsBlogNotRequiredTrue() { //arrange, act var blogRequest = new BlogRequest(null, null, new Uri("http://example.com"), false, RequestLocation.Installation, "/"); //assert Assert.IsTrue(blogRequest.BlogNotRequired); }
public async Task <ActionResult> Search([FromBody] BlogRequest request) { await blogPostUseCase.Search(new BlogRequest() { SearchKey = request.SearchKey }, blogPostPresenter); return(blogPostPresenter.ContentResult); }
public async Task <ActionResult> Post([FromBody] BlogRequest request) { await blogPostUseCase.Post(new BlogRequest() { Title = request.Title, Content = request.Content, CreateUser = CurrentUserId(), CreateDate = DateTime.Now }, blogPostPresenter); return(blogPostPresenter.ContentResult); }
public async Task <ActionResult> Delete([FromBody] BlogRequest request) { await blogPostUseCase.Delete(new BlogRequest() { BlogPostId = request.BlogPostId, UpdateUser = CurrentUserId(), UpdateDate = DateTime.Now }, blogPostPresenter); return(blogPostPresenter.ContentResult); }
public void Ctor_WithRequestForLoginPage_SetsBlogNotRequiredFalse() { //arrange, act var blogRequest = new BlogRequest(null, null, new Uri("http://example.com"), false, RequestLocation.LoginPage, "/"); //assert Assert.IsFalse(blogRequest.BlogNotRequired); }
public BlogConsumer(Channel channel) { _channel = channel; _blog = new Blog(); _blogRequest = new BlogRequest() { Blog = _blog }; _blogServiceClient = new BlogServiceClient(_channel); }
public async Task <ActionResult> Update([FromBody] BlogRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var res = await _blogService.UpdateAsync(request); return(Ok(res)); }
public void Ctor_WithRequestForStaticFile_SetsRequestLocationToStaticFile() { //arrange Mock <HttpRequestBase> request = CreateRequest("example.com", "/", "/Install/anything.css", true); //act var blogRequest = new BlogRequest(request.Object); //assert Assert.AreEqual(RequestLocation.StaticFile, blogRequest.RequestLocation); }
public void Ctor_WithRequestForSystemMessage_SetsRequestLocationToSystemMessages() { //arrange Mock <HttpRequestBase> request = CreateRequest("example.com", "/", "/SystemMessages/anything.aspx", true); //act var blogRequest = new BlogRequest(request.Object); //assert Assert.AreEqual(RequestLocation.SystemMessages, blogRequest.RequestLocation); }
public void Ctor_WithRequestWithSubfolder_CreatesBlogRequestWithSubfolder() { //arrange Mock <HttpRequestBase> request = CreateRequest("example.com", "/", "/foo/bar", true); //act var blogRequest = new BlogRequest(request.Object); //assert Assert.AreEqual("foo", blogRequest.Subfolder); }
public void Ctor_WithHostHavingPort_StripsPort() { //arrange Mock <HttpRequestBase> request = CreateRequest("example.com:1234", "/", "/foo/bar", true); //act var blogRequest = new BlogRequest(request.Object); //assert Assert.AreEqual("example.com", blogRequest.Host); }
public void Ctor_WithRequestHavingNoHostInParameters_CreatesBlogRequestWithHostAuthority() { //arrange Mock <HttpRequestBase> request = CreateRequest("example.com", "/", "/foo/bar", true); //act var blogRequest = new BlogRequest(request.Object); //assert Assert.AreEqual("example.com", blogRequest.Host); }
public void Ctor_WithRequestForLoginPage_SetsRequestLocationToLogin() { //arrange Mock <HttpRequestBase> request = CreateRequest("example.com", "/", "/login.aspx", true); //act var blogRequest = new BlogRequest(request.Object); //assert Assert.AreEqual(RequestLocation.LoginPage, blogRequest.RequestLocation); }
public IActionResult AddBlog(BlogRequest request) { CreateBlogUseCase createBlogUseCase = new CreateBlogUseCase(_BlogsRepository, _UsersRepository); UseCaseBaseOutput <bool> useCaseBaseOutput = createBlogUseCase.Process(request); if (useCaseBaseOutput.httpStatus != StatusCodes.Status200OK) { return(StatusCode(useCaseBaseOutput.httpStatus, "Creating a new Blog failed please try again")); } return(StatusCode(useCaseBaseOutput.httpStatus)); }
public BlogModel AddBlog(BlogRequest request) { var blog = new Blog { Url = request.Url }; blog = _repository.Add(blog); return(new BlogModel { Id = blog.Id, Url = blog.Url }); }
public void Ctor_WithRequestForHostAdmin_SetsRequestLocationToHostAdmin() { //arrange Mock <HttpRequestBase> request = CreateRequest("example.com", "/", "/HostAdmin/anything.aspx", true); //act var blogRequest = new BlogRequest(request.Object); //assert Assert.AreEqual(RequestLocation.HostAdmin, blogRequest.RequestLocation); }
public void Ctor_WithRequestForInstallDirectoryRoot_SetsRequestLocationToInstallDirectory() { //arrange Mock <HttpRequestBase> request = CreateRequest("example.com", "/", "/Install", true); //act var blogRequest = new BlogRequest(request.Object); //assert Assert.AreEqual(RequestLocation.Installation, blogRequest.RequestLocation); }
public async Task <IActionResult> PostMethod([FromBody] BlogRequest blogRequest) { if (blogRequest == null || blogRequest.Title == null || blogRequest.Description == null || blogRequest.Body == null) { return(BadRequest()); } var response = await _service.SendBlogPost(blogRequest); return(Ok(response)); }