// PUT /api/categories/5 public HttpResponseMessage Put(int id, CategoryModel category) { if (ModelState.IsValid) { var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description); var result = commandBus.Submit(command); return new HttpResponseMessage(HttpStatusCode.NoContent); } throw new HttpResponseException(HttpStatusCode.BadRequest); }
// POST /api/categories public HttpResponseMessage<CategoryWithExpense> Post(CategoryModel category) { if (ModelState.IsValid) { var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description); var result = commandBus.Submit(command); if (result.Success) { var categoryWithExpense = new CategoryWithExpense { CategoryName = category.CategoryName, Description = category.Description, TotalExpenses = 0 }; var response = new HttpResponseMessage<CategoryWithExpense>(categoryWithExpense, HttpStatusCode.Created); string uri = Url.Route(null, new { id = category.CategoryId }); response.Headers.Location = new Uri(Request.RequestUri, uri); return response; } } throw new HttpResponseException(HttpStatusCode.BadRequest); }
// POST /api/category public HttpResponseMessage Post(CategoryModel category) { if (ModelState.IsValid) { var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description); var result = commandBus.Submit(command); if (result.Success) { var response = Request.CreateResponse(HttpStatusCode.Created, category); string uri = Url.Link("DefaultApi", new { id = category.CategoryId }); response.Headers.Location = new Uri(uri); return response; } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } throw new HttpResponseException(HttpStatusCode.BadRequest); }
public void Put_Category_Returns_OKStatusCode() { // Arrange commandBus.Setup(c => c.Submit(It.IsAny<CreateOrUpdateCategoryCommand>())).Returns(new CommandResult(true)); Mapper.CreateMap<CategoryFormModel, CreateOrUpdateCategoryCommand>(); CategoryController controller = new CategoryController(commandBus.Object, categoryRepository.Object) { Request = new HttpRequestMessage() { Properties = { { HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration() } } } }; // Act CategoryModel category = new CategoryModel(); category.CategoryId = 1; category.CategoryName = "Mock Category"; var response = controller.Put(category.CategoryId,category); // Assert Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); }
public void Post_EmptyCategory_Returns_BadRequestStatusCode() { // Arrange commandBus.Setup(c => c.Submit(It.IsAny<CreateOrUpdateCategoryCommand>())).Returns(new CommandResult(true)); Mapper.CreateMap<CategoryFormModel, CreateOrUpdateCategoryCommand>(); var httpConfiguration = new HttpConfiguration(); WebApiConfig.Register(httpConfiguration); var httpRouteData = new HttpRouteData(httpConfiguration.Routes["DefaultApi"], new HttpRouteValueDictionary { { "controller", "category" } }); var controller = new CategoryController(commandBus.Object, categoryRepository.Object) { Request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/category/") { Properties = { { HttpPropertyKeys.HttpConfigurationKey, httpConfiguration }, { HttpPropertyKeys.HttpRouteDataKey, httpRouteData } } } }; // Act CategoryModel category = new CategoryModel(); category.CategoryId = 0; category.CategoryName = ""; // The ASP.NET pipeline doesn't run, so validation don't run. controller.ModelState.AddModelError("", "mock error message"); var response = controller.Post(category); // Assert Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void Post_Category_Returns_CreatedStatusCode() { // Arrange commandBus.Setup(c => c.Submit(It.IsAny<CreateOrUpdateCategoryCommand>())).Returns(new CommandResult(true)); Mapper.CreateMap<CategoryFormModel, CreateOrUpdateCategoryCommand>(); var httpConfiguration = new HttpConfiguration(); WebApiConfig.Register(httpConfiguration); var httpRouteData = new HttpRouteData(httpConfiguration.Routes["DefaultApi"], new HttpRouteValueDictionary { { "controller", "category" } }); var controller = new CategoryController(commandBus.Object, categoryRepository.Object) { Request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/category/") { Properties = { { HttpPropertyKeys.HttpConfigurationKey, httpConfiguration }, { HttpPropertyKeys.HttpRouteDataKey, httpRouteData } } } }; // Act CategoryModel category = new CategoryModel(); category.CategoryId = 1; category.CategoryName = "Mock Category"; var response = controller.Post(category); // Assert Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); var newCategory = JsonConvert.DeserializeObject<CategoryModel>(response.Content.ReadAsStringAsync().Result); Assert.AreEqual(string.Format("http://localhost/api/category/{0}", newCategory.CategoryId), response.Headers.Location.ToString()); }