public IActionResult Post([FromBody] CustomerBaseDto dto) { if (dto == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = _provider.AddCustomer(dto); return(result == null?StatusCode(500, "A problem occurred while handling your request.") : CreatedAtRoute("GetCustomer", new { id = result.CustomerId }, result)); }
public CustomerDto AddCustomer(CustomerBaseDto customer) { try { var itemToCreate = Mapper.Map <CustomerDto>(customer); var id = MockData.Current.Customers.AsEnumerable().Max(m => m.CustomerId) + 1; itemToCreate.CustomerId = id; _rep.Add(itemToCreate); //if (!_rep.Save()) return null; return(itemToCreate); } catch (Exception e) { //Logger.ErrorException(e.Message, e); throw e; } }
public void Add_InvalidObjectMobileCharacterPassed_ReturnsBadRequest() { // Arrange var invalidMobileItem = new CustomerBaseDto { CustomerName = "Jack", ContactEmail = "*****@*****.**", MobileNo = "123456789m" }; // Act _controller.ValidateViewModel(invalidMobileItem); var badResponse = _controller.Post(invalidMobileItem); // Assert Assert.IsType <BadRequestObjectResult>(badResponse); }
public void Add_ValidObjectPassed_ReturnsCreatedResponse() { // Arrange var testItem = new CustomerBaseDto { CustomerName = "Jack", ContactEmail = "*****@*****.**", MobileNo = "1020304050" }; MoqSetupAdd(testItem); // Act _controller.ValidateViewModel(testItem); var createdResponse = _controller.Post(testItem); // Assert Assert.IsType <CreatedAtRouteResult>(createdResponse); }
public CustomerDto AddCustomer(CustomerBaseDto customer) { try { var itemToCreate = Mapper.Map <Customer>(customer); _rep.Add(itemToCreate); if (!_rep.Save()) { return(null); } var customerDto = Mapper.Map <CustomerDto>(itemToCreate); return(customerDto); } catch (Exception e) { //Logger.ErrorException(e.Message, e); throw e; } }
public void Add_ValidObjectPassed_ReturnedResponseHasCreatedItem() { // Arrange var testItem = new CustomerBaseDto() { CustomerName = "Jack", ContactEmail = "*****@*****.**", MobileNo = "1020304050" }; MoqSetupAdd(testItem); // Act _controller.ValidateViewModel(testItem); var createdResponse = _controller.Post(testItem) as CreatedAtRouteResult; var item = createdResponse?.Value as CustomerDto; var p = MockData.Current.Customers; // Assert Assert.IsType <CustomerDto>(item); Assert.Equal("Jack", item.CustomerName); }
public void Add_MissingObjectNamePassed_ReturnsBadRequest() { // Arrange var nameMissingItem = new CustomerBaseDto { ContactEmail = "*****@*****.**", MobileNo = "1020304050", }; MoqSetupAdd(nameMissingItem); // Act //See how the ValidateViewModel extension method in the Helper class is useful here _controller.ValidateViewModel(nameMissingItem); //I have used the above useful extension method to simulate validation instead of adding customly like below //_controller.ModelState.AddModelError("CustomerName", "Required"); var badResponse = _controller.Post(nameMissingItem); // Assert Assert.IsType <BadRequestObjectResult>(badResponse); }
private void MoqSetupAdd(CustomerBaseDto testItem) { _mockRepo.Setup(x => x.Add(It.Is <CustomerDto>(y => y == testItem))) .Callback <CustomerDto>(s => MockData.Current.Customers.Add(s)); }