Exemple #1
0
        public static int Add(Role role)
        {
            Role role2 = GetRole(role.Name);
            if (role2 != null)
                throw new Exception("There is another role has the same Name, please choose another Name");

            return RoleDataMapper.Add(role);
        }
Exemple #2
0
        public static void Update(Role role)
        {
            Role role2 = GetRole(role.Name);
            if (role2 != null && role2.ID != role.ID)
                throw new Exception("There is another role has the same Name, please choose another Name");

            RoleDataMapper.Update(role);
        }
        internal static int Add(Role roleEntity)
        {
            using (SqlConnection sqlConnection = new SqlConnection(CMSCoreBase.CMSCoreConnectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(SN_ROLE_ADD, sqlConnection);
                sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                SqlParameter sqlParameter = null;

                sqlParameter = new SqlParameter(PN_ROLE_ID, System.Data.SqlDbType.Int);
                sqlParameter.Direction = System.Data.ParameterDirection.Output;
                sqlParameter.Value = 0;
                sqlCommand.Parameters.Add(sqlParameter);

                sqlParameter = new SqlParameter(PN_ROLE_NAME, System.Data.SqlDbType.NVarChar);
                sqlParameter.Direction = System.Data.ParameterDirection.Input;
                sqlParameter.Value = roleEntity.Name;
                sqlCommand.Parameters.Add(sqlParameter);

                sqlParameter = new SqlParameter(PN_ROLE_DESCRIPTION, System.Data.SqlDbType.NVarChar);
                sqlParameter.Direction = System.Data.ParameterDirection.Input;
                sqlParameter.Value = roleEntity.Description;
                sqlCommand.Parameters.Add(sqlParameter);

                sqlParameter = new SqlParameter(PN_ROLE_IS_DELETED, System.Data.SqlDbType.Bit);
                sqlParameter.Direction = System.Data.ParameterDirection.Input;
                sqlParameter.Value = roleEntity.IsDeleted;
                sqlCommand.Parameters.Add(sqlParameter);

                try
                {
                    sqlCommand.Connection.Open();
                    sqlCommand.ExecuteNonQuery();
                    sqlCommand.Connection.Close();

                    roleEntity.ID = Convert.ToInt32(sqlCommand.Parameters[PN_ROLE_ID].Value);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return roleEntity.ID;
        }
        internal static void FillFromReader(Role role, SqlDataReader reader)
        {
            int colIndex = 0;

            colIndex = reader.GetOrdinal(CN_ROLE_ID);
            if (!reader.IsDBNull(colIndex))
                role.ID = reader.GetInt32(colIndex);

            colIndex = reader.GetOrdinal(CN_ROLE_DESCRIPTION);
            if (!reader.IsDBNull(colIndex))
                role.Description = reader.GetString(colIndex);

            colIndex = reader.GetOrdinal(CN_ROLE_IS_DELETED);
            if (!reader.IsDBNull(colIndex))
                role.IsDeleted = reader.GetBoolean(colIndex);

            colIndex = reader.GetOrdinal(CN_ROLE_NAME);
            if (!reader.IsDBNull(colIndex))
                role.Name = reader.GetString(colIndex);
        }
        internal static Role GetRole(List<Role> roles, SqlDataReader reader)
        {
            int colIndex = 0;
            colIndex = reader.GetOrdinal(CN_ROLE_ID);
            int value = reader.GetInt32(colIndex);

            Role role = roles.Where(c => c.ID == value).FirstOrDefault();
            if (role == null)
            {
                role = new Role();
                roles.Add(role);
            }
            return role;
        }
        internal static Role GetRoleByRoleName(string RoleName)
        {
            Role role = null;

            using (SqlConnection sqlConnection = new SqlConnection(CMSCoreBase.CMSCoreConnectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(SN_ROLE_GET_BY_CODE, sqlConnection);
                sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                SqlParameter parameter = new SqlParameter(PN_ROLE_NAME, System.Data.SqlDbType.NVarChar);
                parameter.Direction = System.Data.ParameterDirection.Input;
                parameter.Value = RoleName;
                sqlCommand.Parameters.Add(parameter);

                sqlCommand.Connection.Open();
                using (SqlDataReader reader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        if (role == null)
                            role = new Role();
                        FillFromReader(role, reader);
                    }
                    reader.Close();
                    sqlCommand.Connection.Close();
                }
            }
            return role;
        }