public void TestGet20MessagesSuccess()
 {
     ChatMessage chatMessage = new ChatMessage("fedt lavet", myUser);
     chatController.SendMessage(chatMessage, myFile);
     List<ChatMessage> messages = chatController.GetLast20MessagesFromFile(myFile);
     Assert.IsNotNull(messages);
 }
        /// <summary>
        /// Adds a message to the ChatMessage table and adds the references to FileChats
        /// </summary>
        /// <param name="chatMessage">a ChatMessage</param>
        /// <param name="fileId">fileId for the given file the message is going to be added to</param>
        /// <returns></returns>
        public bool SendMessageToFile(ChatMessage chatMessage, int fileId)
        {
            try
            {
                File file = dbFile.GetFile(fileId);
                chatMessage.Sender = userController.FindUserById(chatMessage.Sender.Id);
                TransactionOptions options = new TransactionOptions();
                options.IsolationLevel = IsolationLevel.ReadCommitted;
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
                {
                    dbContext.ChatMessages.InsertOnSubmit(chatMessage);
                    dbContext.SubmitChanges();
                    //ChatMessage s = chatMessage;
                    //string sql = "INSERT INTO FileChat VALUES (" + file.Id + "," + chatMessage.Id + ")";
                    //dbContext.ExecuteCommand(sql);
                    //dbContext.SubmitChanges();

                    dbContext.FileChats.InsertOnSubmit(new FileChat{chatMessage = chatMessage, file = file});
                    dbContext.SubmitChanges();
                    scope.Complete(); //TODO check if the data added to db were sucessfull / valid.
                }
            }
            catch (Exception e)
            {
                return false;
                throw new Exception("Chat not added to file \n" + e);
            }
            return true;
            throw new NotImplementedException();
        }
        private void UpdateChat(ChatMessage[] messages)
        {
            //listBox_Filechat.Items.Clear();

            if (messages.Count() > 0)
            {
                if(messages.First().Time.Date < LastDate)
                {
                    LastDate = messages.First().Time;
                    listBox_Filechat.Items.Add("\t\t *** " + LastDate.Date.ToLongDateString() + " ***");
                }

                foreach (ChatMessage message in messages)
                {
                    if (LastDate.Date < message.Time.Date)
                    {
                        LastDate = message.Time.Date;
                        listBox_Filechat.Items.Add("\t\t *** " + message.Time.Date.ToLongDateString() + " ***");
                    }
                    lastMessageId = message.Id;
                    listBox_Filechat.Items.Add(message.Time.ToShortTimeString() + "  " + message.Sender.Username + ": \n" + message.Message);
                }
                listBox_Filechat.SelectedIndex = listBox_Filechat.Items.Count - 1;
                listBox_Filechat.SelectedIndex = -1;
            }
        }
        private void SendMessageToFile()
        {
            if (activeFile != null)
            {
                ChatMessage message = new ChatMessage(richTextBox_Filechat.Text, activeUser);
                bool success = chatService.SendMessageToFile(message, activeFile);

                if (success)
                {
                    //listBox_Filechat.Items.Add(DateTime.Now.ToShortTimeString() + " " + message.Sender.Username + ":\n " + message.Message);
                    richTextBox_Filechat.Text = "";
                }
                else
                {
                    listBox_Filechat.Items.Add("ERROR: Message was not sent!");
                }
            }
            else
            {
                listBox_Filechat.Items.Add("ERROR: You need to select a file first.");
            }
        }
 public void TestSendMessageSuccess()
 {
     ChatMessage chatMessage = new ChatMessage("fedt arbejde", myUser);
     bool success = chatController.SendMessage(chatMessage, myFile);
     Assert.IsTrue(success);
 }
 public void TestSendMessageFailUser()
 {
     ChatMessage chatMessage = new ChatMessage("fedt arbejde", null);
     bool success = chatController.SendMessage(chatMessage, myFile);
     Assert.IsFalse(success);
 }
 public void TestSendMessageFailEmptyMessage()
 {
     ChatMessage chatMessage = new ChatMessage("", myUser);
     bool success = chatController.SendMessage(chatMessage, myFile);
     Assert.IsFalse(success);
 }