Example #1
0
        public async Task <IActionResult> PostAsync([FromBody] CharacterModel characterModel)
        {
            var houseModel = await this.potterAPIClient.GetHouseAsync(characterModel.House);

            if (houseModel?.Id == null)
            {
                return(NotFound("House not exists."));
            }

            var character = CharacterTranslate.To(characterModel);

            await this.characterService.AddAsync(character);

            return(Ok());
        }
Example #2
0
        public async Task <IActionResult> GetAsync(Guid id)
        {
            var character = await this.characterService.GetAsync(id);

            if (character == null)
            {
                return(NotFound());
            }

            var characterModel = CharacterTranslate.To(character);

            characterModel.House = await this.potterAPIClient.GetHouseAsync(character.HouseId);

            return(Ok(characterModel));
        }
Example #3
0
        public void ModelToEntity()
        {
            var characterModel = new CharacterModel
            {
                House    = "3824jgdjdg",
                Name     = "HP",
                Patronus = "Cachorro",
                Role     = "Estudante",
                School   = "Hogwarts"
            };

            var character = CharacterTranslate.To(characterModel);

            Assert.Equal("3824jgdjdg", character.HouseId);
            Assert.Equal("HP", character.Name);
            Assert.Equal("Cachorro", character.Patronus);
            Assert.Equal("Estudante", character.Role);
            Assert.Equal("Hogwarts", character.School);
        }
Example #4
0
        public async Task <IActionResult> PutAsync(Guid id, [FromBody] CharacterModel characterModel)
        {
            try
            {
                var houseModel = await this.potterAPIClient.GetHouseAsync(characterModel.House);

                if (houseModel?.Id == null)
                {
                    return(NotFound("House not exists."));
                }

                var character = CharacterTranslate.To(characterModel, id);
                await this.characterService.UpdateAsync(id, character);

                return(Ok());
            }
            catch (ArgumentNullException)
            {
                return(NotFound());
            }
        }
Example #5
0
        public void EntityToModel()
        {
            var id        = Guid.NewGuid();
            var character = new Character
            {
                Id       = id,
                HouseId  = "3824jgdjdg",
                Name     = "HP",
                Patronus = "Cachorro",
                Role     = "Estudante",
                School   = "Hogwarts"
            };

            var characterModel = CharacterTranslate.To(character);

            Assert.Equal(characterModel.Id, id);
            Assert.Null(characterModel.House);
            Assert.Equal("HP", characterModel.Name);
            Assert.Equal("Cachorro", characterModel.Patronus);
            Assert.Equal("Estudante", characterModel.Role);
            Assert.Equal("Hogwarts", characterModel.School);
        }
Example #6
0
        public async Task <IActionResult> GetAsync(string house = null)
        {
            var characters = await this.characterService.AllAsync(house);

            if (characters?.Count == 0)
            {
                return(NotFound());
            }

            var houses = await this.potterAPIClient.GetHousesAsync();

            var characterModels = new List <CharacterGetModel>();

            foreach (var item in characters)
            {
                var characterModel = CharacterTranslate.To(item);
                characterModel.House = houses?.FirstOrDefault(h => h.Id == item.HouseId);
                characterModels.Add(characterModel);
            }

            return(Ok(characterModels));
        }
Example #7
0
        public void EntityToModelWhenEntityIsNull()
        {
            var characterModel = CharacterTranslate.To((Character)null);

            Assert.Null(characterModel);
        }
Example #8
0
        public void ModelToEntityWhenModelIsNull()
        {
            var character = CharacterTranslate.To((CharacterModel)null);

            Assert.Null(character);
        }