// POST categories
        public IHttpActionResult Post(CategoryRepresentation value)
        {
            Category category;

            try
            {
                category = new Category()
                {
                    Title = value.Title
                };
            }
            catch
            {
                return(BadRequest());
            }

            db.Categories.Add(category);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = category.Id }, new CategoryRepresentation()
            {
                Id = category.Id,
                Title = category.Title
            }));
        }
        // GET categories/1
        public IHttpActionResult Get(int id)
        {
            var category = db.Categories.Include("Posts").ToList().Find(x => x.Id == id);

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

            var categoryRepresentation = new CategoryRepresentation()
            {
                Id    = category.Id,
                Title = category.Title,
                Posts = category.Posts.Select(x => new PostRepresentation()
                {
                    Id      = x.Id,
                    Title   = x.Title,
                    Content = x.Content
                }).ToList()
            };

            return(Ok(categoryRepresentation));
        }
        // PUT: categories/1
        public IHttpActionResult Put(int id, CategoryRepresentation newCategoryRepresentation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var newCategory = new Category()
            {
                Id    = newCategoryRepresentation.Id,
                Title = newCategoryRepresentation.Title,
                Posts = newCategoryRepresentation.Posts?.Select(x => new Post()
                {
                    Id    = x.Id,
                    Title = x.Title
                }).ToList() ?? new List <Post>(),
            };

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

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

                db.Entry(category).CurrentValues.SetValues(newCategory);

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

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }