コード例 #1
0
ファイル: UtilizadorDAO.cs プロジェクト: devzizu/umfit
        /*
         * Recebe a interface do utilizador, o tipo de utilizador(0, 1 ou 2), ou seja,
         * Cliente, Instrutor ou Rececionista
         * e recebe a hash da password
         */
        public bool InsertUser(InterfaceUtilizador user, int type, string hashPass)
        {
            object res = false;

            try
            {
                string sqlCommand;

                MySqlCommand command = null;

                // 0 - Cliente, 1 - Instrutor, 2 - Rececionista
                if (type == 0)
                {
                    Cliente u = (Cliente)user;

                    ExisteLocal(u.localidade);

                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }

                    sqlCommand = "insert into Cliente (email, nif, nome, hashpass, data_nascimento, " +
                                 "genero, categoria, localidade) " +
                                 "select * from (select @EMAIL as em, @NIF as ni, @NOME as nom, @HASHPASS as hashp," +
                                 "@DATA_NASCIMENTO as dat, @GENERO as gen, @CATEGORIA as cat, @LOCALIDADE as loc" +
                                 ") as tmp " +
                                 "where not exists (select email from Cliente " +
                                 "where email = @EMAIL or nif = @NIF) limit 1";

                    command = new MySqlCommand(sqlCommand, connection);

                    command.Parameters.Add(new MySqlParameter("@HASHPASS", MySqlDbType.VarChar));
                    command.Parameters["@HASHPASS"].Value = hashPass;

                    u.IniParamSql(command);
                }
                else if (type == 1)
                {
                    Instrutor u = (Instrutor)user;

                    ExisteLocal(u.localidade);

                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }

                    sqlCommand = "insert into Instrutor (email, nif, nome, hashpass, data_nascimento, " +
                                 "genero, localidade) " +
                                 "select * from (select @EMAIL as em, @NIF as ni, @NOME as nom, @HASHPASS as hashp," +
                                 "@DATA_NASCIMENTO as dat, @GENERO as gen, @LOCALIDADE as loc" +
                                 ") as tmp " +
                                 "where not exists (select email from Instrutor " +
                                 "where email = @EMAIL or nif = @NIF) limit 1";

                    command = new MySqlCommand(sqlCommand, connection);

                    command.Parameters.Add(new MySqlParameter("@HASHPASS", MySqlDbType.VarChar));
                    command.Parameters["@HASHPASS"].Value = hashPass;

                    u.IniParamSql(command);
                }
                else if (type == 2)
                {
                    Rececionista u = (Rececionista)user;

                    ExisteLocal(u.localidade);

                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }

                    sqlCommand = "insert into Rececionista (email, nif, nome, hashpass, data_nascimento, " +
                                 "genero, localidade) " +
                                 "select * from (select @EMAIL as em, @NIF as ni, @NOME as nom, @HASHPASS as hashp," +
                                 "@DATA_NASCIMENTO as dat, @GENERO as gen, @LOCALIDADE as loc" +
                                 ") as tmp " +
                                 "where not exists (select email from Rececionista " +
                                 "where email = @EMAIL or nif = @NIF) limit 1";

                    command = new MySqlCommand(sqlCommand, connection);

                    command.Parameters.Add(new MySqlParameter("@HASHPASS", MySqlDbType.VarChar));
                    command.Parameters["@HASHPASS"].Value = hashPass;

                    u.IniParamSql(command);
                }

                res = command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                connection.Close();
            }

            return(res.ToString().Equals("1") ? true : false);
        }