public async Task CreateAsync_creates_a_hero_with_basic_properties()
        {
            var hero = new SuperheroCreateDTO
            {
                Name            = "Diana",
                AlterEgo        = "Wonder Woman",
                Occupation      = "Amazon Princess",
                Gender          = Female,
                FirstAppearance = 1941,
                PortraitUrl     = "https://ondfisk.blob.core.windows.net/superheroes/wonder-woman-portrait.jpg",
                BackgroundUrl   = "https://ondfisk.blob.core.windows.net/superheroes/wonder-woman-background.jpg"
            };

            var(_, id) = await _repository.CreateAsync(hero);

            var created = await _context.Superheroes.FindAsync(id);

            Assert.Equal(4, created.Id);
            Assert.Equal("Diana", created.Name);
            Assert.Equal("Wonder Woman", created.AlterEgo);
            Assert.Equal("Amazon Princess", created.Occupation);
            Assert.Equal(Entities.Gender.Female, created.Gender);
            Assert.Equal(1941, created.FirstAppearance);
            Assert.Equal("https://ondfisk.blob.core.windows.net/superheroes/wonder-woman-portrait.jpg", created.PortraitUrl);
            Assert.Equal("https://ondfisk.blob.core.windows.net/superheroes/wonder-woman-background.jpg", created.BackgroundUrl);
        }
        public async Task CreateAsync_returns_Created()
        {
            var hero = new SuperheroCreateDTO
            {
                Name     = "Diana",
                AlterEgo = "Wonder Woman"
            };

            var(response, _) = await _repository.CreateAsync(hero);

            Assert.Equal(Created, response);
        }
        private async Task ExecuteSaveCommand()
        {
            IsBusy = true;

            var powers = Powers?.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()) ?? new string[0];

            var superhero = new SuperheroCreateDTO
            {
                Name            = Name,
                AlterEgo        = AlterEgo,
                Occupation      = Occupation,
                CityName        = CityName,
                PortraitUrl     = PortraitUrl,
                BackgroundUrl   = BackgroundUrl,
                FirstAppearance = FirstAppearance,
                Gender          = Gender,
                Powers          = new HashSet <string>(powers)
            };

            try
            {
                var(status, uri) = await _client.PostAsync("superheroes", superhero);

                if (status != HttpStatusCode.Created)
                {
                    await _dialog.DisplayAlertAsync("Error", $"Error from api: {status}", "OK");
                }
                else
                {
                    var id = int.Parse(uri.AbsoluteUri.Substring(uri.AbsoluteUri.LastIndexOf("/") + 1));

                    var superheroListDTO = new SuperheroListDTO
                    {
                        Id          = id,
                        Name        = Name,
                        AlterEgo    = AlterEgo,
                        PortraitUrl = PortraitUrl
                    };

                    _messaging.Send(this, AddSuperhero, superheroListDTO);

                    await _navigation.CancelAsync();
                }
            }
            catch (Exception e)
            {
                await _dialog.DisplayAlertAsync(e);
            }

            IsBusy = false;
        }
