Ejemplo n.º 1
0
        /// <summary> Private function which updates a provided <seealso cref="AnimalOwnership">. </summary>
        /// <returns> bool value representing whether it successfully updated the <seealso cref="AnimalOwnership"> object </returns>
        private bool UpdateAnimal(AnimalOwnership ownedAnimal)
        {
            //Update the updated field to now
            ownedAnimal.LastUpdated = DateTime.Now;

            _context.Entry(ownedAnimal).State = EntityState.Modified;

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                //Relationship doesn't exist, fail
                if (!AnimalOwnershipExists(ownedAnimal.Id))
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }

            //Successfully updated the OwnedAnimal
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary> Validate Owned Animal state, whenever the animal is requested via the API validate the Animal doesn't need degradation on it's stats </summary>
        /// <returns> bool value representing whether it successfully validated and or degraded the <seealso cref="AnimalOwnership"> object </returns>
        private bool ValidateAndDegradeAnimal(AnimalOwnership ownedAnimal)
        {
            //New Animals with a default timestamp which is less than this year could be generated via auto creation, so set their update time to now.
            if (ownedAnimal.LastUpdated.Year < YEAR_OF_PRODUCTION)
            {
                //Animal is freshly made, set the last updated time to now and continue
                return(UpdateAnimal(ownedAnimal));
            }

            //Calculate the degradation multipler
            int degradationMultiplier = (int)Math.Floor((DateTime.Now - ownedAnimal.LastUpdated).TotalMilliseconds / DEGRADING_TIMEFRAME_IN_MILLISECONDS);

            //Min the degradation multiplier against a defined max value
            degradationMultiplier = Math.Min(MAX_DEGRADATION, degradationMultiplier);

            //Check if we need to apply stat degradation
            if (degradationMultiplier > 0)
            {
                //Degrade the animals stats providing the multiplier (time degradation times).
                ownedAnimal.DegradeStats(degradationMultiplier);

                //Return updated animal
                return(UpdateAnimal(ownedAnimal));
            }

            //Validation complete without any modifications to Animal status
            return(true);
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <OwnedAnimal> > StrokeAnimal(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.Stroke();

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

            await _context.SaveChangesAsync();

            return(new OwnedAnimal(ownedAnimal.Id, ownedAnimal.Name, ownedAnimal.Happiness, ownedAnimal.Hunger));
        }
Ejemplo n.º 4
0
        public void TestDegradeStatsByZero()
        {
            AnimalOwnership ownedAnimal          = context.AnimalOwnership.Find(ID_TO_FIND);
            Animal          ownedAnimalDefintion = context.Animal.Find(ownedAnimal.AnimalId);

            ownedAnimal.Animal = ownedAnimalDefintion;
            TestStatDegradation(ownedAnimal, ownedAnimalDefintion, 0);
        }
Ejemplo n.º 5
0
        public void TestFeed()
        {
            AnimalOwnership ownedAnimal   = context.AnimalOwnership.Find(ID_TO_FIND);
            int             currentHunger = ownedAnimal.Hunger;

            ownedAnimal.Feed();
            Assert.Equal(currentHunger - 1, ownedAnimal.Hunger);
        }
Ejemplo n.º 6
0
        public void TestStroke()
        {
            AnimalOwnership ownedAnimal      = context.AnimalOwnership.Find(ID_TO_FIND);
            int             currentHappiness = ownedAnimal.Happiness;

            ownedAnimal.Stroke();
            Assert.Equal(currentHappiness + 1, ownedAnimal.Happiness);
        }
Ejemplo n.º 7
0
        private void TestStatDegradation(AnimalOwnership ownedAnimal, Animal animal, int multiplier)
        {
            int currentHappiness = ownedAnimal.Happiness;
            int currentHunger    = ownedAnimal.Hunger;

            ownedAnimal.DegradeStats(multiplier);

            Assert.Equal(currentHappiness -= (animal.HappinessDecrease * multiplier), ownedAnimal.Happiness);
            Assert.Equal(currentHunger    += (animal.HungerIncrease * multiplier), ownedAnimal.Hunger);
        }
Ejemplo n.º 8
0
        public async Task TestPutAnimalOwnershipInappropriateId()
        {
            var             controller  = new AnimalOwnershipsController(context);
            AnimalOwnership ownedAnimal = context.AnimalOwnership.Find(ID_TO_FIND);

            var modifiedOwnedAnimal = await controller.PutAnimalOwnership(INAPPROPRIATE_ID_TO_FIND, ownedAnimal);

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

            Assert.IsType <BadRequestResult>(actionResult.Result);
        }
Ejemplo n.º 9
0
        public async Task TestPostAnimalOwnershipInappropriateId()
        {
            var             controller  = new AnimalOwnershipsController(context);
            AnimalOwnership ownedAnimal = context.AnimalOwnership.Find(ID_TO_FIND);

            ownedAnimal.AnimalId = INAPPROPRIATE_ID_TO_FIND;
            var newOwnedAnimal = await controller.PostAnimalOwnership(ownedAnimal);

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

            Assert.IsType <NotFoundResult>(actionResult.Result);
        }
Ejemplo n.º 10
0
        public async Task TestPutAnimalOwnershipAppropriateId()
        {
            var             controller  = new AnimalOwnershipsController(context);
            AnimalOwnership ownedAnimal = context.AnimalOwnership.Find(ID_TO_FIND);

            ownedAnimal.Name = CHANGED_TEXT;

            var modifiedOwnedAnimal = await controller.PutAnimalOwnership(ownedAnimal.Id, ownedAnimal);

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

            Assert.NotNull(actionResult);
            Assert.Equal(CHANGED_TEXT, actionResult.Value.Name);
            ownedAnimal = context.AnimalOwnership.Find(ID_TO_FIND);
            Assert.Equal(CHANGED_TEXT, ownedAnimal.Name);
        }
Ejemplo n.º 11
0
        public async Task TestDeleteAnimalOwnershipAppropriateId()
        {
            var             controller  = new AnimalOwnershipsController(context);
            AnimalOwnership ownedAnimal = context.AnimalOwnership.Find(ID_TO_FIND);

            Assert.False(ownedAnimal.IsDeleted);

            var deletedOwnedAnimal = await controller.DeleteAnimalOwnership(ID_TO_FIND);

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

            Assert.True(deletedOwnedAnimal.Value.IsDeleted);
            ownedAnimal = context.AnimalOwnership.Find(ID_TO_FIND);
            Assert.True(ownedAnimal.IsDeleted);
        }
Ejemplo n.º 12
0
        public void TestDegradeStatsByInappropriateValue()
        {
            AnimalOwnership ownedAnimal          = context.AnimalOwnership.Find(ID_TO_FIND);
            Animal          ownedAnimalDefintion = context.Animal.Find(ownedAnimal.AnimalId);

            ownedAnimal.Animal = ownedAnimalDefintion;

            int multiplier       = -1;
            int currentHunger    = ownedAnimal.Hunger;
            int currentHappiness = ownedAnimal.Happiness;

            ownedAnimal.DegradeStats(multiplier);

            Assert.Equal(currentHunger, ownedAnimal.Hunger);
            Assert.Equal(currentHappiness, ownedAnimal.Happiness);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
        public async Task TestPostAnimalOwnershipAppropriateId()
        {
            var             controller  = new AnimalOwnershipsController(context);
            AnimalOwnership ownedAnimal = context.AnimalOwnership.Find(ID_TO_FIND);

            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);
        }
Ejemplo n.º 15
0
        public async Task <ActionResult <OwnedAnimal> > GetAnimal(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);

            //Null check if the animal doesn't exist
            if (ownedAnimal == null)
            {
                return(NotFound());
            }


            //On retrieval validate and degrade appropriately
            if (!ValidateAndDegradeAnimal(ownedAnimal))
            {
                //If validating state failed, return bad request
                return(BadRequest());
            }

            return(new OwnedAnimal(ownedAnimal.Id, ownedAnimal.Name, ownedAnimal.Happiness, ownedAnimal.Hunger));
        }
Ejemplo n.º 16
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));
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
        public async Task <ActionResult <AnimalOwnership> > PutAnimalOwnership(long id, AnimalOwnership animalOwnership)
        {
            if (id != animalOwnership.Id)
            {
                return(BadRequest());
            }

            //Update the updated field
            animalOwnership.LastUpdated = DateTime.Now;

            _context.Entry(animalOwnership).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AnimalOwnershipExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(animalOwnership);
        }