public void Post() { // Arrange IRepository<Employee> repository = new FileSystemRepository(new TestFileHelper()); EmployeeController controller = new EmployeeController(repository); int lengthBefore = controller.GetAllEmployees().Count(); // Act Employee e = new Employee {id = 4, name="test",age=45,sex="female"}; controller.PostEmployee(e); int lengthAfter = controller.GetAllEmployees().Count(); // Assert Assert.AreEqual(lengthBefore + 1, lengthAfter); }
// POST api/v1/employee/ // Creates a new employee resource with a given body public HttpResponseMessage PostEmployee(Employee item) { try { item = repository.AddItem(item); if (item == null) throw new ArgumentNullException(); var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, item); string uri = Url.Link("DefaultApi", new { id = item.id }); response.Headers.Location = new Uri(uri); return response; } catch (ArgumentNullException) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); } catch(Exception) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }
// PUT api/v1/employee/ // Updates a employee resource with a given body public HttpResponseMessage PutEmployee(Employee item) { try { item = repository.UpdateItem(item); if(item==null) { throw new NullReferenceException(); } var response = Request.CreateResponse<Employee>(HttpStatusCode.OK, item); return response; } catch(NullReferenceException) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); } catch(Exception) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }