public IHttpActionResult PostItem(DtoItem dtoItem)
        {
            //Validate supplied DTO
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (dtoItem == null)
            {
                return(BadRequest("Must supply item in body of request."));
            }

            if (dtoItem.Key == null)
            {
                return(BadRequest("Must supply key when creating item."));
            }

            //Get parent item
            Item potentialParent = null;

            if (dtoItem.Parent != null)
            {
                potentialParent = db.Items.Find(dtoItem.Parent);
                if (potentialParent == null)
                {
                    return(BadRequest("Can't find parent with specified Id"));
                }
                if (potentialParent is Leaf)
                {
                    throw new System.InvalidOperationException("Parent cannot be Leaf");
                }
            }

            //Create item and assign it to its parent as a child
            Item item;

            try
            {
                item = dtoItem.ToDomainObject((Node)potentialParent);

                if (potentialParent != null)
                {
                    ((Node)potentialParent).Children.Add(item);
                }
            }
            catch (InvalidDataException exception)
            {
                return(BadRequest(exception.Message));
            }

            item.Created();
            db.Items.Add(item);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = item.Id }, item.ToDto()));
        }
        public IHttpActionResult PutItem(int id, DtoItem dtoItem)
        {
            //Check dto received is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Check id matches
            if (dtoItem == null)
            {
                return(BadRequest("Must supply item in body of request."));
            }
            if (id != dtoItem.Id)
            {
                return(BadRequest("Id in url must match id in request body."));
            }

            //Get the itemfrom the db
            Item item = db.Items.Find(id);

            if (item == null)
            {
                return(StatusCode(HttpStatusCode.NotFound));
            }

            //Get the item's parent
            Item potentialParent = null;

            if (dtoItem.Parent != null)
            {
                potentialParent = db.Items.Find(dtoItem.Parent);
                if (potentialParent == null)
                {
                    return(BadRequest("Can't find parent with specified Id"));
                }
                if (potentialParent is Leaf)
                {
                    throw new System.InvalidOperationException("Parent cannot be Leaf");
                }
            }

            //Convert parent to domain object
            //Update the item's key to match the parent, and value if the item is a leaf
            try
            {
                Item new_item = dtoItem.ToDomainObject((Node)potentialParent);

                item.Key = new_item.Key;
                if (dtoItem is DtoLeaf)
                {
                    if (!(item is Leaf))
                    {
                        throw new System.InvalidOperationException("Item type must match.");
                    }
                    ((Leaf)item).Value = ((Leaf)new_item).Value;
                    ((Leaf)item).Stale = ((Leaf)new_item).Stale;
                }
            }
            catch (InvalidDataException exception)
            {
                return(BadRequest(exception.Message));
            }

            item.Updated();
            db.SaveChanges();
            return(Ok(item.ToDto()));
        }