public async Task UpdateAsync_updates_a_hero_with_basic_properties()
        {
            var hero = new SuperheroUpdateDTO
            {
                Id              = 1,
                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"
            };

            await _repository.UpdateAsync(hero);

            var updated = _context.Superheroes.Find(1);

            Assert.Equal(1, updated.Id);
            Assert.Equal("Diana", updated.Name);
            Assert.Equal("Wonder Woman", updated.AlterEgo);
            Assert.Equal("Amazon Princess", updated.Occupation);
            Assert.Equal(Entities.Gender.Female, updated.Gender);
            Assert.Equal(1941, updated.FirstAppearance);
            Assert.Equal("https://ondfisk.blob.core.windows.net/superheroes/wonder-woman-portrait.jpg", updated.PortraitUrl);
            Assert.Equal("https://ondfisk.blob.core.windows.net/superheroes/wonder-woman-background.jpg", updated.BackgroundUrl);
        }
        public void Update_given_existing_entity_uses_existing_city()
        {
            var builder = new DbContextOptionsBuilder <SuperheroContext>().UseInMemoryDatabase(nameof(Update_given_no_entity_returns_NotFound));
            var context = new SuperheroContext(builder.Options);
            var entity  = new Superhero
            {
                Name     = "Bruce Wayne",
                AlterEgo = "Batman"
            };

            context.Superheroes.Add(entity);
            var entityCity = new City {
                Name = "Metropolis"
            };

            context.Cities.Add(entityCity);
            context.SaveChanges();
            var repository = new SuperheroRepository(context);

            var superhero = new SuperheroUpdateDTO
            {
                Id       = entity.Id,
                Name     = "Clark Kent",
                AlterEgo = "Superman",
                CityName = "Metropolis"
            };

            var response = repository.Update(superhero);

            var updated = context.Superheroes.Find(entity.Id);

            Assert.Equal("Clark Kent", updated.Name);
            Assert.Equal("Superman", updated.AlterEgo);
            Assert.Equal(entityCity.Id, updated.CityId);
        }
Esempio n. 3
0
        public async Task Given_non_existing_id_When_Update_it_returns_not_found()
        {
            var superhero = new SuperheroUpdateDTO {
                Id = 42
            };

            var result = await _repository.Update(superhero);

            Assert.Equal(NotFound, result);
        }
        public async Task <IActionResult> Put(int id, [FromBody] SuperheroUpdateDTO superhero)
        {
            var response = await _repository.UpdateAsync(superhero);

            return(response switch
            {
                Updated => NoContent(),
                Lecture10.Models.Response.NotFound => NotFound(),
                _ => throw new NotSupportedException(), // <- can't happen
            });
        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 SuperheroUpdateDTO
            {
                Id              = Id,
                Name            = Name,
                AlterEgo        = AlterEgo,
                Occupation      = Occupation,
                CityName        = CityName,
                PortraitUrl     = PortraitUrl,
                BackgroundUrl   = BackgroundUrl,
                FirstAppearance = FirstAppearance,
                Gender          = Gender,
                Powers          = new HashSet <string>(powers)
            };

            try
            {
                var status = await _client.PutAsync($"superheroes/{Id}", superhero);

                if (status != HttpStatusCode.NoContent)
                {
                    await _dialog.DisplayAlertAsync("Error", $"Error from api: {status}", "OK");
                }
                else
                {
                    var superheroDetailsDTO = new SuperheroDetailsDTO
                    {
                        Id              = Id,
                        Name            = Name,
                        AlterEgo        = AlterEgo,
                        Occupation      = Occupation,
                        CityName        = CityName,
                        PortraitUrl     = PortraitUrl,
                        BackgroundUrl   = BackgroundUrl,
                        FirstAppearance = FirstAppearance,
                        Gender          = Gender,
                        Powers          = superhero.Powers
                    };

                    _messaging.Send(this, UpdateSuperhero, superheroDetailsDTO);
                    await _navigation.CancelAsync();
                }
            }
            catch (Exception e)
            {
                await _dialog.DisplayAlertAsync(e);
            }

            IsBusy = false;
        }
        public async Task UpdateAsync_given_non_existing_returns_NotFound()
        {
            var hero = new SuperheroUpdateDTO
            {
                Id = 42
            };

            var response = await _repository.UpdateAsync(hero);

            Assert.Equal(NotFound, response);
        }
