private static void processLogin(PacoteLogin pacoteLogin, ClientData cd)
        {
            Utilizador u_pacote      = pacoteLogin.GetUtilizador();
            Utilizador u             = dao_gestUtil.LoginUtilizador(u_pacote.GetUsername(), u_pacote.GetPassword());
            Socket     client_socket = cd.GetSocket();

            if (u == null)
            {
                PacoteLogin pl_send = new PacoteLogin(null, "Login Failed!");
                AMessage    sending = Serializer.Serialize(pl_send);

                client_socket.BeginSend(sending.Data, 0, sending.Data.Length, SocketFlags.None, new AsyncCallback(SendCallback), cd);
            }
            else
            {
                if (u is Cliente)
                {
                    Cliente     c       = (Cliente)u;
                    PacoteLogin pl_send = new PacoteLogin(c, "CSucesso");
                    AMessage    sending = Serializer.Serialize(pl_send);
                    cd.SetUtilizador(c);
                    TurnOn.SetActivityText(c.GetUsername() + " " + c.GetPassword() + "\n");
                    client_socket.BeginSend(sending.Data, 0, sending.Data.Length, SocketFlags.None, new AsyncCallback(SendCallback), cd);
                }
                else
                {
                    Proprietario p = (Proprietario)u;
                    cd.SetUtilizador(p);
                    PacoteLogin pl_send = new PacoteLogin(p, "PSucesso");
                    AMessage    sending = Serializer.Serialize(pl_send);
                    client_socket.BeginSend(sending.Data, 0, sending.Data.Length, SocketFlags.None, new AsyncCallback(SendCallback), cd);
                }
            }
        }
Example #2
0
        /*-----------------------------------------------------------------------
         * Método que realiza o registo de um Proprietário na base de dados,
         * assim como o seu(s) restaurante(s) e pratos do respectivo restaurante
         * ------------------------------------------------------------------------*/
        private int RegistaProprietario(Utilizador u)
        {
            int idade, r = 1;; string nome, usr, psw, email;

            nome  = u.GetNome();
            idade = u.GetIdade(); usr = u.GetUsername();
            psw   = u.GetPassword(); email = u.GetEmail();
            List <Restaurante> lista = ((Proprietario)u).GetRestaurantesProprietario();

            try
            {
                myConnection.Open();
                SqlDataReader myReader = null;

                string username = u.GetUsername();
                //Verificar se já existe um username igual ao introduzido
                SqlCommand myCommand = new SqlCommand("SELECT P.Username FROM Proprietario AS P" +
                                                      "WHERE P.Username = '******'; ", myConnection);
                myReader = myCommand.ExecuteReader();
                if (myReader.Read())
                {
                    myReader.Close();
                    return(-2);
                }
                myReader.Close();


                //Inserir o proprietario na base de dados
                myCommand = new SqlCommand("INSERT INTO [dbo].[Proprietario] " +
                                           "([Nome], [Idade], [Username], [Password], [FotoPerfil], [Email] " +
                                           "VALUES " +
                                           "('" + nome + "', " + idade + ", '" + usr + "', '" + psw + "', " +
                                           " NULL, '" + email + "' );", myConnection);
                myReader = myCommand.ExecuteReader();
                myReader.Close();
                myConnection.Close();  //Evitar conflitos com proximas conexões

                RegistaRestaurantes(u);

                r = 0;
            }
            catch (Exception e) { Console.WriteLine(e); }
            return(r);
        }
