public void TestFeed()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            // Wait until hunger is at maximum
            Thread.Sleep(2000);

            // Make sure that feeding reduces back to zero
            userAnimal.Feed(1);
            Assert.AreEqual(0, userAnimal.Hunger);

            // Make sure that hunger goes to -1
            userAnimal.Feed(1);
            Assert.AreEqual(-1, userAnimal.Hunger);

            // Make sure that hunger goes no lower than -1
            userAnimal.Feed(1);
            Assert.AreEqual(-1, userAnimal.Hunger);

            // Wait until hunger is at maximum
            Thread.Sleep(2000);

            // Make sure that feeding reduces back to -1 and no further
            userAnimal.Feed(3);
            Assert.AreEqual(-1, userAnimal.Hunger);
        }
        public void TestStroke()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            // Wait until happiness is at its lowest
            Thread.Sleep(4000);

            // Make sure that stroking reduces back to zero
            userAnimal.Stroke(1);
            Assert.AreEqual(0, userAnimal.Happiness);

            // Make sure that stroking can go up to 1
            userAnimal.Stroke(1);
            Assert.AreEqual(1, userAnimal.Happiness);

            // Make sure that stroking can go no further than 1
            userAnimal.Stroke(1);
            Assert.AreEqual(1, userAnimal.Happiness);

            // Wait until happiness is at its lowest
            Thread.Sleep(4000);

            // Make sure that stroking can go no further than 1
            userAnimal.Stroke(3);
            Assert.AreEqual(1, userAnimal.Happiness);
        }
        public void TestFindOne()
        {
            User user = new User()
            {
                DisplayName = "Some display name"
            };

            Animal animal = new Animal()
            {
                TypeName         = "Test animal",
                HungerPerSecond  = 0.5m,
                SadnessPerSecond = 0.4m
            };

            User user2 = new User()
            {
                DisplayName = "Some display name 2"
            };

            Animal animal2 = new Animal()
            {
                TypeName         = "Test animal 2",
                HungerPerSecond  = 0.5m,
                SadnessPerSecond = 0.4m
            };

            UserAnimal userAnimal;
            UserAnimal userAnimal2;

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Users.Add(user);
                context.Animals.Add(animal);

                userAnimal = new UserAnimal()
                {
                    UserId   = user.Id,
                    AnimalId = animal.Id,
                };

                userAnimal2 = new UserAnimal()
                {
                    UserId   = user2.Id,
                    AnimalId = animal2.Id,
                };

                context.UserAnimals.Add(userAnimal);
                context.UserAnimals.Add(userAnimal2);
                context.SaveChanges();
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                Assert.AreEqual(user.Id, service.FindOne(user.Id).UserId);
                Assert.AreEqual(animal.Id, service.FindOne(user.Id).AnimalId);
                Assert.IsNotNull(service.FindOne(user.Id).Animal);
            }
        }
        public void TestFeedInvalid()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            Assert.Catch <System.ArgumentException>(() => userAnimal.Feed(-0.1m));
            Assert.Catch <System.ArgumentException>(() => userAnimal.Feed(0));
        }
        public ActionResult <UserAnimal> Get(uint userId, uint id)
        {
            UserAnimal userAnimal = service
                                    .FindAll()
                                    .FirstOrDefault(ua => ua.UserId == userId && ua.AnimalId == id)
            ;

            if (userAnimal == null)
            {
                return(new NotFoundResult());
            }

            return(new JsonResult(userAnimal));
        }
        private async void likeButton_Clicked(object sender, EventArgs e)
        {
            await SwipeView1.InvokeSwipe((MLToolkit.Forms.SwipeCardView.Core.SwipeCardDirection) MLToolkit.Forms.SwipeCardView.Core.SwipeCardDirection.Right);

            SwipeView1.SupportedSwipeDirections = SwipeCardDirection.Right;
            var animal = await App.Database.GetAnimalAsync();

            var        a  = animal.ElementAt(iterator);
            UserAnimal ua = new UserAnimal
            {
                UserId   = Database.LoggedUser.UserId,
                AnimalId = a.AnimalId
            };
            await App.Database.SaveUserAnimalAsync(ua);


            iterator++;
        }
        public ActionResult <UserAnimal> Stroke(uint userId, uint id)
        {
            UserAnimal userAnimal = service
                                    .FindAll()
                                    .FirstOrDefault(ua => ua.UserId == userId && ua.AnimalId == id)
            ;

            if (userAnimal == null)
            {
                return(new NotFoundResult());
            }

            // Stroke the animal and update the database
            userAnimal.Stroke(STROKE_AMOUNT);
            service.Update(userAnimal);

            return(Get(userId, id));
        }
        public void TestUpdateInvalid()
        {
            User user = new User()
            {
                DisplayName = "Some display name"
            };

            Animal animal = new Animal()
            {
                TypeName         = "Test animal",
                HungerPerSecond  = 0.5m,
                SadnessPerSecond = 0.4m
            };

            UserAnimal userAnimal;

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Users.Add(user);
                context.Animals.Add(animal);

                userAnimal = new UserAnimal()
                {
                    UserId   = user.Id,
                    AnimalId = animal.Id,
                };
                context.UserAnimals.Add(userAnimal);
                context.SaveChanges();
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);
                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();

                // Test with an invalid user ID
                userAnimal.UserId = user.Id + 1;
                Assert.Catch <NotFoundException>(() => service.Update(userAnimal));

                // Test with an invalid animal ID
                userAnimal.UserId   = user.Id;
                userAnimal.AnimalId = animal.Id + 1;
                Assert.Catch <NotFoundException>(() => service.Update(userAnimal));
            }
        }
        private async void SwipeView1_Swiped(object sender, SwipedCardEventArgs e)
        {
            if (e.Direction.ToString() == "Right")
            {
                var animal = await App.Database.GetAnimalAsync();

                var        a  = animal.ElementAt(iterator);
                UserAnimal ua = new UserAnimal
                {
                    UserId   = Database.LoggedUser.UserId,
                    AnimalId = a.AnimalId
                };
                await App.Database.SaveUserAnimalAsync(ua);


                // iterator++;
            }

            iterator++;
        }
        public ActionResult <UserAnimal> Post(uint userId, [FromBody] uint animalId)
        {
            if (userService.FindOne(userId) == null)
            {
                return(new NotFoundResult());
            }

            try {
                UserAnimal userAnimal = new UserAnimal()
                {
                    UserId = userId, AnimalId = animalId
                };
                service.Add(userAnimal);

                return(new CreatedAtActionResult("Get", "UserAnimals", new { userId, id = animalId }, userAnimal));
            } catch (DuplicateEntryException e) {
                return(new BadRequestObjectResult(e.Message));
            } catch (NotFoundException e) {
                return(new BadRequestObjectResult(e.Message));
            }
        }
        public void TestHappinessDecrease()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            // Make sure that 1 second decreases Happiness by one unit (in this case 0.5)
            Thread.Sleep(1000);
            Assert.AreEqual(-0.25, userAnimal.Happiness);

            // Make sure half a second doesn't decrease Happiness by another half unit
            Thread.Sleep(500);
            Assert.AreEqual(-0.25, userAnimal.Happiness);

            // Make sure half a second more decrease Happiness by another unit
            Thread.Sleep(500);
            Assert.AreEqual(-0.5, userAnimal.Happiness);

            // Make sure that happiness doesn't go below the threshold
            Thread.Sleep(4000);
            Assert.AreEqual(-1, userAnimal.Happiness);
        }
        public void TestHungerIncreases()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            // Make sure that 1 second increases Hunger by one unit (in this case 0.5)
            Thread.Sleep(1000);
            Assert.AreEqual(0.5, userAnimal.Hunger);

            // Make sure half a second doesn't increase Hunger by another half unit
            Thread.Sleep(500);
            Assert.AreEqual(0.5, userAnimal.Hunger);

            // Make sure half a second more increase Hunger by another unit
            Thread.Sleep(500);
            Assert.AreEqual(1, userAnimal.Hunger);

            // Make sure that hunger doesn't go over the threshold
            Thread.Sleep(1000);
            Assert.AreEqual(1, userAnimal.Hunger);
        }
        public void TestUpdate()
        {
            User user = new User()
            {
                DisplayName = "Some display name"
            };

            Animal animal = new Animal()
            {
                TypeName         = "Test animal",
                HungerPerSecond  = 0.5m,
                SadnessPerSecond = 0.4m
            };

            UserAnimal userAnimal;

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Users.Add(user);
                context.Animals.Add(animal);

                userAnimal = new UserAnimal()
                {
                    UserId   = user.Id,
                    AnimalId = animal.Id,
                };
                context.UserAnimals.Add(userAnimal);
                context.SaveChanges();
            }

            // Check the initial state of the animal
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();

                Assert.AreEqual(0, userAnimal.Hunger);
                Assert.AreEqual(0, userAnimal.Happiness);
            }

            // Add an update (at this stage it won't really update much)
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0.5, userAnimal.Hunger);
                Assert.AreEqual(-0.4, userAnimal.Happiness);
            }

            // Feed the animal and update again - this will cause the internal dates to update
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                userAnimal.Feed(1);
                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0, userAnimal.Hunger);
                Assert.AreEqual(-0.8, userAnimal.Happiness);
            }

            // Stroke the animal and update again - this will cause the internal dates to update
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                userAnimal.Stroke(1);
                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0.5, userAnimal.Hunger);
                Assert.AreEqual(0, userAnimal.Happiness);
            }
        }
        public void TestHappinessNoAnimal()
        {
            UserAnimal userAnimal = new UserAnimal();

            Assert.Catch <System.InvalidOperationException>(() => { decimal x = userAnimal.Happiness; });
        }