Esempio n. 1
0
        public async Task <ActionResult <OwnedAnimal> > FeedAnimal(long id)
        {
            //This is configured to Eagerly load the foreign data, couldn't make lazy loading functional :@
            AnimalOwnership ownedAnimal = await _context.AnimalOwnership.Include(a => a.Animal).SingleOrDefaultAsync(i => i.Id == id);

            if (ownedAnimal == null)
            {
                //If validating state failed, return bad request
                return(BadRequest());
            }

            //Validate current state of animal
            if (!ValidateAndDegradeAnimal(ownedAnimal))
            {
                //If validating state failed, return bad request
                return(BadRequest());
            }

            //Stroke Animal
            ownedAnimal.Feed();

            //Persist state
            _context.Entry(ownedAnimal).State = EntityState.Modified;

            await _context.SaveChangesAsync();

            return(new OwnedAnimal(ownedAnimal.Id, ownedAnimal.Name, ownedAnimal.Happiness, ownedAnimal.Hunger));
        }
Esempio n. 2
0
        public void TestFeed()
        {
            AnimalOwnership ownedAnimal   = context.AnimalOwnership.Find(ID_TO_FIND);
            int             currentHunger = ownedAnimal.Hunger;

            ownedAnimal.Feed();
            Assert.Equal(currentHunger - 1, ownedAnimal.Hunger);
        }
Esempio n. 3
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. 4
0
        public async Task TestPostAnimalOwnershipAppropriateIdResetDefaults()
        {
            var             controller  = new AnimalOwnershipsController(context);
            AnimalOwnership ownedAnimal = context.AnimalOwnership.Find(ID_TO_FIND);

            //Increment the values
            ownedAnimal.Feed();
            ownedAnimal.Stroke();
            ownedAnimal.Id   = EXPECTED_SIZE_OF_ALL + 1;
            ownedAnimal.Name = CHANGED_TEXT;
            var newOwnedAnimal = await controller.PostAnimalOwnership(ownedAnimal);

            var actionResult = Assert.IsType <ActionResult <AnimalOwnership> >(newOwnedAnimal);

            Assert.IsType <CreatedAtActionResult>(actionResult.Result);
            Assert.NotNull(actionResult);
            long expectedSize = EXPECTED_SIZE_OF_ALL + 1;

            ownedAnimal = context.AnimalOwnership.Find(expectedSize);
            Assert.Equal(CHANGED_TEXT, ownedAnimal.Name);
            Assert.Equal(expectedSize, ownedAnimal.Id);
            Assert.Equal(ownedAnimal.Animal.HungerDefault, ownedAnimal.Hunger);
            Assert.Equal(ownedAnimal.Animal.HappinessDefault, ownedAnimal.Happiness);
        }