Example #1
0
        public async Task <IActionResult> PutRegiao(short id, Regiao regiao)
        {
            if (id != regiao.CodRegiao)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Example #2
0
        public async Task <ActionResult <History> > PostEnter([FromForm, Required] string rfid)
        {
            var qUser = _context.User.Where(f => f.Rfid == rfid);

            if (!await qUser.AnyAsync())
            {
                return(BadRequest("Rfid tanımsız!"));
            }


            var qUnrevoked = _context.History.Where(f => f.User.Rfid == rfid && f.Revoked == null);

            if (await qUnrevoked.AnyAsync())
            {
                return(BadRequest("Önce çıkış yapılması lazım!"));
            }

            var value = new History {
                Created = DateTime.Now, UserId = (await qUser.FirstAsync()).Id
            };

            _context.History.Add(value);
            var res = await _context.SaveChangesAsync();

            if (res > 0)
            {
                return(Ok(value));
            }
            return(BadRequest("Kayıt gerçekleşemedi!"));
        }
Example #3
0
        public async Task <IActionResult> PutCorreioEletronico(short ano, long id, CorreioEletronico correioEletronico)
        {
            if (id != correioEletronico.CodEntidade || ano != correioEletronico.Ano)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Example #4
0
        public async Task <IActionResult> PutMunicipio(long id, Municipio municipio)
        {
            if (id != municipio.CodMunicipio)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Example #5
0
        public async Task <IActionResult> PutUser(long id, User user)
        {
            if (id != user.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <IActionResult> PutTelefone(long num, short ano, long id, Telefone telefone)
        {
            if (id != telefone.CodEntidade || ano != telefone.Ano || num != telefone.Numero)
            {
                return(BadRequest());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TelefoneExists(num, ano, id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #7
0
        public async Task <IActionResult> PutEscola(long id, Escola escola)
        {
            if (id != escola.CodEntidade)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Example #8
0
        public async Task <IActionResult> addProduct([FromBody] Producto productos)
        {
            await db.Productos.AddAsync(productos);

            await db.SaveChangesAsync();

            return(Ok());
        }
Example #9
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Test test)
        {
            if (ModelState.IsValid)
            {
                _context.Add(test);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(test));
        }
Example #10
0
        public async Task <IActionResult> Create([Bind("Id,Filme,Genero")] TbFilme tbFilme)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tbFilme);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(tbFilme));
        }
Example #11
0
        public async Task <IActionResult> Create([Bind("Id,Nome,Endereco,Cpf")] TbCliente tbCliente)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tbCliente);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(tbCliente));
        }
Example #12
0
        public async Task <IActionResult> Create([Bind("TbClienteId,TbFilmeId,DataLocacao,DataDevolucao,Id")] TbLocacao tbLocacao)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tbLocacao);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TbClienteId"] = new SelectList(_context.TbCliente, "Id", "Cpf", tbLocacao.TbClienteId);
            ViewData["TbFilmeId"]   = new SelectList(_context.TbFilme, "Id", "Filme", tbLocacao.TbFilmeId);
            return(View(tbLocacao));
        }
Example #13
0
        public async Task <User> Register(User user, string password)
        {
            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);
            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;
            await _dbContext.AddAsync(user);

            await _dbContext.SaveChangesAsync();

            return(user);
        }
Example #14
0
        public async Task CreatesNpgSqlComposite()
        {
            var parent = new Efcoretest
            {
                Name         = $"{nameof(BulkAddTests)}_Composite",
                Createddate  = DateTime.UtcNow,
                Modifieddate = DateTime.UtcNow
            };

            using (var db = new postgresContext())
            {
                await db.AddAsync(parent);

                await db.SaveChangesAsync();
            }

            var toCreate = new List <Efcoretestchild>();

            for (var i = 0; i < 10; i++)
            {
                toCreate.Add(new Efcoretestchild
                {
                    Efcoretestid = parent.Id,
                    Name         = i == 0 ? null : $"{nameof(BulkAddTests)}_{i}",
                    Createddate  = DateTime.UtcNow,
                    Modifieddate = DateTime.UtcNow
                });
            }

            using (var db = new postgresContext())
            {
                await db.Efcoretestchild.BulkAddAsync(toCreate);

                foreach (var e in toCreate)
                {
                    var added = await db.Efcoretestchild.FindAsync(e.Id, e.Efcoretestid);

                    Assert.NotNull(added);
                    Assert.Equal(e.Name, added.Name);
                }
            }
        }
