Ejemplo n.º 1
0
        // GET posts/1
        public IHttpActionResult Get(int id)
        {
            var post = db.Posts.Include("Categories").Include("Reviews").ToList().Find(x => x.Id == id);

            if (post == null)
            {
                return(NotFound());
            }

            var postRepresentation = new PostRepresentation()
            {
                Id         = post.Id,
                Content    = post.Content,
                Title      = post.Title,
                Categories = post.Categories.Select(x => new CategoryRepresentation()
                {
                    Id    = x.Id,
                    Title = x.Title
                }).ToList(),
                Reviews = post.Reviews.Select(x => new ReviewRepresentation()
                {
                    Id      = x.Id,
                    Content = x.Content
                }).ToList()
            };

            return(Ok(postRepresentation));
        }
Ejemplo n.º 2
0
        // POST reviews
        public IHttpActionResult Post(PostRepresentation value)
        {
            Post post;

            try
            {
                post = new Post()
                {
                    Content = value.Content,
                    Title   = value.Title
                };
            }
            catch
            {
                return(BadRequest());
            }

            db.Posts.Add(post);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = post.Id }, new PostRepresentation()
            {
                Id = post.Id,
                Title = post.Title,
                Content = post.Content
            }));
        }
        public void UpdatePostTest_TestsThatAPostIsUpdatedAndRetreivedAsExpected_VerifiesByDatabaseRetrieval()
        {
            var postController = _kernel.Get <PostController>();

            Assert.NotNull(postController);
            string title             = "Post # 1";
            string description       = "Description of Post # 1";
            string category          = "Category of Post # 1";
            string email             = "*****@*****.**";
            var    createPostCommand = new CreatePostCommand();

            createPostCommand.Title       = title;
            createPostCommand.Description = description;
            createPostCommand.Category    = category;
            createPostCommand.Email       = email;

            // Set the Current User's username(which is the same as his email), otherwise the request for posting a new Post will fail
            postController.User = new ClaimsPrincipal(new List <ClaimsIdentity>()
            {
                new ClaimsIdentity(new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, email)
                })
            });

            var    postIdHttpContent = postController.Post(JsonConvert.SerializeObject(createPostCommand));
            string postId            = ((OkNegotiatedContentResult <string>)postIdHttpContent).Content;

            IHttpActionResult  response      = (IHttpActionResult)postController.Get(postId);
            PostRepresentation retreivedPost = ((OkNegotiatedContentResult <PostRepresentation>)response).Content;

            Assert.NotNull(retreivedPost);

            Assert.AreEqual(title, retreivedPost.Title);
            Assert.AreEqual(description, retreivedPost.Description);
            Assert.AreEqual(category, retreivedPost.Category);

            string title2       = "Post # 2";
            string description2 = "Description of Post # 2";
            string category2    = "Category of Post # 2";

            var updatePostCommand = new UpdatePostCommand()
            {
                Category    = category2,
                Description = description2,
                Id          = postId,
                Title       = title2
            };

            postController.Put(JsonConvert.SerializeObject(updatePostCommand));

            response      = (IHttpActionResult)postController.Get(postId);
            retreivedPost = ((OkNegotiatedContentResult <PostRepresentation>)response).Content;
            Assert.NotNull(retreivedPost);

            Assert.AreEqual(title2, retreivedPost.Title);
            Assert.AreEqual(description2, retreivedPost.Description);
            Assert.AreEqual(category2, retreivedPost.Category);
        }
