Esempio n. 1
0
        public void TestSetToDefaults()
        {
            AnimalOwnership ownedAnimal          = context.AnimalOwnership.Find(ID_TO_FIND);
            Animal          ownedAnimalDefintion = context.Animal.Find(ownedAnimal.AnimalId);

            ownedAnimal.Animal = ownedAnimalDefintion;

            ownedAnimal.Feed();
            ownedAnimal.Stroke();

            Assert.NotEqual(ownedAnimalDefintion.HappinessDefault, ownedAnimal.Happiness);
            Assert.NotEqual(ownedAnimalDefintion.HungerDefault, ownedAnimal.Hunger);

            ownedAnimal.SetToDefaults(ownedAnimalDefintion);
            Assert.Equal(ownedAnimalDefintion.HappinessDefault, ownedAnimal.Happiness);
            Assert.Equal(ownedAnimalDefintion.HungerDefault, ownedAnimal.Hunger);
        }
Esempio n. 2
0
        public async Task <ActionResult <AnimalOwnership> > PostAnimalOwnership(AnimalOwnership animalOwnership)
        {
            Animal animal = await _context.Animal.FindAsync(animalOwnership.AnimalId);

            //Check if the Animal exists
            if (animal == null)
            {
                return(NotFound());
            }

            //Validate default values have been appropriately set
            if (animalOwnership.Happiness != animal.HappinessDefault || animalOwnership.Hunger != animal.HungerDefault)
            {
                //Override defaults
                animalOwnership.SetToDefaults(animal);
            }

            _context.AnimalOwnership.Add(animalOwnership);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetAnimalOwnership", new { id = animalOwnership.Id }, animalOwnership));
        }