public void Update_IncorrectBID_shouldReturnNull() { Blog_Detail response = controller.UpdateBlogById(incorrectBid, GenerateRandomBlog()); Assert.AreEqual(null, response.Title); Assert.AreEqual(null, response.Blog_Content); }
public HttpResponseMessage CreateBlog([FromBody] Blog_Detail blog) { var identity = this.User.Identity as ClaimsIdentity; var nonRoleClaims = identity.Claims.Where(x => x.Type != ClaimsIdentity.DefaultRoleClaimType).Select(x => new { Type = x.Type, Value = x.Value }).ToList(); var uid = Int32.Parse(nonRoleClaims[1].Value); blog.UID = uid; try { using (BloggerAppDBEntities entities = new BloggerAppDBEntities()) { blog.DateOfUpdation = DateTime.Now; entities.Blog_Detail.Add(blog); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, blog); message.Headers.Location = new Uri(Request.RequestUri + blog.Title.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage UpdateBlogById(int bid, [FromBody] Blog_Detail blog) { try { using (BloggerAppDBEntities entity = new BloggerAppDBEntities()) { var blogToEdit = entity.Blog_Detail.FirstOrDefault(e => e.BID == bid); if (blogToEdit != null) { blogToEdit.Title = blog.Title; blogToEdit.Blog_Content = blog.Blog_Content; blogToEdit.DateOfUpdation = DateTime.Now; entity.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, blogToEdit)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Blog with BID = " + bid + " Not found ! ")); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public void Update_CorrectBID_ShouldUpdate() { Blog_Detail RandomGeneratedBlog = GenerateRandomBlog(); Blog_Detail response = controller.UpdateBlogById(correctBid, RandomGeneratedBlog); bool BlogsAreEqual = response.Title == RandomGeneratedBlog.Title && response.Blog_Content == RandomGeneratedBlog.Blog_Content; Assert.IsTrue(BlogsAreEqual, "Blog having correct BID has been updated successfully! "); }
public void Update_BlogContentIsValid_ShouldUpdate() { var updatedBlog = GenerateRandomBlog(); var updatedTitle = updatedBlog.Title; var updatedContent = updatedBlog.Blog_Content; Blog_Detail response = controller.UpdateBlogById(correctBid, updatedBlog); Assert.AreEqual(updatedContent, response.Blog_Content); }
public Blog_Detail UpdateBlogById(int bid, [FromBody] Blog_Detail blog) { var result = new Blog_Detail(); if (blog.Title == null || blog.Blog_Content == null || (!ModelState.IsValid)) { throw new ArgumentNullException("blog Title or content cannot be null."); } using (BloggerAppDBEntities entity = new BloggerAppDBEntities()) { var blogToEdit = entity.Blog_Detail.FirstOrDefault(e => e.BID == bid); if (blogToEdit != null) { blogToEdit.Title = blog.Title; blogToEdit.Blog_Content = blog.Blog_Content; blogToEdit.DateOfUpdation = DateTime.Now; entity.SaveChanges(); result = blogToEdit; } return(result); } }