Example #3
0
        /*--------------------------------------------------------------------
         * Método que realiza o registo de um cliente na base de dados
         * ---------------------------------------------------------------------*/
        private int RegistaCliente(Utilizador u)
        {
            int idade, ordem, r = 1;;       string nome, usr, psw, email;

            nome  = u.GetNome();
            idade = u.GetIdade();          usr = u.GetUsername();
            psw   = u.GetPassword();         email = u.GetEmail();
            ordem = ((Cliente)u).GetPreferencia().GetOrdemPreferencia();
            try
            {
                myConnection.Open();
                SqlDataReader myReader = null;

                string username = u.GetUsername();

                //Verificar se já existe um username igual ao introduzido
                SqlCommand myCommand = new SqlCommand("SELECT C.Username FROM Cliente AS C" +
                                                      "WHERE C.Username = '******'; ", myConnection);
                myReader = myCommand.ExecuteReader();
                if (myReader.Read())
                {
                    myReader.Close();
                    return(-2);
                }
                myReader.Close();

                myCommand = new SqlCommand("INSERT INTO [dbo].[Cliente] " +
                                           "([Nome], [Idade], [Username], [Password], [FotoPerfil], [Email], [OrdemPreferencia]) " +
                                           "VALUES " +
                                           "('" + nome + "', " + idade + ", '" + usr + "', '" + psw + "', " +
                                           " NULL, '" + email + "', " + ordem + ") ", myConnection);

                myReader = myCommand.ExecuteReader();
                myReader.Close();
                myConnection.Close();  //Evitar conflitos com proximas conexões
                r = 0;
            }
            catch (Exception e) { Console.WriteLine(e); }
            return(r);
        }
Example #4
0
        /*-----------------------------------------------------------------------
         * Método que realiza o registo do(s) restaurante(s) e pratos do
         * respectivo restaurante de um determinado utilizador Proprietário
         * ------------------------------------------------------------------------*/
        public void RegistaRestaurantes(Utilizador u)
        {
            try
            {
                myConnection.Open();

                SqlDataReader      myReader = null;
                string             usr = u.GetUsername(), psw = u.GetPassword();
                List <Restaurante> listaRest = ((Proprietario)u).GetRestaurantesProprietario();

                //Determinar o id atribuido ao proprietario acabado de inserir
                SqlCommand myCommand = new SqlCommand("SELECT P.idProprietario FROM Proprietario AS P " +
                                                      "WHERE P.Username = '******' " +
                                                      "AND P.Password = '******'; ", myConnection);
                int idProp = (-1);
                myReader = myCommand.ExecuteReader();
                if (myReader.Read())
                {
                    idProp = Convert.ToInt32(myReader["idProprietario"].ToString());
                }
                myReader.Close();

                int    idTipoCozinha, contacto;
                string desig, local; double pontos;
                //Inserir os restaurantes do proprietário (pelo menos existe 1 estabelecimento)
                foreach (Restaurante rest in listaRest)
                {
                    desig         = rest.GetDesignacao(); pontos = rest.GetPontuacao();
                    local         = rest.GetLocalizacao(); contacto = rest.GetContacto();
                    idTipoCozinha = rest.GetIdTipoCozinha();
                    List <Prato> listaPratos = rest.GetPratos();

                    //Inserir o restaurante, associado ao seu proprietário
                    myCommand = new SqlCommand("INSERT INTO [dbo].[Restaurante] " +
                                               "([Designacao], [Pontuacao], [Localizacao], [Contacto], [idproprietario],[idTipoCozinha]) " +
                                               "VALUES " +
                                               "( '" + desig + "', " + pontos + ", '" + local + "', " + contacto + ", " + idProp + ", " + idTipoCozinha + ";", myConnection);
                    myReader = myCommand.ExecuteReader();
                    myReader.Close();

                    //Determinar o id atribuido ao restaurante acabado de adicionar
                    myCommand = new SqlCommand("SELECT R.idRestaurante FROM Restaurante AS R " +
                                               "WHERE R.idproprietario = " + idProp + " AND R.Designacao = '" + desig + "';", myConnection);
                    int idRest = (-1);
                    myReader = myCommand.ExecuteReader();
                    if (myReader.Read())
                    {
                        idRest = Convert.ToInt32(myReader["idRestaurante"].ToString());
                    }
                    myReader.Close();

                    //Para cada restaurante insirir a sua lista de restaurantes
                    string desc; double preco;
                    foreach (Prato p in listaPratos)
                    {
                        desig = p.GetDesignacao();
                        desc  = p.GetDescricao();
                        preco = p.GetPreco();

                        myCommand = new SqlCommand("INSERT INTO [dbo].[Prato] " +
                                                   "([Designacao], [Descricao], [Preco], [idRestaurante]) " +
                                                   "VALUES " +
                                                   "( '" + desig + "', '" + desc + "', " + preco + ", " + idRest + "); ", myConnection);
                        myReader = myCommand.ExecuteReader();
                        myReader.Close();
                    }
                }
            }
            catch (Exception e) { Console.WriteLine(e); }
        }