// POST /api/projects/4/researchItems
        public virtual HttpResponseMessage Post(int projectId, ResearchItem researchItem)
        {
            try {
                // Make sure it is being inserted to a project owned by the user.
                var project = _projectRepository.GetByUser(projectId, User.Identity.Name);
                if (project == null) {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Project not found for user " + User.Identity.Name + ".");
                }
                researchItem.ProjectId = projectId;
                researchItem.UserName = User.Identity.Name;
                researchItem.DateCreated = DateTime.Now;
                researchItem.DateUpdated = DateTime.Now;
                _researchItemRepository.Insert(researchItem);
                _researchItemRepository.Commit();
            } catch (Exception ex) {
                var error = new HttpError("Error inserting research item: " + ex.Message) { { "Trace", ex.StackTrace } };
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
            }
            var response = Request.CreateResponse(HttpStatusCode.Created, researchItem);
            string uri = Url.Link("ProjectResearchItemsApi", new { projectId = projectId, researchItemId = researchItem.ResearchItemId });
            response.Headers.Location = new Uri(uri);
            return response;

        }
        // PUT /api/projects/4/researchitems/5
        public virtual HttpResponseMessage Put(int projectId, int researchItemId, ResearchItem researchItem)
        {
            var project = new Project();
            ResearchItem currentResearchItem = null;
            try
            {
                project = _projectRepository.GetByUser(projectId, User.Identity.Name);
                if (project == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Project not found for user " + User.Identity.Name + ".");
                }
                currentResearchItem = _researchItemRepository.GetByUser(researchItemId, User.Identity.Name);
                if (currentResearchItem == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Research item not found for user " + User.Identity.Name + ".");
                }
                currentResearchItem.DateUpdated = DateTime.Now;
                currentResearchItem.Subject = researchItem.Subject;
                currentResearchItem.Description = researchItem.Description;
                _researchItemRepository.Commit();
            }
            catch (Exception ex)
            {
                var error = new HttpError("Error updating research item: " + ex.Message) { { "Trace", ex.StackTrace } };
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
            }

            var response = Request.CreateResponse(HttpStatusCode.OK, currentResearchItem);
            string uri = Url.Link("ProjectResearchItemsApi", new { projectId = projectId, researchItemId = researchItem.ResearchItemId });
            response.Headers.Location = new Uri(uri);
            return response;
        }
        public void Put_James_ResearchItem_Returns_Null_ResearchItem_For_John(string version)
        {
            //Setup
            _projectRepository = _mockRepositories.GetProjectsRepository(ReturnType.Normal);
            _researchItemRepository = _mockRepositories.GetResearchItemsRepository(ReturnType.Normal);
            var researchItemsController = SetupController(_researchItemRepository.Object, _projectRepository.Object, "john", HttpMethod.Put, version);
            var inputResearchItem = new ResearchItem() { ResearchItemId = 1, ProjectId = 3, Subject = "Update Test 1", UserName = "******", Description = "Insert Test Description" };

            //Act
            var response = researchItemsController.Put(3, 1, inputResearchItem);
            dynamic errorMessage = JObject.Parse(response.Content.ReadAsStringAsync().Result);

            //Assert
            Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode, "Expecting a Not Found Status Code");
            Assert.AreEqual("Research item not found for user john.", (string)errorMessage.Message);
        }
        public void Put_ResearchItem_Database_Exception_Returns_Error(string version)
        {
            //Setup
            _projectRepository = _mockRepositories.GetProjectsRepository(ReturnType.Normal);
            _researchItemRepository = _mockRepositories.GetResearchItemsRepository(ReturnType.Exception);
            var researchItemsController = SetupController(_researchItemRepository.Object, _projectRepository.Object, "james", HttpMethod.Put, version);
            var inputResearchItem = new ResearchItem() { ResearchItemId = 1, ProjectId = 1, Subject = "Update Test 1", UserName = "******", Description = "Insert Test Description" };

            //Act
            var response = researchItemsController.Put(1, 1, inputResearchItem);
            dynamic errorMessage = JObject.Parse(response.Content.ReadAsStringAsync().Result);

            //Assert
            Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode, "Expecting an Internal Server Error Status Code");
            Assert.AreEqual("Error updating research item: Database exception!", (string)errorMessage.Message);
        }
        public void Put_ResearchItem_Returns_Expected_Header_For_James(string version)
        {
            //Setup
            _projectRepository = _mockRepositories.GetProjectsRepository(ReturnType.Normal);
            _researchItemRepository = _mockRepositories.GetResearchItemsRepository(ReturnType.Normal);
            var researchItemsController = SetupController(_researchItemRepository.Object, _projectRepository.Object, "james", HttpMethod.Put, version);
            var inputResearchItem = new ResearchItem() { ResearchItemId = 1, ProjectId = 1, Subject = "Update Test 1", UserName = "******", Description = "Insert Test Description" };

            //Act
            var response = researchItemsController.Put(1, 1, inputResearchItem);

            //Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Expecting an OK Status Code");
            Assert.AreEqual("http://localhost/api/projects/1/researchItems/1", response.Headers.Location.ToString());
        }