Exemplo n.º 1
0
        private void AcceptFriendInvitation(string username, string friendusername)
        {
            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentNullException(nameof(username));
            }

            if (string.IsNullOrWhiteSpace(friendusername))
            {
                throw new ArgumentNullException(nameof(friendusername));
            }

            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.OpenAsync();
                string topicname = username + "1" + friendusername;

                string query = $"Update Friends Set Accepted = 'Y' where UserName = '******' and FriendsUsername = '******'";

                string query1 = $"INSERT INTO Friends" +
                                $" (UserName,FriendsUsername,Accepted) " +
                                $"VALUES('{username}', '{friendusername}', 'Y')";

                string query2 = $"Create table `{topicname}`(messageid int auto_increment not null primary key, SenderName varchar(50) not null, Messege varchar(1000));";

                MySqlCommand cmd = new MySqlCommand(query, connection);

                cmd.ExecuteNonQuery();
                cmd.CommandText = query1;
                cmd.ExecuteNonQuery();
                cmd.CommandText = query2;
                cmd.ExecuteNonQuery();

                connection.CloseAsync();
                AzureServiceBusHelper.CreateTopic(topicname);
                AzureServiceBusHelper.CreateSubscription(topicname, new List <string>()
                {
                    username, friendusername
                });
                Console.WriteLine("Successfully added new friend");
            }
        }
Exemplo n.º 2
0
        public static void CreateMultiplechat(string user, string tabletopic)
        {
            List <string> topics = AzureServiceBusHelper.TakeAllTopicsForUser(user);
            string        topic  = null;
            bool          f      = true;

            foreach (var top in topics.OrderBy(x => x.Length))
            {
                f = true;
                foreach (var name in (tabletopic + '1' + user).ToString().Split('1').ToList())
                {
                    if (top.Split('1').ToList().Contains(name) && top.Split('1').Length == (tabletopic + '1' + user).ToString().Split('1').Length&& f)
                    {
                        topic = top;
                        break;
                    }
                    else
                    {
                        f     = false;
                        topic = null;
                        continue;
                    }
                }
            }
            if (topic == null && tabletopic.Contains("1"))
            {
                AzureServiceBusHelper.CreateTopic(tabletopic);
                AzureServiceBusHelper.CreateSubscription(tabletopic, tabletopic.Split('1').ToList());
                string query = $"Create table `{tabletopic}`(messageid int auto_increment not null pimary key, SenderName varchar(50) not null, Messege varchar(1000));";
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    connection.OpenAsync();
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    cmd.ExecuteNonQuery();
                    connection.CloseAsync();
                }
                Console.WriteLine("New group chat has been successfully created!");
            }
            else
            {
                string query = $"SELECT * FROM ChatApp.{topic} order by messageid";

                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    connection.OpenAsync();
                    //create command and assign the query and connection from the constructor
                    MySqlCommand cmd = new MySqlCommand(query, connection);

                    MySqlDataReader dataReader = cmd.ExecuteReader();

                    //Read the data and store them in the list
                    int i = 10;
                    while (dataReader.Read())
                    {
                        Console.WriteLine(dataReader["SenderName"].ToString().ToLowerInvariant() + ": " + dataReader["Messege"].ToString());
                        i--;
                        if (i < 0)
                        {
                            break;
                        }
                    }

                    //close Data Reader
                    dataReader.Close();

                    connection.CloseAsync();
                }
            }
        }