Esempio n. 4
0
        public async Task Given_superhero_with_new_city_When_Create_it_Creates_superhero_and_returns_id()
        {
            var superhero = new SuperheroCreateDTO
            {
                Name            = "Diana",
                AlterEgo        = "Wonder Woman",
                CityName        = "Themyscira",
                Occupation      = "Amazon Princess",
                Gender          = Gender.Female,
                FirstAppearance = 1941,
                Powers          = new HashSet <string>
                {
                    "super strength",
                    "invulnerability",
                    "flight",
                    "combat skill",
                    "superhuman agility",
                    "healing factor",
                    "magic weaponry"
                }
            };

            var id = await _repository.Create(superhero);

            var entity = await _context.Superheroes
                         .Include(h => h.City)
                         .Include(h => h.Powers)
                         .ThenInclude(h => h.Power)
                         .FirstOrDefaultAsync(h => h.Id == id);

            Assert.Equal("Diana", entity.Name);
            Assert.Equal("Wonder Woman", entity.AlterEgo);
            Assert.Equal("Amazon Princess", entity.Occupation);
            Assert.Equal(Female, entity.Gender);
            Assert.Equal(1941, entity.FirstAppearance);
            Assert.Equal(3, entity.CityId);
            Assert.Equal("Themyscira", entity.City.Name);
            Assert.Equal(new[]
            {
                "super strength",
                "invulnerability",
                "flight",
                "combat skill",
                "superhuman agility",
                "healing factor",
                "magic weaponry"
            },
                         entity.Powers.Select(p => p.Power.Name).ToHashSet()
                         );
        }
        public async Task CreateAsync_creates_a_hero_with_existing_city()
        {
            var hero = new SuperheroCreateDTO
            {
                Name     = "Kate Kane",
                AlterEgo = "Batwoman",
                CityName = "Gotham City"
            };

            var(_, id) = await _repository.CreateAsync(hero);

            var created = await _context.Superheroes.FindAsync(id);

            Assert.Equal(2, created.CityId);
        }
        public async Task CreateAsync_creates_a_hero_with_new_city()
        {
            var hero = new SuperheroCreateDTO
            {
                Name     = "Diana",
                AlterEgo = "Wonder Woman",
                CityName = "Themyscira"
            };

            var(_, id) = await _repository.CreateAsync(hero);

            var created = await _context.Superheroes.Include(c => c.City).FirstOrDefaultAsync(c => c.Id == id);

            Assert.Equal(3, created.CityId);
            Assert.Equal(3, created.City.Id);
            Assert.Equal("Themyscira", created.City.Name);
        }
        public async Task Post_returns_CreatedAtAction_with_id()
        {
            var superhero = new SuperheroCreateDTO();

            var repository = new Mock <ISuperheroRepository>();

            repository.Setup(s => s.CreateAsync(superhero)).ReturnsAsync((Created, 42));

            var logger = new Mock <ILogger <SuperheroesController> >();

            var controller = new SuperheroesController(repository.Object, logger.Object);

            var actual = await controller.Post(superhero);

            Assert.Equal("Get", actual.ActionName);
            Assert.Equal(42, actual.RouteValues["id"]);
        }
Esempio n. 8
0
        public async Task CreateAsync_creates_a_hero_with_powers()
        {
            var hero = new SuperheroCreateDTO
            {
                Name     = "Diana",
                AlterEgo = "Wonder Woman",
                Powers   = new[] { "super strength", "invulnerability", "flight", "combat skill", "combat strategy", "superhuman agility", "healing factor", "magic weaponry" }
            };

            var(_, id) = await _repository.CreateAsync(hero);

            var powers = from p in _context.SuperheroPowers
                         where p.SuperheroId == id
                         select p.Power.Name;

            Assert.True(hero.Powers.ToHashSet().SetEquals(powers));
        }
Esempio n. 9
0
        public async Task Given_superhero_with_existing_city_When_Create_it_Creates_superhero_and_returns_id()
        {
            var superhero = new SuperheroCreateDTO
            {
                Name            = "Selina Kyle",
                AlterEgo        = "Catwoman",
                CityName        = "Gotham City",
                Occupation      = "Thief",
                Gender          = Gender.Female,
                FirstAppearance = 1940,
                Powers          = new HashSet <string>
                {
                    "exceptional martial artist",
                    "gymnastic ability",
                    "combat skill"
                }
            };

            var id = await _repository.Create(superhero);

            Assert.Equal(3, id);

            var entity = _context.Superheroes
                         .Include(h => h.City)
                         .Include(h => h.Powers)
                         .ThenInclude(h => h.Power)
                         .FirstOrDefault(h => h.Id == id);

            Assert.Equal("Selina Kyle", entity.Name);
            Assert.Equal("Catwoman", entity.AlterEgo);
            Assert.Equal("Thief", entity.Occupation);
            Assert.Equal(Female, entity.Gender);
            Assert.Equal(1940, entity.FirstAppearance);
            Assert.Equal(2, entity.CityId);
            Assert.Equal("Gotham City", entity.City.Name);
            Assert.Equal(new[]
            {
                "exceptional martial artist",
                "gymnastic ability",
                "combat skill"
            },
                         entity.Powers.Select(p => p.Power.Name).ToHashSet()
                         );
        }
Esempio n. 10
0
        public void Given_hero_create_creates()
        {
            var hero = new SuperheroCreateDTO
            {
                Name     = "Selina Kyle",
                AlterEgo = "Catwoman",
                CityName = "Gotham City",
                Gender   = Gender.Female
            };

            var id = _repository.Create(hero);

            Assert.Equal(3, id);

            var entity = _context.Superheroes.Include(c => c.City).FirstOrDefault(c => c.Id == id);

            Assert.Equal("Selina Kyle", entity.Name);
            Assert.Equal("Gotham City", entity.City.Name);
        }
