Exemplo n.º 1
0
        public GetChatContacts getContacts(GetChatContacts contactsCmd)
        {
            GetChatContacts contacts = new GetChatContacts
            {
                usersname    = contactsCmd.usersname,
                contactNames = new List <string>()
            };

            if (openConnection() == true)
            {
                string query = $@"(SELECT receiver FROM chat WHERE sender = '{contacts.usersname}') UNION 
                                (SELECT sender FROM chat WHERE receiver = '{contacts.usersname}');";

                MySqlCommand    cmd    = new MySqlCommand(query, connection);
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    contacts.contactNames.Add(reader.GetString("receiver"));
                }

                closeConnection();

                return(contacts);
            }
            else
            {
                Debug.consoleMsg("Unable to connect to database");
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Selects the names of all other users that the given user has made chat contact with in the past
        /// </summary>
        /// <param name="usersname">The name of the user</param>
        /// <returns>A list of usernames the user has sent at least one chat message to</returns>
        public GetChatContactsResponse getAllChatContactsForUser(GetChatContactsRequest request)
        {
            //TODO low importance: Turn this from max 3 queries to 2 for added efficiency
            bool   result   = false;
            string response = "";

            GetChatContacts requestData = request.getCommand;

            if (openConnection() == true)
            {
                List <string>   contacts = new List <string>();
                MySqlDataReader reader   = null;
                try
                {
                    string query = "SELECT * FROM " + databaseName + ".chats " +
                                   "WHERE usersname='" + requestData.usersname + "' OR companyname='" + requestData.usersname + "';";

                    MySqlCommand command = new MySqlCommand(query, connection);
                    reader = command.ExecuteReader();

                    while (reader.Read() == true)
                    {
                        if (requestData.usersname.Equals(reader.GetString("usersname")))
                        {
                            contacts.Add(reader.GetString("companyname"));
                        }
                        else
                        {
                            contacts.Add(reader.GetString("usersname"));
                        }
                    }

                    requestData.contactNames = contacts;
                    result = true;
                }
                catch (Exception e)
                {
                    response = e.Message;
                }
                finally
                {
                    if (reader != null && reader.IsClosed == false)
                    {
                        reader.Close();
                    }
                    closeConnection();
                }

                return(new GetChatContactsResponse(result, response, requestData));
            }

            return(new GetChatContactsResponse(false, "Could not connect to database", requestData));
        }
Exemplo n.º 3
0
        public Task Handle(GetChatContactsRequest message, IMessageHandlerContext context)
        {
            Debug.consoleMsg("Got to GetChatContactHandler");


            //Error Checking
            string          response  = "Success";
            GetChatContacts returnVal = ChatServiceDatabase.getInstance().getChatContacts(message.getCommand.usersname);

            Debug.consoleMsg("Size:" + returnVal.contactNames.Count);

            return(context.Reply(new GetChatContactsResponse(true, response, returnVal)));
        }
Exemplo n.º 4
0
        public ActionResult Index()
        {
            if (Globals.isLoggedIn() == false)
            {
                return(RedirectToAction("Index", "Authentication"));
            }

            ServiceBusConnection connection = ConnectionManager.getConnectionObject(Globals.getUser());

            if (connection == null)
            {
                return(RedirectToAction("Index", "Authentication"));
            }

            GetChatContacts getContactsCommand = new GetChatContacts
            {
                usersname    = Globals.getUser(),
                contactNames = null
            };

            GetChatContactsRequest  contactsRequest  = new GetChatContactsRequest(getContactsCommand);
            GetChatContactsResponse contactsResponse = connection.getAllChatContacts(contactsRequest);

            ChatHistory firstDisplayedChatHistory = null;

            if (contactsResponse.responseData.contactNames.Count != 0)
            {
                GetChatHistory getHistoryCommand = new GetChatHistory()
                {
                    history = new ChatHistory
                    {
                        user1 = Globals.getUser(),
                        user2 = contactsResponse.responseData.contactNames[0]
                    }
                };

                GetChatHistoryRequest historyRequest = new GetChatHistoryRequest(getHistoryCommand);
                firstDisplayedChatHistory = connection.getChatHistory(historyRequest).responseData.history;
            }
            else
            {
                firstDisplayedChatHistory = new ChatHistory();
            }

            ViewBag.ChatInstances        = contactsResponse.responseData.contactNames;
            ViewBag.DisplayedChatHistory = firstDisplayedChatHistory;

            return(View());
        }
Exemplo n.º 5
0
        public ServiceBusResponse getContacts2(GetChatContacts contacts)
        {
            GetChatContacts resp = new GetChatContacts
            {
                usersname    = contacts.usersname,
                contactNames = new List <string>()
            };

            if (openConnection() == true)
            {
                try
                {
                    string query = $@"SELECT DISTINCT sender,receiver FROM chat WHERE 
                                    (sender = '{contacts.usersname}' OR receiver = '{contacts.usersname}') GROUP BY sender,receiver;";

                    MySqlCommand    cmd    = new MySqlCommand(query, connection);
                    MySqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        string rec = reader.GetString("receiver");
                        string sen = reader.GetString("sender");

                        if (String.Equals(sen, contacts.usersname))
                        {
                            resp.contactNames.Add(rec);
                        }
                        else
                        {
                            resp.contactNames.Add(sen);
                        }
                    }

                    closeConnection();

                    return(new ServiceBusResponse(true, JsonConvert.SerializeObject(resp)));
                }
                catch (Exception err)
                {
                    return(new ServiceBusResponse(false, err.Message));
                }
            }
            else
            {
                return(new ServiceBusResponse(false, "Unable to connect to database."));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Searches for company information
        /// </summary>
        /// <param name="message">Information about the company</param>
        /// <param name="context">Used to access information regarding the endpoints used for this handle</param>
        /// <returns>The response to be sent back to the calling process</returns>
        public Task Handle(GetChatContactsRequest message, IMessageHandlerContext context)
        {
            GetChatContactsResponse response;
            // TODO: May need to fix. Currently hardcoded to always retrieve client contacts.
            GetChatContacts conts = new GetChatContacts(message.getCommand.usersname, ChatDatabase.getInstance().getContacts(message.getCommand.usersname));

            if (conts.contactNames.Count == 0)
            {
                response = new GetChatContactsResponse(false, "Could not find any contacts.", conts);
            }
            else
            {
                response = new GetChatContactsResponse(true, "Successfully retrieved contacts.", conts);
            }

            return(context.Reply(response));
        }
Exemplo n.º 7
0
        public GetChatContactsResponse retrieveChatContacts(string usersname)
        {
            string query = @"SELECT * FROM " + dbname + ".chatcontacts "
                           + "WHERE usersname='" + usersname + "';";

            bool            result   = false;
            string          message  = "";
            GetChatContacts contacts = new GetChatContacts();

            contacts.usersname    = usersname;
            contacts.contactNames = new List <string>();

            if (openConnection() == true)
            {
                result = true;

                MySqlCommand    command    = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = command.ExecuteReader();
                // if tuples are returned
                if (dataReader.HasRows)
                {
                    while (dataReader.Read())
                    {
                        contacts.contactNames.Add(dataReader.GetString(2));
                    }

                    dataReader.Close();
                    closeConnection();
                    return(new GetChatContactsResponse(result, message, contacts));
                }
                // no tuples returned
                else
                {
                    dataReader.Close();
                    closeConnection();
                    return(new GetChatContactsResponse(result, message, contacts));
                }
            }
            else
            {
                result  = false;
                message = "Could not connect to database.";
            }
            return(new GetChatContactsResponse(result, message, contacts));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the contacts for a user from DB.
        /// </summary>
        /// <param name="request"></param>
        public GetChatContactsResponse getChatContacts(GetChatContactsRequest request)
        {
            bool            result       = false;
            string          message      = "";
            GetChatContacts chatContacts = new GetChatContacts();

            chatContacts.contactNames = new List <string>();
            if (openConnection() == true)
            {
                string query = @"SELECT RECEIVER FROM " + dbname + @".CHAT WHERE SENDER = '" + request.getCommand.usersname +
                               @"' UNION SELECT SENDER FROM " + dbname + @".CHAT WHERE RECEIVER = '" + request.getCommand.usersname +
                               @"';";
                try
                {
                    MySqlCommand    command = new MySqlCommand(query, connection);
                    MySqlDataReader reader  = command.ExecuteReader();
                    while (reader.Read())
                    {
                        chatContacts.contactNames.Add(reader.GetString("RECEIVER"));
                    }
                    reader.Close();
                    result = true;
                } catch (MySqlException e)
                {
                    message = e.Message;
                } catch (Exception e) {
                    Messages.Debug.consoleMsg("Unable to complete select from chat contacts database." +
                                              " Error :" + e.Message);
                    Messages.Debug.consoleMsg("The query was:" + query);
                    chatContacts.contactNames.Add(e.Message);
                    message = e.Message;
                } finally
                {
                    closeConnection();
                }
            }
            else
            {
                Debug.consoleMsg("Unable to connect to database");
                message = "Unable to connect to database";
            }
            return(new GetChatContactsResponse(result, message, chatContacts));
        }
Exemplo n.º 9
0
        public GetChatContacts getChatContacts(string userName)
        {
            if (openConnection() == true)
            {
                string query = "SELECT DISTINCT receiver FROM " + dbname + ".chathistory WHERE sender = @Username" +
                               " UNION SELECT DISTINCT sender FROM " + dbname + ".chathistory WHERE receiver = @Username;";

                MySqlCommand command = new MySqlCommand(query, connection);
                command.Parameters.AddWithValue("@Username", userName);
                MySqlDataReader reader = command.ExecuteReader();

                List <string> companies = new List <string>();

                if (reader.Read())
                {
                    do
                    {
                        Debug.consoleMsg(reader.GetString("receiver"));
                        companies.Add(reader.GetString("receiver"));
                    } while (reader.Read());
                }
                else
                {
                    Debug.consoleMsg("Error: No such user: '******' in database");
                }

                GetChatContacts ret = new GetChatContacts()
                {
                    usersname    = userName,
                    contactNames = companies
                };
                Debug.consoleMsg("Leaving Function");
                closeConnection();
                return(ret);
            }
            else
            {
                Debug.consoleMsg("Unable to connect to database");
                closeConnection();
                return(null);
            }
        }
Exemplo n.º 10
0
        public Task Handle(GetChatContactsRequest message, IMessageHandlerContext context)
        {
            try
            {
                GetChatContacts contacts = ChatDatabase.getInstance().getContacts(message.getCommand);

                if (contacts != null)
                {
                    return(context.Reply(new GetChatContactsResponse(true, "Successful", contacts)));
                }
                else
                {
                    return(context.Reply(new GetChatContactsResponse(false, "Unable to connect to database", message.getCommand)));
                }
            }
            catch (Exception err)
            {
                return(context.Reply(new GetChatContactsResponse(false, err.Message, message.getCommand)));
            }
        }
Exemplo n.º 11
0
 public GetChatContactsRequest(GetChatContacts getCommand)
     : base(ChatRequest.getChatContacts)
 {
     this.getCommand = getCommand;
 }
 public GetChatContactsResponse(bool result, string response, GetChatContacts responseData)
     : base(result, response)
 {
     this.responseData = responseData;
 }
Exemplo n.º 13
0
        public GetChatContactsResponse getContacts(string userName)
        {
            string responseMessage = "";
            bool   result          = false;

            GetChatContacts userResponseData = new GetChatContacts()
            {
                usersname    = userName,
                contactNames = new List <string>()
            };

            if (openConnection() == true)
            {
                List <string> userContacts = new List <String>();

                string          query        = @"SELECT DISTINCT receiver FROM chatdata WHERE sender = '" + userName + @"'";
                MySqlCommand    doQuery      = new MySqlCommand(query, connection);
                MySqlDataReader incomingData = doQuery.ExecuteReader();

                while (incomingData.Read())
                {
                    userContacts.Add(incomingData.GetString("receiver"));
                }
                incomingData.Close();

                query        = @"SELECT DISTINCT sender FROM chatdata WHERE receiver = '" + userName + @"'";
                doQuery      = new MySqlCommand(query, connection);
                incomingData = doQuery.ExecuteReader();

                String userContact;
                while (incomingData.Read())
                {
                    userContact = incomingData.GetString("sender");
                    if (!userContacts.Contains(userContact))
                    {
                        userContacts.Add(userContact);
                    }
                }
                incomingData.Close();

                if (userContacts.Count() != 0)
                {
                    result = true;
                    userResponseData.contactNames = userContacts;
                }
                else
                {
                    responseMessage = "No user contacts were found.";
                }

                incomingData.Close();
                closeConnection();
            }
            else
            {
                responseMessage = "Error connecting to the database.";
                Debug.consoleMsg("Error connecting to the database.");
            }

            return(new GetChatContactsResponse(result, responseMessage, userResponseData));
        }