public async Task <IActionResult> CrieUsuarioPadrao()
        {
            if (!VerificaSeUsuarioJaExiste("admin"))
            {
                Usuario usuario;
                var     senha = "123456";
                using (var ctx = new EventosDataContext())
                {
                    usuario = new Usuario
                    {
                        DataCadastro = DateTime.Now.Date,
                        Email        = "*****@*****.**",
                        Nome         = "admin",
                        Senha        = senha,
                        UserName     = "******"
                    };

                    usuario.Senha = EncryptPassword.Encode(usuario.Senha);
                    await ctx.Usuario.AddAsync(usuario);

                    await ctx.SaveChangesAsync();
                }

                return(new JsonResult($"Usuario: {usuario.UserName} Senha: {senha}."));
            }
            else
            {
                return(BadRequest("Já existe Usuário com este nome!"));
            }
        }
 public IEnumerable <Evento> ObterEventosPorUsuario(int id)
 {
     using (var ctx = new EventosDataContext())
     {
         return(ctx.Evento.Where(c => c.Id == id).ToList());
     }
 }
 public async Task <Usuario> BuscarPorId(int id)
 {
     using (var ctx = new EventosDataContext())
     {
         return(await ctx.Usuario.FindAsync(id));
     }
 }
 public IEnumerable <Usuario> ObterTodos()
 {
     using (var ctx = new EventosDataContext())
     {
         return(ctx.Usuario.ToList());
     }
 }
 public async Task <Evento> BuscarPorId(int id)
 {
     using (var ctx = new EventosDataContext())
     {
         //_logger.LogInformation("Solicitou um Evento");
         return(await ctx.Evento.FindAsync(id));
     }
 }
 public async void Delete(int id)
 {
     using (var ctx = new EventosDataContext())
     {
         ctx.Usuario.Remove(await ctx.Usuario.FindAsync(id));
         await ctx.SaveChangesAsync();
     }
 }
        private Usuario GetUser(string username, string password)
        {
            var ctx = new EventosDataContext();

            var user = ctx.Usuario.Where(c => c.UserName == username && c.Senha == password).FirstOrDefault();

            return(user);
        }
 public IEnumerable <Evento> ObterTodos()
 {
     using (var ctx = new EventosDataContext())
     {
         //var logger = _loggerFactory.CreateLogger("Debug");
         //logger.LogInformation("Solicitou lista de eventos");
         return(ctx.Evento.ToList());
     }
 }
        public async void Novo([FromBody] Evento evento)
        {
            using (var ctx = new EventosDataContext())
            {
                // _logger.LogInformation("Adicionou um novo Evento");
                await ctx.Evento.AddAsync(evento);

                await ctx.SaveChangesAsync();
            }
        }
        private Task <ClaimsIdentity> GetIdentity(string username, string password)
        {
            var ctx = new EventosDataContext();

            var userExists = ctx.Usuario.Where(c => c.UserName == username && c.Senha == password).FirstOrDefault();

            if (userExists != null)
            {
                return(Task.FromResult(new ClaimsIdentity(new System.Security.Principal.GenericIdentity(username, "Token"), new Claim[] { })));
            }

            return(Task.FromResult <ClaimsIdentity>(null));
        }
        private bool VerificaSeUsuarioJaExiste(string userName)
        {
            using (var ctx = new EventosDataContext())
            {
                var usuario = (from u in ctx.Usuario
                               where u.UserName == userName
                               select u).FirstOrDefault();

                if (usuario == null)
                {
                    return(false);
                }
            }

            return(true);
        }
        public async Task <IActionResult> Delete(int id)
        {
            try
            {
                using (var ctx = new EventosDataContext())
                {
                    // _logger.LogInformation("Excluiu um Evento");
                    ctx.Evento.Remove(await ctx.Evento.FindAsync(id));
                    await ctx.SaveChangesAsync();
                }

                return(Ok("Evento excluído"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IActionResult> Novo([FromBody] Usuario usuario)
        {
            if (!VerificaSeUsuarioJaExiste(usuario.UserName))
            {
                using (var ctx = new EventosDataContext())
                {
                    usuario.Senha = EncryptPassword.Encode(usuario.Senha);
                    await ctx.Usuario.AddAsync(usuario);

                    await ctx.SaveChangesAsync();
                }

                return(Ok(usuario));
            }
            else
            {
                return(BadRequest("Já existe Usuário com este nome!"));
            }
        }