Example #1
0
        public async Task <IActionResult> PutPais([FromRoute] Guid id, [FromBody] Pais pais)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != pais.PaisId)
            {
                return(BadRequest());
            }

            _context.Entry(pais).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!await PaisExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #2
0
        private async Task <Pais> AdicionarPais(DbContextOptions <CompeticoesDbContext> options)
        {
            var pais = new Pais()
            {
                PaisId = Guid.NewGuid(),
                Nome   = "Brasil",
                Sigla  = "BR"
            };

            using (CompeticoesDbContext context = new CompeticoesDbContext(options))
            {
                await context.Paises.AddAsync(pais);

                await context.SaveChangesAsync();
            }

            return(pais);
        }
        private async Task <Estado> AdicionarEstado(
            DbContextOptions <CompeticoesDbContext> options)
        {
            var estado = new Estado()
            {
                EstadoId = Guid.NewGuid(),
                Nome     = "Rio Grande do Sul",
                Sigla    = "RS",
            };

            using (CompeticoesDbContext context = new CompeticoesDbContext(options))
            {
                await context.Estados.AddAsync(estado);

                await context.SaveChangesAsync();
            }

            return(estado);
        }
        public async Task PutEstado_ConcurrencyException_NotExists_NotFound()
        {
            var options = Options;

            var estado = await AdicionarEstado(options);

            using (var context = new CompeticoesDbContext(options))
            {
                context.Estados.Remove(estado);
                await context.SaveChangesAsync();
            }

            using (var context = new CompeticoesDbContext(options))
            {
                estado.Nome = "Santa Catarina";
                var controller = new EstadosController(context);

                var result = await controller.PutEstado(estado.EstadoId, estado);

                Assert.IsType <NotFoundResult>(result);
            }
        }
Example #5
0
        public async Task PutPais_ConcurrencyExceptions_NotExists_NotFound()
        {
            var options = Options;

            var pais = await AdicionarPais(options);

            using (CompeticoesDbContext context = new CompeticoesDbContext(options))
            {
                context.Paises.Remove(pais);
                await context.SaveChangesAsync();
            }

            using (CompeticoesDbContext context = new CompeticoesDbContext(options))
            {
                pais.Nome = "Brazil";
                var controller = new PaisesController(context);

                var result = await controller.PutPais(pais.PaisId, pais);

                Assert.IsType <NotFoundResult>(result);
            }
        }