示例#1
0
        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);
        }
示例#2
0
        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 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);
        }