Exemplo n.º 1
0
        public Usuario Login(string user, string pwd, int empresaId)
        {
            IConnection conn = ConnectionFactory.GetConnection();

            string sql = $@"
                Select * 
                  From {ClassManipulation.GetTableName<Usuario>()}
                 Where Login = {conn.SqlParameter}user
                   And Senha = {conn.SqlParameter}pwd
                   And Ativo = 1
                   And EmpresaID = {empresaId}
                ";

            var command = conn.Db.CreateCommand();

            var param = command.CreateParameter();

            param.ParameterName = "user";
            param.Value         = user;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "pwd";
            param.Value         = pwd;
            command.Parameters.Add(param);

            command.CommandText = sql;

            using (DbDataReader reader = command.ExecuteReader())
                return(DataReaderManipulation.DataReaderToClass <Usuario>(reader));
        }
Exemplo n.º 2
0
        public List <T> List()
        {
            string sql = $@"
                Select * 
                  From {ClassManipulation.GetTableName<T>()}";

            if (typeof(T) is IChildEmpresaObject)
            {
                if (!EmpresaId.HasValue)
                {
                    throw new BrokenRulesException("Não foi informado o código da empresa logada");
                }
                sql += $" Where EmpresaId == {EmpresaId.Value}";
            }

            IConnection conn = ConnectionFactory.GetConnection();

            var command = conn.Db.CreateCommand();

            command.CommandText = sql;
            DbDataReader reader = command.ExecuteReader();

            return(DataReaderManipulation.DataReaderMapToList <T>(reader));
        }
Exemplo n.º 3
0
        public T Find(int identity)
        {
            string identityColumn = ClassManipulation.GetIdentityColumn <T>();
            string sql            = $@"
                Select * 
                  From {ClassManipulation.GetTableName<T>()}
                 Where {identityColumn} = {string.Concat(ConnectionFactory.SqlParameter, identityColumn)}";

            IConnection conn = ConnectionFactory.GetConnection();

            var command = conn.Db.CreateCommand();

            var param = command.CreateParameter();

            param.Value         = identity;
            param.ParameterName = identityColumn;
            command.Parameters.Add(param);
            command.CommandText = sql;

            using (IDataReader reader = command.ExecuteReader())
            {
                return(DataReaderManipulation.DataReaderToClass <T>(reader));
            }
        }
Exemplo n.º 4
0
        public List <T> List(FilterObject <T> filter)
        {
            var    model = filter.Model;
            string sql   = $@"
                Select * 
                  From {ClassManipulation.GetTableName<T>()}";

            IConnection conn = ConnectionFactory.GetConnection();

            var command = conn.Db.CreateCommand();

            if (model != null && filter != null && filter.Properties.Count > 0)
            {
                sql += " Where ";
                for (var i = 0; i < filter.Properties.Count; i++)
                {
                    var prop = filter.Properties[i];

                    sql += prop.LogicalOperator;

                    var property = ClassManipulation.GetColumn <T>(prop.Property);
                    sql += property.Name + " "
                           + prop.Operator + " "
                           + ConnectionFactory.SqlParameter + property.Name;

                    var param = command.CreateParameter();
                    param.ParameterName = property.Name;

                    if (prop.OperatorEnum == OperatorEnum.Like)
                    {
                        param.Value = prop.PreAppend + property.GetValue(model) + prop.PosAppend;
                    }
                    else
                    {
                        param.Value = property.GetValue(model);
                    }

                    command.Parameters.Add(param);
                }
                if (typeof(T) is IChildEmpresaObject)
                {
                    if (!EmpresaId.HasValue)
                    {
                        throw new BrokenRulesException("Não foi informado o código da empresa logada");
                    }
                    sql += $" And EmpresaId == {EmpresaId.Value}";
                }
            }
            else
            {
                if (typeof(T) is IChildEmpresaObject)
                {
                    if (!EmpresaId.HasValue)
                    {
                        throw new BrokenRulesException("Não foi informado o código da empresa logada");
                    }
                    sql += $" Where EmpresaId == {EmpresaId.Value}";
                }
            }

            command.CommandText = sql;

            using (DbDataReader reader = command.ExecuteReader())
                return(DataReaderManipulation.DataReaderMapToList <T>(reader));
        }