Esempio n. 7
0
        public async Task Update_turns_Superman_into_Wonder_Woman()
        {
            var superhero = new SuperheroUpdateDTO
            {
                Id              = 1,
                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 result = await _repository.Update(superhero);

            Assert.Equal(Updated, result);

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

            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()
                         );
        }
Esempio n. 8
0
        public async Task <IActionResult> Put(int id, [FromBody] SuperheroUpdateDTO superhero)
        {
            if (id != superhero.Id)
            {
                ModelState.AddModelError("id", "id in URL must match id in body");

                return(BadRequest(ModelState));
            }

            var response = await _repository.Update(superhero);

            return(new StatusCodeResult((int)response));
        }
        public void Update_given_no_entity_returns_NotFound()
        {
            var builder    = new DbContextOptionsBuilder <SuperheroContext>().UseInMemoryDatabase(nameof(Update_given_no_entity_returns_NotFound));
            var context    = new SuperheroContext(builder.Options);
            var repository = new SuperheroRepository(context);

            var superhero = new SuperheroUpdateDTO {
                Id = 42
            };

            var response = repository.Update(superhero);

            Assert.Equal(NotFound, response);
        }
Esempio n. 10
0
        public async Task <IActionResult> Put(int id, [FromBody] SuperheroUpdateDTO superhero)
        {
            var response = await _repository.UpdateAsync(superhero);

            switch (response)
            {
            case Updated:
                return(NoContent());

            case BDSA2019.Lecture09.Models.Response.NotFound:
                return(NotFound());

            default:
                throw new NotSupportedException();     // <- can't happen
            }
        }
        public async Task UpdateAsync_updates_a_hero_with_existing_city()
        {
            var hero = new SuperheroUpdateDTO
            {
                Id       = 1,
                Name     = "Superman",
                AlterEgo = "Superman",
                CityName = "Gotham City"
            };

            await _repository.UpdateAsync(hero);

            var updated = _context.Superheroes.Find(1);

            Assert.Equal(2, updated.CityId);
        }
        public async Task Put_given_repository_returns_response_returns_returnType(Response response, Type returnType)
        {
            var superhero = new SuperheroUpdateDTO {
                Id = 12
            };
            var repository = new Mock <ISuperheroRepository>();

            repository.Setup(s => s.UpdateAsync(superhero)).ReturnsAsync(response);

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

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

            var actual = await controller.Put(12, superhero);

            Assert.IsType(returnType, actual);
        }