Example #15
0
        public async Task NpgSqlCommit()
        {
            using (var db = new postgresContext())
                using (var scope = db.Database.CurrentTransaction ?? db.Database.BeginTransaction())
                {
                    var toAdd = new Efcoretest
                    {
                        Name         = $"{nameof(BulkTransactionTests)}_Added",
                        Createddate  = DateTime.Now,
                        Modifieddate = DateTime.Now
                    };

                    await db.Efcoretest.AddAsync(toAdd);

                    await db.SaveChangesAsync(false);

                    var ids = await db.Efcoretest
                              .Where(x => x.Name.Contains(nameof(BulkTransactionTests)))
                              .Select(x => x.Id)
                              .ToListAsync();

                    await db.Efcoretest
                    .Where(x => x.Name.Contains(nameof(BulkTransactionTests)))
                    .BulkRemoveAsync(ids);

                    scope.Commit();
                }

            using (var db = new postgresContext())
            {
                var count = await db.Efcoretest
                            .Where(x => x.Name.Contains(nameof(BulkTransactionTests)))
                            .CountAsync();

                Assert.Equal(0, count);
            }
        }
Example #16
0
        public async Task MergesNpgSql(BulkMergeNotMatchedBehavior notMatchedBehavior)
        {
            List <Efcoretest> entities;

            var dontDelete = new Efcoretest
            {
                Name         = "dont delete",
                Createddate  = DateTime.UtcNow,
                Modifieddate = DateTime.UtcNow
            };

            using (var db = new postgresContext())
            {
                entities = await db.Efcoretest
                           .Where(e => e.Name.Contains(nameof(BulkMergeTests)))
                           .ToListAsync();

                var added = await db.AddAsync(dontDelete);

                await db.SaveChangesAsync();

                dontDelete.Id = added.Entity.Id;
            }

            const int numUpdate = 5;

            var toMerge = entities
                          .OrderBy(e => e.Id)
                          .Take(numUpdate)
                          .Select(x =>
            {
                x.Modifieddate = DateTime.UtcNow.AddDays(x.Id);
                return(x);
            })
                          .ToList();

            var numNotMatched = entities.Count - toMerge.Count;

            var numAdd = 2;

            for (var i = 0; i < numAdd; i++)
            {
                toMerge.Add(new Efcoretest
                {
                    Name         = $"{nameof(BulkMergeTests)}_insert_{i}",
                    Modifieddate = DateTime.Now,
                    Createddate  = DateTime.Now
                });
            }

            var expectedRecordsAffected = notMatchedBehavior == BulkMergeNotMatchedBehavior.DoNothing
                ? numUpdate + numAdd
                : numUpdate + numNotMatched + numAdd;

            using (var db = new postgresContext())
            {
                var numRowsAffected = await db.Efcoretest
                                      .Where(e => e.Name.Contains(nameof(BulkMergeTests)))
                                      .BulkMergeAsync(
                    toMerge,
                    notMatchedBehavior: notMatchedBehavior,
                    whenNotMatched: () => new Efcoretest {
                    Name = $"{nameof(BulkMergeTests)}__Archived__"
                });

                Assert.Equal(expectedRecordsAffected, numRowsAffected);

                var dbCount = await db.Efcoretest
                              .Where(e => e.Name.Contains(nameof(BulkMergeTests)))
                              .CountAsync();

                if (notMatchedBehavior == BulkMergeNotMatchedBehavior.Delete)
                {
                    Assert.Equal(toMerge.Count, dbCount);
                }
                else
                {
                    Assert.NotEqual(toMerge.Count, dbCount);
                }

                foreach (var m in toMerge)
                {
                    var dbEntity = await db.Efcoretest.FindAsync(m.Id);

                    Assert.NotNull(dbEntity);
                    Assert.Equal(m.Name, dbEntity.Name);
                    Assert.Equal(m.Modifieddate.ToString("d"), dbEntity.Modifieddate.ToString("d"));
                    Assert.Equal(m.Createddate.ToString("d"), dbEntity.Createddate.ToString("d"));
                }

                var dontDeleteEntity = await db.Efcoretest.FindAsync(dontDelete.Id);

                Assert.NotNull(dontDeleteEntity);
            }
        }