public AnimalTypeCreationDTO GetFullAnimalTypeById(int id) { AnimalType animalType = animalTypeRepository.GetAnimalTypeById(id); AnimalTypeCreationDTO returnValue = Mapper.Map <AnimalTypeCreationDTO>(animalType); return(returnValue); }
public AnimalTypeCreatedDTO UpdateAnimalType(int id, AnimalTypeCreationDTO updatedAnimalType) { try { animalTypeRepository.GetAnimalTypeById(id); } //Handled by controller logic catch (InvalidOperationException) { throw; } //Unexpected result, throw internal server error catch (Exception) { throw; } AnimalType animalType = Mapper.Map <AnimalType>(updatedAnimalType); //Not needed if using EF database with sequential column animalType.Id = id; AnimalTypeCreatedDTO returnValue = Mapper.Map <AnimalTypeCreatedDTO>(animalTypeRepository.UpdateAnimalType(id, animalType)); return(returnValue); }
public IActionResult Update(int id, [FromBody] AnimalTypeCreationDTO animalType) { if (animalType == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var returnValue = animalTypeManager.UpdateAnimalType(id, animalType); } catch (InvalidOperationException) { return(NotFound($"{id} was not found")); } catch (Exception) { return(StatusCode(500)); } //As per API standards, returning NoContent(); //Personally I think it should return a 201 return(NoContent()); }
public void AnimalTypeCreationDTO_ShouldFailModelValidation_WhenFieldsOverMaxLength() { string largeString = HelperMethods.GenerateLargeString(100); AnimalTypeCreationDTO animalTypeCreation = new AnimalTypeCreationDTO() { AnimalTypeName = largeString, HappinessDeductionRate = 1, HungerIncreaseRate = 1 }; bool result = HelperMethods.CheckModelValidation(animalTypeCreation); Assert.IsFalse(result); animalTypeCreation = new AnimalTypeCreationDTO() { AnimalTypeName = "Bunny", HappinessDeductionRate = 100, HungerIncreaseRate = 1 }; result = HelperMethods.CheckModelValidation(animalTypeCreation); Assert.IsFalse(result); animalTypeCreation = new AnimalTypeCreationDTO() { AnimalTypeName = "Bunny", HappinessDeductionRate = 1, HungerIncreaseRate = 100 }; result = HelperMethods.CheckModelValidation(animalTypeCreation); }
public void PartialUpdateAnimalType_ShouldReturn404_WhenCalledWithInvalidId() { AnimalTypeCreationDTO animalTypePatch = new AnimalTypeCreationDTO() { AnimalTypeName = "Rabbit", HappinessDeductionRate = 2, HungerIncreaseRate = 3 }; string newName = "Rabbit"; var patch = new JsonPatchDocument <AnimalTypeCreationDTO>(); patch.Add(x => x.AnimalTypeName, newName); // https://github.com/aspnet/Mvc/issues/3586 //TryValidateModel breaks on Unit Tests var objectValidator = new Mock <IObjectModelValidator>(); objectValidator.Setup(o => o.Validate(It.IsAny <ActionContext>(), It.IsAny <ValidationStateDictionary>(), It.IsAny <string>(), It.IsAny <Object>())); animalTypeController.ObjectValidator = objectValidator.Object; var result = animalTypeController.PartialUpdate(100, patch); var response = result as NotFoundObjectResult; Assert.IsNotNull(response); Assert.AreEqual(404, response.StatusCode); Assert.AreEqual("100 was not found", response.Value); }
public void AnimalTypeCreationDTO_ShouldFailModelValidation_WhenRequiredFieldNotEntered() { AnimalTypeCreationDTO animalTypeCreationDTO = new AnimalTypeCreationDTO() { AnimalTypeName = "Rabbit" }; bool result = HelperMethods.CheckModelValidation(animalTypeCreationDTO); Assert.IsFalse(result); }
public AnimalTypeCreatedDTO CreateAnimalType(AnimalTypeCreationDTO animalTypeCreationDTO) { AnimalType animalType = Mapper.Map <AnimalType>(animalTypeCreationDTO); animalType.Id = GetNextId(); AnimalType result = animalTypeRepository.CreateAnimalType(animalType); AnimalTypeCreatedDTO returnValue = Mapper.Map <AnimalTypeCreatedDTO>(result); return(returnValue); }
public void AnimalTypeCreationDTO_ShouldPassModelValidation_WhenRequiredFieldAreEntered() { AnimalTypeCreationDTO AnimalTypeCreation = new AnimalTypeCreationDTO() { AnimalTypeName = "Bunny", HappinessDeductionRate = 1, HungerIncreaseRate = 1 }; bool result = HelperMethods.CheckModelValidation(AnimalTypeCreation); Assert.IsTrue(result); }
public IActionResult Create([FromBody] AnimalTypeCreationDTO user) { if (user == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var returnValue = animalTypeManager.CreateAnimalType(user); return(CreatedAtRoute("GetAnimalTypeById", new { id = returnValue.Id }, returnValue)); }
public void PartialUpdatedAnimalType_ShouldReturn204_WhenCalledWithValidRequest() { AnimalTypeCreationDTO animalTypePatch = new AnimalTypeCreationDTO() { AnimalTypeName = "Rabbit", HappinessDeductionRate = 2, HungerIncreaseRate = 3 }; string newName = "Rabbit"; var patch = new JsonPatchDocument <AnimalTypeCreationDTO>(); patch.Add(x => x.AnimalTypeName, newName); // https://github.com/aspnet/Mvc/issues/3586 //TryValidateModel breaks on Unit Tests var objectValidator = new Mock <IObjectModelValidator>(); objectValidator.Setup(o => o.Validate(It.IsAny <ActionContext>(), It.IsAny <ValidationStateDictionary>(), It.IsAny <string>(), It.IsAny <Object>())); animalTypeController.ObjectValidator = objectValidator.Object; var result = animalTypeController.PartialUpdate(1, patch); var response = result as NoContentResult; //Check if a response was received Assert.IsNotNull(response); var returnResult = animalTypeController.GetById(1); var returnResponse = returnResult as OkObjectResult; var returnValue = returnResponse.Value as AnimalTypeDTO; Assert.AreEqual(newName, returnValue.AnimalTypeName); }
public void UpdateAnimalType_ShouldReturn204_WhenValidId() { AnimalTypeCreationDTO animalType = new AnimalTypeCreationDTO() { AnimalTypeName = "Lovely Doggo", HappinessDeductionRate = 9, HungerIncreaseRate = 3 }; var result = animalTypeController.Update(1, animalType); var response = result as NoContentResult; Assert.IsNotNull(response); Assert.AreEqual(204, response.StatusCode); var getResult = animalTypeController.GetById(1); var getResponse = getResult as OkObjectResult; var getValue = getResponse.Value as AnimalTypeDTO; Assert.AreEqual(animalType.AnimalTypeName, getValue.AnimalTypeName); }
public void CreateAnimalType_ShouldReturn201_WhenCalledWithValidAnimalCreationDTO() { AnimalTypeCreationDTO animalTypeCreation = new AnimalTypeCreationDTO() { AnimalTypeName = "Hamster", HappinessDeductionRate = 2, HungerIncreaseRate = 9 }; var result = animalTypeController.Create(animalTypeCreation); var response = result as CreatedAtRouteResult; Assert.IsNotNull(response); Assert.AreEqual(201, response.StatusCode); Assert.AreEqual("GetAnimalTypeById", response.RouteName); Assert.IsTrue(response.Value is AnimalTypeCreatedDTO); AnimalTypeCreatedDTO animalTypeCreatedDTO = response.Value as AnimalTypeCreatedDTO; Assert.AreEqual(animalTypeCreation.AnimalTypeName, animalTypeCreatedDTO.AnimalTypeName); Assert.AreEqual(animalTypeCreation.HappinessDeductionRate, animalTypeCreatedDTO.HappinessDeductionRate); Assert.AreEqual(animalTypeCreation.HungerIncreaseRate, animalTypeCreatedDTO.HungerIncreaseRate); }
public IActionResult PartialUpdate(int id, [FromBody] JsonPatchDocument <AnimalTypeCreationDTO> patch) { AnimalTypeCreationDTO animalTypeCreationDTO = null; if (patch == null) { return(BadRequest()); } try { animalTypeCreationDTO = animalTypeManager.GetFullAnimalTypeById(id); } //Manager will throw exceptions to manage responses catch (InvalidOperationException) { return(NotFound($"{id} was not found")); } catch (Exception) { return(StatusCode(500)); } //Was initially going to add this as a manager method, however model state is built into controller making it much easier to handle here. patch.ApplyTo(animalTypeCreationDTO, ModelState); TryValidateModel(animalTypeCreationDTO); //If the updated model state is invalid, return bad request. if (!ModelState.IsValid) { return(BadRequest(ModelState)); } animalTypeManager.UpdateAnimalType(id, animalTypeCreationDTO); return(NoContent()); }