public List <Message> GetMessages(int id) { ChatDAO chatDAO = new ChatDAO(_connection); List <Message> messageList = chatDAO.GetMessageList(id); return(messageList); }
public ActionResult <List <Message> > GetMessages(int chatId) { int?loggedUser = ClaimHelper.GetIdFromClaimIdentity((ClaimsIdentity)this.ControllerContext.HttpContext.User.Identity); if (loggedUser == null) { return(Unauthorized("Utilizador não autorizado ou inexistente!")); } try { ChatDAO chatDAO = new ChatDAO(_connection); List <Message> messageList = chatDAO.GetMessageList(chatId, (int)loggedUser); return(Ok(messageList)); } catch (Exception e) { return(BadRequest(new ErrorMessageModel(e.Message))); } }
public void CanAddMessageOfflineUserTest() { IEmployerDAO <Employer> employerDAO = new EmployerDAO(_connection); Employer testEmployerC = new Employer(); testEmployerC.FirstName = "Ema"; testEmployerC.LastName = "Coelho"; testEmployerC.UserName = "******"; testEmployerC.Password = "******"; testEmployerC.Email = "*****@*****.**"; testEmployerC.Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; testEmployerC.Address = "Lousada"; // Employe a utilizar Employer returnedEmployer = employerDAO.Create(testEmployerC); IMateDAO <Mate> MateDAO = new MateDAO(_connection); Mate firstMate = new Mate(); firstMate.FirstName = "Marcelddo"; firstMate.LastName = "Carvalho"; firstMate.UserName = "******"; firstMate.Password = "******"; firstMate.Email = "*****@*****.**"; firstMate.Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; firstMate.Address = "Sra. Aparecida, Lousada"; firstMate.Categories = new[] { Categories.CLEANING, Categories.ELECTRICITY }; firstMate.Rank = Ranks.MATE; firstMate.Range = 50; Mate returnMate = MateDAO.Create(firstMate); ChatDAO chatDAO = new ChatDAO(_connection); chatDAO.AddChatHubConnection(returnedEmployer.Id, "connection1"); chatDAO.AddChatHubConnection(returnMate.Id, "connection2"); int chatId = chatDAO.CreateChatId(); Chat chat = new Chat(); chat.ChatId = chatId; chat.UserId = returnedEmployer.Id; chatDAO.CreateChat(chat); chat.UserId = returnMate.Id; chatDAO.CreateChat(chat); ChatConnection connect1 = chatDAO.GetChatConnectionFromUserId(returnedEmployer.Id); ChatConnection connect2 = chatDAO.GetChatConnectionFromUserId(returnMate.Id); String MessageSend = "message test"; String MessageSend2 = "message test 2"; bool addedToDb1 = chatDAO.AddMessage(null, connect2.Connection, MessageSend, DateTime.Now, returnedEmployer.Id); bool addedToDb2 = chatDAO.AddMessage(null, connect1.Connection, MessageSend2, DateTime.Now, returnMate.Id); List <Message> messages = chatDAO.GetMessageList(chatId, returnMate.Id); List <Message> messages2 = chatDAO.GetMessageList(chatId, returnedEmployer.Id); Assert.Equal(messages.Count, messages2.Count); Assert.Equal(2, messages.Count); Assert.Equal(2, messages2.Count); messages.ToArray(); messages2.ToArray(); Assert.Equal("message test", messages[0].MessageSend); Assert.Equal("message test", messages2[0].MessageSend); Assert.Equal("message test 2", messages[1].MessageSend); Assert.Equal("message test 2", messages2[1].MessageSend); Assert.True(addedToDb1); Assert.True(addedToDb2); _fixture.Dispose(); }
public void CanGetChatsFromDb() { UserDAO userDAO = new UserDAO(_connection); User testUser1 = new User(); testUser1.Email = "*****@*****.**"; testUser1.Password = "******"; testUser1.FirstName = "Ema"; testUser1.LastName = "Coelho"; testUser1.Password = "******"; testUser1.Image = "ImageLocation"; Address adr = new Address(); adr.PostalCode = "4615-423"; adr.Street = "Rua de Real"; adr.StreetNumber = 55; adr.District = "Porto"; adr.Country = "Portugal"; testUser1.Localization = adr.ToString(); //User 1 a utilizar User returnedUser = userDAO.Create(testUser1); User testUser2 = new User(); testUser2.Email = "*****@*****.**"; testUser2.Password = "******"; testUser2.FirstName = "Ema"; testUser2.LastName = "Coelho"; testUser2.Password = "******"; testUser2.Image = "ImageLocation"; Address adr2 = new Address(); adr2.PostalCode = "4615-423"; adr2.Street = "Rua de Real"; adr2.StreetNumber = 55; adr2.District = "Porto"; adr2.Country = "Portugal"; testUser2.Localization = adr2.ToString(); //User 2 a utilizar User returnedUser2 = userDAO.Create(testUser2); Chat chat = new Chat(); ChatDAO chatDAO = new ChatDAO(_connection); int chatId = chatDAO.CreateChatId(); chat.ChatId = chatId; chat.UserId = returnedUser.Id; chatDAO.CreateChat(chat); chat.UserId = returnedUser2.Id; chatDAO.CreateChat(chat); Chat[] chatArrayEmploye = chatDAO.GetChats(returnedUser.Id); Chat[] chatArrayMate = chatDAO.GetChats(returnedUser2.Id); Message message = new Message(); message.ChatId = chatId; message.MessageSend = "message test"; message.SenderId = returnedUser.Id; message.Time = DateTime.Now; bool addedToDb = chatDAO.AddMessage(message); List <Message> returnMessages = chatDAO.GetMessageList(chatId); Message returnedMessage = returnMessages.First(); Assert.Equal(message.ChatId, returnedMessage.ChatId); Assert.Equal(message.MessageSend, returnedMessage.MessageSend); Assert.Equal(message.SenderId, returnedMessage.SenderId); _fixture.Dispose(); }