public void SaveReturnsTrueIfChangesAreMade() { // Arrange var propertyToCreate = new PropertyForCreationDto() { Description = "Cosy semi-detached house in central Mansfield.", MarketValue = 95000, Rent = 450, Costs = 100, HouseNumber = 12, Street = "Newcastle Street", PostCode = "S70 2FF", Country = "United Kingdom", Tenants = new List <Tenant>() { new Tenant() { FirstName = "Some", LastName = "Dude" } } }; // Act var propertyToAdd = Mapper.Map <Property>(propertyToCreate); Sut.AddProperty(propertyToAdd); // Act var actual = Sut.Save(); // Assert Assert.That(actual, Is.True); }
public void CreatePropertyAddsNewPropertyToDbReturnsRouteAndObject() { // Arrange var propertyForCreation = new PropertyForCreationDto() { Description = "Lovely flat based in central Worksop.", MarketValue = 95000, Rent = 390, Costs = 120, HouseNumber = 22, Street = "Potter Street", PostCode = "S802AF", Country = "United Kingdom", Tenants = new List <Tenant>() { new Tenant() { FirstName = "Some", LastName = "Dude" } } }; // Act var result = Sut.CreateProperty(propertyForCreation) as CreatedAtRouteResult; // Assert Assert.That(result, Is.TypeOf <CreatedAtRouteResult>()); Assert.That(result.StatusCode, Is.EqualTo(201)); Assert.That(result.RouteValues.Values.FirstOrDefault(), Is.TypeOf <Guid>()); Assert.That(result.Value, Is.TypeOf <PropertyDto>()); }
//Implement mapper public async Task <IActionResult> CreateProperty([FromBody] PropertyForCreationDto property) { try { if (property == null) { _logger.LogError("Property received is a Null Object."); return(BadRequest("Property object is null. Please send full request.")); } else if (!ModelState.IsValid) { _logger.LogError("Invalid Property object sent from client."); return(BadRequest("Property object is not Valid")); } var propertyEntity = _mapper.Map <Property>(property); _repositoryWrapper.Property.CreateProperty(propertyEntity); await _repositoryWrapper.Save(); var createdProperty = _mapper.Map <PropertyWithAddressDto>(propertyEntity); var address = await _repositoryWrapper.Address.GetAddressById(createdProperty.AddressId); createdProperty.Address = _mapper.Map <AddressDto>(address); return(CreatedAtRoute("PropertyWithAddressByPropertyId", new { id = createdProperty.Id }, createdProperty)); } catch (Exception e) { _logger.LogError($"Something went wrong inside CreateProperty(propertForCreationDto) action: {e.Message}"); return(StatusCode(500, e.Message)); } }
public ActionResult Save(PropertyForCreationDto newProperty) { if (newProperty == null) { return(Json(new { Response = "Property is empty!" })); } if (!ModelState.IsValid) { return(Json(new { Response = "Invalid Property data" })); } try { var propertyToSave = Mapper.Map <PropertyForCreationDto, Property>(newProperty); _context.Properties.Add(propertyToSave); _context.SaveChanges(); } catch (Exception exception) { // TODO: Log Exception and notify devOps Console.WriteLine(exception); return(Json(new { Response = "Error saving Property to database. Our DevOps has been notified. Sorry for the incovenience" })); } return(Json(new { Response = "Successfully saved Property!" })); }
public void CreatePropertyReturnsBadRequestIfRequestBodyHasIncorrectData() { // Arrange PropertyForCreationDto propertyForCreation = null; // Act var result = Sut.CreateProperty(propertyForCreation) as BadRequestResult; // Assert Assert.That(result, Is.TypeOf <BadRequestResult>()); Assert.That(result.StatusCode, Is.EqualTo(400)); }
public IActionResult CreateProperty([FromBody] PropertyForCreationDto property) { if (property == null) { return(BadRequest()); } var propertyEntity = Mapper.Map <Property>(property); _propertyRepository.AddProperty(propertyEntity); if (!_propertyRepository.Save()) { throw new Exception("Creating property failed on save."); } var propertyToReturn = Mapper.Map <PropertyDto>(propertyEntity); return(CreatedAtRoute("GetProperty", new { id = propertyToReturn.Id }, propertyToReturn)); }
public void AddPropertyAddsPropertyToDbCorrectlyAssigningNewGuids() { // Arrange var propertyToCreate = new PropertyForCreationDto() { Description = "Cosy semi-detached house in central Mansfield.", MarketValue = 95000, Rent = 450, Costs = 100, HouseNumber = 12, Street = "Newcastle Street", PostCode = "S70 2FF", Country = "United Kingdom", Tenants = new List <Tenant>() { new Tenant() { FirstName = "Some", LastName = "Dude" } } }; // Act var propertyToAdd = Mapper.Map <Property>(propertyToCreate); Sut.AddProperty(propertyToAdd); Sut.Save(); var actual = Sut.GetProperties().FirstOrDefault(p => p.PostCode == "S70 2FF"); // Assert Assert.That(actual.Id, Is.Not.Null); Assert.That(actual.Tenants.FirstOrDefault().Id, Is.Not.Null); Assert.That(actual.Rent, Is.EqualTo(propertyToAdd.Rent)); }