Ejemplo n.º 4
0
        // PUT: posts/1
        public IHttpActionResult Put(int id, PostRepresentation newPostRepresentation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var newPost = new Post()
            {
                Id      = newPostRepresentation.Id,
                Content = newPostRepresentation.Content,
                Title   = newPostRepresentation.Title,
                Reviews = newPostRepresentation.Reviews?.Select(x => new Review()
                {
                    Id      = x.Id,
                    Content = x.Content
                }).ToList() ?? new List <Review>(),
                Categories = newPostRepresentation.Categories?.Select(x => new Category()
                {
                    Id    = x.Id,
                    Title = x.Title
                }).ToList() ?? new List <Category>(),
            };

            if (id != newPostRepresentation.Id)
            {
                return(BadRequest());
            }

            try
            {
                var post = db.Posts.Include("Reviews").Include("Categories").Single(x => x.Id == id);

                db.Entry(post).CurrentValues.SetValues(newPost);

                foreach (var review in post.Reviews.ToList())
                {
                    // ReSharper disable once SimplifyLinqExpression
                    if (!newPost.Reviews.Any(x => x.Id == review.Id))
                    {
                        post.Reviews.Remove(review);
                    }
                }

                foreach (var category in post.Categories.ToList())
                {
                    // ReSharper disable once SimplifyLinqExpression
                    if (!newPost.Categories.Any(x => x.Id == category.Id))
                    {
                        post.Categories.Remove(category);
                    }
                }

                foreach (var review in newPost.Reviews)
                {
                    if (post.Reviews.Any(x => x.Id == review.Id))
                    {
                        continue;
                    }
                    db.Reviews.Attach(review);
                    post.Reviews.Add(review);
                }

                foreach (var category in newPost.Categories)
                {
                    if (post.Categories.Any(x => x.Id == category.Id))
                    {
                        continue;
                    }
                    db.Categories.Attach(category);
                    post.Categories.Add(category);
                }

                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public void AddNewCommentsToPostTest_TestsThatANewCommentIsAddedToPostAsExpected_VerifiesByDatabaseRetrieval()
        {
            var postController = _kernel.Get <PostController>();

            Assert.NotNull(postController);
            string title             = "Post # 1";
            string description       = "Description of Post # 1";
            string category          = "Category of Post # 1";
            string email             = "*****@*****.**";
            var    createPostCommand = new CreatePostCommand();

            createPostCommand.Title       = title;
            createPostCommand.Description = description;
            createPostCommand.Category    = category;
            createPostCommand.Email       = email;

            // Set the Current User's username(which is the same as his email), otherwise the request for posting a new Post will fail
            postController.User = new ClaimsPrincipal(new List <ClaimsIdentity>()
            {
                new ClaimsIdentity(new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, email)
                })
            });
            var    postIdHttpContent = postController.Post(JsonConvert.SerializeObject(createPostCommand));
            string postId            = ((OkNegotiatedContentResult <string>)postIdHttpContent).Content;

            IHttpActionResult  response      = (IHttpActionResult)postController.Get(postId);
            PostRepresentation retreivedPost = ((OkNegotiatedContentResult <PostRepresentation>)response).Content;

            Assert.NotNull(retreivedPost);

            var    authorId1     = "GandalfTheGrey";
            var    text1         = "You shall not pass";
            string emailComment1 = "*****@*****.**";

            postController.User = new ClaimsPrincipal(new List <ClaimsIdentity>()
            {
                new ClaimsIdentity(new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, emailComment1)
                })
            });
            AddCommentCommand addCommentCommand = new AddCommentCommand()
            {
                AuthorEmail = emailComment1,
                PostId      = postId,
                Text        = text1
            };

            postController.PostComment(JsonConvert.SerializeObject(addCommentCommand));
            var    authorId2     = "GandalfTheWhite";
            var    text2         = "I have returned to finish the job";
            string emailComment2 = "*****@*****.**";

            postController.User = new ClaimsPrincipal(new List <ClaimsIdentity>()
            {
                new ClaimsIdentity(new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, emailComment2)
                })
            });
            AddCommentCommand addCommentCommand2 = new AddCommentCommand()
            {
                AuthorEmail = emailComment2,
                PostId      = postId,
                Text        = text2
            };

            postController.PostComment(JsonConvert.SerializeObject(addCommentCommand2));
            response      = postController.Get(postId);
            retreivedPost = ((OkNegotiatedContentResult <PostRepresentation>)response).Content;
            Assert.NotNull(retreivedPost);
            Assert.AreEqual(2, retreivedPost.Comments.Count);
            Assert.AreEqual(emailComment1, retreivedPost.Comments[0].AuthorEmail);
            Assert.AreEqual(postId, retreivedPost.Comments[0].PostId);
            Assert.AreEqual(text1, retreivedPost.Comments[0].Text);
            Assert.AreEqual(emailComment2, retreivedPost.Comments[1].AuthorEmail);
            Assert.AreEqual(postId, retreivedPost.Comments[1].PostId);
            Assert.AreEqual(text2, retreivedPost.Comments[1].Text);
        }