Esempio n. 11
0
        public async Task <int> Create(SuperheroCreateDTO superhero)
        {
            var entity = new Superhero
            {
                Name            = superhero.Name,
                AlterEgo        = superhero.AlterEgo,
                City            = await GetCity(superhero.CityName),
                Occupation      = superhero.Occupation,
                Gender          = superhero.Gender.ToGender(),
                FirstAppearance = superhero.FirstAppearance,
                PortraitUrl     = superhero.PortraitUrl,
                BackgroundUrl   = superhero.BackgroundUrl,
                Powers          = await GetPowers(superhero.Powers).ToListAsync()
            };

            _context.Superheroes.Add(entity);
            await _context.SaveChangesAsync();

            return(entity.Id);
        }
Esempio n. 12
0
        public async Task <(Response response, int superheroId)> CreateAsync(SuperheroCreateDTO superhero)
        {
            var entity = new Superhero
            {
                Name            = superhero.Name,
                AlterEgo        = superhero.AlterEgo,
                City            = await MapCityAsync(superhero.CityName),
                FirstAppearance = superhero.FirstAppearance,
                Gender          = superhero.Gender.Convert(),
                Occupation      = superhero.Occupation,
                PortraitUrl     = superhero.PortraitUrl,
                BackgroundUrl   = superhero.BackgroundUrl,
                Powers          = await MapPowersAsync(0, superhero.Powers)
            };

            _context.Superheroes.Add(entity);
            await _context.SaveChangesAsync();

            return(Created, entity.Id);
        }
        public async Task CreateAsync_creates_a_hero_with_basic_properties()
        {
            var hero = new SuperheroCreateDTO
            {
                Name            = "Diana",
                AlterEgo        = "Wonder Woman",
                Occupation      = "Amazon Princess",
                Gender          = Female,
                FirstAppearance = 1941
            };

            var(_, id) = await _repository.CreateAsync(hero);

            var created = await _context.Superheroes.FindAsync(id);

            Assert.Equal(4, created.Id);
            Assert.Equal("Diana", created.Name);
            Assert.Equal("Wonder Woman", created.AlterEgo);
            Assert.Equal("Amazon Princess", created.Occupation);
            Assert.Equal(Female, created.Gender);
            Assert.Equal(1941, created.FirstAppearance);
        }
        private async Task ExecuteSaveCommand()
        {
            IsBusy = true;

            var powers = Powers?.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()) ?? new string[0];

            var superhero = new SuperheroCreateDTO
            {
                Name            = Name,
                AlterEgo        = AlterEgo,
                Occupation      = Occupation,
                CityName        = CityName,
                PortraitUrl     = PortraitUrl,
                BackgroundUrl   = BackgroundUrl,
                FirstAppearance = FirstAppearance,
                Gender          = Gender,
                Powers          = new HashSet <string>(powers)
            };

            var uri = await _client.PostAsync("superheroes", superhero);

            var id = int.Parse(uri.AbsoluteUri.Substring(uri.AbsoluteUri.LastIndexOf("/") + 1));

            var superheroListDTO = new SuperheroListDTO
            {
                Id          = id,
                Name        = Name,
                AlterEgo    = AlterEgo,
                PortraitUrl = PortraitUrl
            };

            _messaging.Send(this, AddSuperhero, superheroListDTO);

            await _navigation.CancelAsync();

            IsBusy = false;
        }
Esempio n. 15
0
        public void Create_creates_a_hero()
        {
            var hero = new SuperheroCreateDTO
            {
                Name            = "Kara Zor-El",
                AlterEgo        = "Supergirl",
                Occupation      = "Actress",
                Gender          = Female,
                FirstAppearance = 1959,
                CityName        = "New York City"
            };

            var(_, id) = _repository.Create(hero);

            var created = _context.Superheroes.Find(id);

            Assert.Equal(9, created.Id);
            Assert.Equal("Kara Zor-El", created.Name);
            Assert.Equal("Supergirl", created.AlterEgo);
            Assert.Equal("Actress", created.Occupation);
            Assert.Equal(Female, created.Gender);
            Assert.Equal(1959, created.FirstAppearance);
            Assert.Equal(5, created.CityId);
        }
Esempio n. 16
0
        public async Task <CreatedAtActionResult> Post([FromBody] SuperheroCreateDTO superhero)
        {
            var(_, id) = await _repository.CreateAsync(superhero);

            return(CreatedAtAction("Get", new { id }, null));
        }
Esempio n. 17
0
        public async Task <IActionResult> Post([FromBody] SuperheroCreateDTO superhero)
        {
            var id = await _repository.Create(superhero);

            return(CreatedAtAction(nameof(Get), new { id }, default));
        }