public static LocalidadeCategoria GetByID(int id)
        {
            DataTable           table     = new DataTable();
            LocalidadeCategoria categoria = null;

            OpenConn();
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(null, dbConn);
                command.CommandText = "SELECT * FROM localidade_categoria WHERE id = " + id;
                NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(command);
                Adpt.Fill(table);
                if (table.Rows.Count > 0)
                {
                    foreach (DataRow dr in table.Rows)
                    {
                        categoria = new LocalidadeCategoria
                        {
                            ID        = Convert.ToInt32(dr["id"]),
                            Nome      = dr["nome"].ToString(),
                            Descricao = dr["descricao"].ToString()
                        };
                        dbConn.Close();
                        return(categoria);
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("INVENTARIO/LocalidadeCategoriaDAO/GetAll:: " + e);
            }
            dbConn.Close();
            return(null);
        }
        public ActionResult Edit(int id)
        {
            // Início: Proteção de rota
            if (Session["usuario_nivel"] == null)
            {
                TempData["msg"]      = "Necessário estar logado!";
                TempData["msg_type"] = "warning";
                return(RedirectToAction("Index", "Home"));
            }

            if (Convert.ToInt32(Session["usuario_nivel"]) > 1)
            {
                TempData["msg"]      = "Você não tem autorização para acessar esta área!";
                TempData["msg_type"] = "danger";
                return(RedirectToAction("Index", "Home"));
            }
            // Fim: Proteção de Rota

            try
            {
                LocalidadeCategoria cat = LocalidadeCategoriaDAO.GetByID(id);
                if (cat != null)
                {
                    return(View(cat));
                }
            }
            catch
            {
            }
            TempData["msg"]      = "Erro buscar dados!";
            TempData["msg_type"] = "danger";
            return(RedirectToAction("List"));
        }
        public ActionResult Create(LocalidadeCategoria cat)
        {
            // Início: Proteção de rota
            if (Session["usuario_nivel"] == null)
            {
                TempData["msg"]      = "Necessário estar logado!";
                TempData["msg_type"] = "warning";
                return(RedirectToAction("Index", "Home"));
            }

            if (Convert.ToInt32(Session["usuario_nivel"]) > 1)
            {
                TempData["msg"]      = "Você não tem autorização para acessar esta área!";
                TempData["msg_type"] = "danger";
                return(RedirectToAction("Index", "Home"));
            }
            // Fim: Proteção de Rota

            try
            {
                if (LocalidadeCategoriaDAO.Create(cat) != null)
                {
                    TempData["msg"] = "Criado com sucesso!";
                }
                TempData["msg_type"] = "success";
                return(RedirectToAction("Index"));
            }
            catch
            {
            }
            TempData["msg"]      = "Erro ao criar!";
            TempData["msg_type"] = "danger";
            return(View());
        }
Exemplo n.º 4
0
        public static List <Localidade> GetByCategory(LocalidadeCategoria cat)
        {
            DataTable         table       = new DataTable();
            List <Localidade> localidades = new List <Localidade>();
            NpgsqlParameter   param;

            OpenConn();
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(null, dbConn);
                command.CommandText = "SELECT * FROM localidade WHERE categoria = @categoria";

                param       = new NpgsqlParameter("@categoria", NpgsqlTypes.NpgsqlDbType.Integer, 0);
                param.Value = cat.ID;
                command.Parameters.Add(param);

                NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(command);
                Adpt.Fill(table);

                if (table.Rows.Count > 0)
                {
                    foreach (DataRow dr in table.Rows)
                    {
                        localidades.Add(
                            new Localidade
                        {
                            ID        = Convert.ToInt32(dr["id"]),
                            Nome      = dr["nome"].ToString(),
                            Bloco     = dr["bloco"].ToString(),
                            Categoria = new LocalidadeCategoria
                            {
                                ID = Convert.ToInt32(dr["categoria"])
                            }
                        }
                            );
                    }
                }
                dbConn.Close();
                return(localidades);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("INVENTARIO/LocalidadeDAO/GetByCategory:: " + e);
                dbConn.Close();
                return(null);
            }
        }
        public static Boolean Update(LocalidadeCategoria categoria)
        {
            NpgsqlParameter param;

            OpenConn();
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(null, dbConn);
                command.CommandText = "UPDATE localidade_categoria SET nome = @nome, descricao = @descricao WHERE id =@id  RETURNING id";

                param       = new NpgsqlParameter("@nome", NpgsqlTypes.NpgsqlDbType.Varchar, 50);
                param.Value = categoria.Nome;
                command.Parameters.Add(param);

                param       = new NpgsqlParameter("@descricao", NpgsqlTypes.NpgsqlDbType.Varchar, 255);
                param.Value = categoria.Descricao;
                command.Parameters.Add(param);

                param       = new NpgsqlParameter("@id", NpgsqlTypes.NpgsqlDbType.Integer, 0);
                param.Value = categoria.ID;
                command.Parameters.Add(param);

                command.Prepare();

                var result = command.ExecuteScalar();
                dbConn.Close();

                if (result != null && (int)result > 0)
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("INVENTARIO/LocalidadeCategoriaDAO/Update:: " + e);

                dbConn.Close();
            }
            return(false);
        }
        // Cria subcategoria
        public static object Create(LocalidadeCategoria categoria)
        {
            NpgsqlParameter param;

            if (dbConn == null)
            {
                dbConn = Database.Conexao;
            }
            else
            {
                dbConn.Open();
            }
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(null, dbConn);
                command.CommandText = "INSERT INTO localidade_categoria(nome, descricao) values (@nome, @descricao) RETURNING id";

                param       = new NpgsqlParameter("@nome", NpgsqlTypes.NpgsqlDbType.Varchar, 50);
                param.Value = categoria.Nome;
                command.Parameters.Add(param);

                param       = new NpgsqlParameter("@descricao", NpgsqlTypes.NpgsqlDbType.Varchar, 255);
                param.Value = categoria.Descricao;
                command.Parameters.Add(param);

                command.Prepare();
                var result = command.ExecuteScalar();
                dbConn.Close();

                return(result);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("INVENTARIO/LocalidadeCategoriaDAO/Create:: " + e);
                dbConn.Close();
                return(null);
            }
        }