Esempio n. 13
0
        public async Task Update_turns_Batman_into_Catwoman()
        {
            var superhero = new SuperheroUpdateDTO
            {
                Id              = 2,
                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 result = await _repository.Update(superhero);

            Assert.Equal(Updated, result);

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

            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()
                         );
        }
        public async Task UpdateAsync_updates_a_hero_with_new_city()
        {
            var hero = new SuperheroUpdateDTO
            {
                Id       = 1,
                Name     = "Superman",
                AlterEgo = "Superman",
                CityName = "New York City"
            };

            await _repository.UpdateAsync(hero);

            var updated = await _context.Superheroes.Include(c => c.City).FirstOrDefaultAsync(c => c.Id == 1);

            Assert.Equal(3, updated.CityId);
            Assert.Equal(3, updated.City.Id);
            Assert.Equal("New York City", updated.City.Name);
        }
        public async Task UpdateAsync_updates_a_hero_with_powers()
        {
            var hero = new SuperheroUpdateDTO
            {
                Id       = 1,
                Name     = "Diana",
                AlterEgo = "Wonder Woman",
                Powers   = new HashSet <string> {
                    "super strength", "invulnerability", "flight", "combat skill", "combat strategy", "superhuman agility", "healing factor", "magic weaponry"
                }
            };

            await _repository.UpdateAsync(hero);

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

            Assert.True(hero.Powers.SetEquals(powers));
        }
        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 SuperheroUpdateDTO
            {
                Id              = Id,
                Name            = Name,
                AlterEgo        = AlterEgo,
                Occupation      = Occupation,
                CityName        = CityName,
                PortraitUrl     = PortraitUrl,
                BackgroundUrl   = BackgroundUrl,
                FirstAppearance = FirstAppearance,
                Gender          = Gender,
                Powers          = new HashSet <string>(powers)
            };

            await _client.PutAsync($"superheroes/{Id}", superhero);

            var superheroDetailsDTO = new SuperheroDetailsDTO
            {
                Id              = Id,
                Name            = Name,
                AlterEgo        = AlterEgo,
                Occupation      = Occupation,
                CityName        = CityName,
                PortraitUrl     = PortraitUrl,
                BackgroundUrl   = BackgroundUrl,
                FirstAppearance = FirstAppearance,
                Gender          = Gender,
                Powers          = superhero.Powers
            };

            _messaging.Send(this, UpdateSuperhero, superheroDetailsDTO);
            await _navigation.CancelAsync();

            IsBusy = false;
        }
        public async Task UpdateAsync_updates_a_hero_with_basic_properties()
        {
            var hero = new SuperheroUpdateDTO
            {
                Id              = 1,
                Name            = "Diana",
                AlterEgo        = "Wonder Woman",
                Occupation      = "Amazon Princess",
                Gender          = Female,
                FirstAppearance = 1941
            };

            await _repository.UpdateAsync(hero);

            var updated = _context.Superheroes.Find(1);

            Assert.Equal(1, updated.Id);
            Assert.Equal("Diana", updated.Name);
            Assert.Equal("Wonder Woman", updated.AlterEgo);
            Assert.Equal("Amazon Princess", updated.Occupation);
            Assert.Equal(Female, updated.Gender);
            Assert.Equal(1941, updated.FirstAppearance);
        }
Esempio n. 18
0
        public async Task <HttpStatusCode> Update(SuperheroUpdateDTO superhero)
        {
            var entity = await _context.Superheroes.FindAsync(superhero.Id);

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

            entity.Name     = superhero.Name;
            entity.AlterEgo = superhero.AlterEgo;
            entity.City     = await GetCity(superhero.CityName);

            entity.Occupation      = superhero.Occupation;
            entity.Gender          = superhero.Gender.ToGender();
            entity.FirstAppearance = superhero.FirstAppearance;
            entity.PortraitUrl     = superhero.PortraitUrl;
            entity.BackgroundUrl   = superhero.BackgroundUrl;
            entity.Powers          = await GetPowers(superhero.Powers).ToListAsync();

            await _context.SaveChangesAsync();

            return(NoContent);
        }
Esempio n. 19
0
        public async Task <Response> UpdateAsync(SuperheroUpdateDTO superhero)
        {
            var entity = await _context.Superheroes.Include(c => c.Powers).FirstOrDefaultAsync(c => c.Id == superhero.Id);

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

            entity.Name     = superhero.Name;
            entity.AlterEgo = superhero.AlterEgo;
            entity.City     = await MapCityAsync(superhero.CityName);

            entity.Gender          = superhero.Gender.Convert();
            entity.FirstAppearance = superhero.FirstAppearance;
            entity.Occupation      = superhero.Occupation;
            entity.PortraitUrl     = superhero.PortraitUrl;
            entity.BackgroundUrl   = superhero.BackgroundUrl;
            entity.Powers          = await MapPowersAsync(superhero.Id, superhero.Powers);

            await _context.SaveChangesAsync();

            return(Updated);
        }
Esempio n. 20
0
        public async Task <IActionResult> Put(int id, [FromBody] SuperheroUpdateDTO superhero)
        {
            var response = await _repository.Update(superhero);

            return(new StatusCodeResult((int)response));
        }