Example #1
0
        public async Task CanAddItemToLoot()
        {
            var repo = BuildRepo();

            await repo.AddItemToLoot(1, 3);

            EnemyDTO enemy = await repo.GetEnemy(1);

            EnemyLootDTO expected = new EnemyLootDTO()
            {
                EnemyId = 1,
                ItemId  = 3,
                Item    = new ItemDTO
                {
                    Id    = 3,
                    Name  = "Dungeon Key",
                    Value = "100 cp"
                }
            };
            bool found = false;

            foreach (var item in enemy.Loot)
            {
                if (item.ItemId == expected.ItemId)
                {
                    Assert.Equal(expected.EnemyId, item.EnemyId);
                    Assert.Equal(expected.Item.Name, item.Item.Name);
                    found = true;
                }
            }
            Assert.True(found);
        }
Example #2
0
        public async Task CanUpdateEnemies()
        {
            Enemy enemy = new Enemy()
            {
                Id        = 1,
                Abilities = "Slash",
                Type      = "Warrior",
            };

            var repo = BuildRepo();

            EnemyDTO enemyDTO = new EnemyDTO()
            {
                Id        = enemy.Id,
                Abilities = enemy.Abilities,
                Type      = enemy.Type
            };

            await repo.Update(enemyDTO);

            var result = await repo.GetEnemy(1);

            Assert.Equal(1, result.Id);
            Assert.Equal(result.Id, enemy.Id);
            Assert.Equal(result.Abilities, enemy.Abilities);
            Assert.Equal(result.Type, enemy.Type);
        }
        public async Task <ActionResult <EnemyDTO> > PostEnemy(EnemyDTO enemy)
        {
            await _enemy.Create(enemy);

            await _log.CreateLog(HttpContext, User.FindFirst("UserName").Value);

            return(CreatedAtAction("GetEnemy", new { id = enemy.Id }, enemy));
        }
        public async Task <IActionResult> Create(EnemyDTO enemy)
        {
            if (ModelState.IsValid)
            {
                await _enemy.Create(enemy);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(enemy));
        }
        public async Task <IActionResult> PutEnemy(int id, EnemyDTO enemy)
        {
            if (id != enemy.Id)
            {
                return(BadRequest());
            }
            var result = await _enemy.Update(enemy);

            await _log.CreateLog(HttpContext, User.FindFirst("UserName").Value);

            return(Ok(result));
        }
        /// <summary>
        /// Get a specific enemy in the database by ID
        /// </summary>
        /// <param name="id">Id of enemy to search for</param>
        /// <returns>Successful result of specified enemy</returns>
        public async Task <EnemyDTO> GetEnemy(int id)
        {
            Enemy enemy = await _context.Enemy.Where(x => x.Id == id).FirstOrDefaultAsync();

            EnemyDTO enemyDTO = new EnemyDTO()
            {
                Id        = enemy.Id,
                Abilities = enemy.Abilities,
                Type      = enemy.Type,
                Species   = enemy.Species.ToString()
            };

            enemyDTO.Loot = await GetEnemyLoot(enemyDTO.Id);

            return(enemyDTO);
        }
        /// <summary>
        /// Creates a new enemy in the database
        /// </summary>
        /// <param name="enemyDTO">enemy information for creation</param>
        /// <returns>Successful result of enemy creation</returns>
        public async Task <EnemyDTO> Create(EnemyDTO enemyDTO)
        {
            Enum.TryParse(enemyDTO.Species, out Species species);
            Enemy enemy = new Enemy()
            {
                Abilities = enemyDTO.Abilities,
                Type      = enemyDTO.Type,
                Species   = species
            };

            _context.Entry(enemy).State = EntityState.Added;
            await _context.SaveChangesAsync();

            enemyDTO.Id   = enemy.Id;
            enemyDTO.Loot = new List <EnemyLootDTO>();
            return(enemyDTO);
        }
        /// <summary>
        /// Update a given enemy in the database
        /// </summary>
        /// <param name="enemyDTO">Enemy information for update</param>
        /// <returns>Successful result of specified updated enemy</returns>
        public async Task <EnemyDTO> Update(EnemyDTO enemyDTO)
        {
            Enum.TryParse(enemyDTO.Species, out Species species);

            Enemy enemy = new Enemy()
            {
                Id        = enemyDTO.Id,
                Abilities = enemyDTO.Abilities,
                Type      = enemyDTO.Type,
                Species   = species
            };

            _context.Entry(enemy).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            enemyDTO.Loot = await GetEnemyLoot(enemyDTO.Id);

            return(enemyDTO);
        }
Example #9
0
        public async Task CanRemoveItemFromLoot()
        {
            var repo = BuildRepo();

            await repo.RemoveItemFromLoot(1, 2);

            EnemyDTO enemy = await repo.GetEnemy(1);

            EnemyLootDTO expected = new EnemyLootDTO()
            {
                EnemyId = 1,
                ItemId  = 2,
                Item    = new ItemDTO
                {
                    Id    = 2,
                    Name  = "Cup",
                    Value = "100 cp"
                }
            };

            Assert.DoesNotContain(expected, enemy.Loot);
        }
Example #10
0
        public async Task CanReadEnemy()
        {
            Enemy enemy = new Enemy()
            {
                Id        = 4,
                Abilities = "Firebreath",
                Type      = "Warrior",
            };

            var repo = BuildRepo();

            EnemyDTO enemyDTO = new EnemyDTO()
            {
                Id        = enemy.Id,
                Abilities = enemy.Abilities,
                Type      = enemy.Type
            };

            await repo.Create(enemyDTO);

            var result = await repo.GetEnemy(enemyDTO.Id);

            Assert.Equal(enemy.Id, result.Id);
        }
Example #11
0
        public async void CanCreateEnemy()
        {
            Enemy enemy = new Enemy()
            {
                Id        = 4,
                Abilities = "Firebreath",
                Type      = "Warrior",
            };

            var repository = BuildRepo();

            EnemyDTO enemyDTO = new EnemyDTO()
            {
                Id        = enemy.Id,
                Abilities = enemy.Abilities,
                Type      = enemy.Type
            };

            var saved = await repository.Create(enemyDTO);

            Assert.NotNull(saved);
            Assert.Equal(enemy.Abilities, saved.Abilities);
            Assert.Equal(enemy.Type, saved.Type);
        }