public ActionResult <bool> CreateChat(int matchId, int publicationId, string publicationName) { int?userId = ClaimHelper.GetIdFromClaimIdentity((ClaimsIdentity)this.ControllerContext.HttpContext.User.Identity); if (userId == null) { return(Unauthorized(new ErrorExceptionModel("Utilizador não tem autorização!"))); } UserDAO userDAO = new UserDAO(_connection); User user = userDAO.FindById(matchId); if (userId < 0) { return(UnprocessableEntity(new ErrorExceptionModel("Utilizador não existe!"))); } Chat chat = new Chat(); chat.UserId = (int)userId; chat.PublicationId = publicationId; chat.PublicationName = publicationName; ChatDAO chatDAO = new ChatDAO(_connection); chat.ChatId = chatDAO.CreateChatId(); chatDAO.CreateChat(chat); chat.UserId = matchId; chatDAO.CreateChat(chat); return(Ok("Concluído!")); }
public ActionResult CreateChat(int matchId) { int?userId = ClaimHelper.GetIdFromClaimIdentity((ClaimsIdentity)this.ControllerContext.HttpContext.User.Identity); if (userId == null) { return(Unauthorized(new ErrorMessageModel("Utilizador não tem autorização ou não existe!"))); } try { ChatDAO chatDAO = new ChatDAO(_connection); if (chatDAO.getChatIdWithTargetUser((int)userId, matchId) == null) { Chat chat = new Chat(); chat.UserId = (int)userId; chat.ChatId = chatDAO.CreateChatId(); chatDAO.CreateChat(chat); chat.UserId = matchId; chatDAO.CreateChat(chat); return(Ok(new SuccessMessageModel("Chat criado!"))); } return(Ok(new SuccessMessageModel("O Chat já existe!"))); } catch (Exception e) { return(BadRequest(new ErrorMessageModel(e.Message))); } }
public void CanVerifyIfUserIsNotFromChatTest() { IEmployerDAO <Employer> EmployerDAO = new EmployerDAO(_connection); Employer testEmployer = new Employer(); testEmployer.FirstName = "Marcelo"; testEmployer.LastName = "Carvalho"; testEmployer.UserName = "******"; testEmployer.Password = "******"; testEmployer.Email = "*****@*****.**"; testEmployer.Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; testEmployer.Address = "Lixa"; Employer returned = EmployerDAO.Create(testEmployer); IMateDAO <Mate> MateDAO = new MateDAO(_connection); Mate testMate = new Mate(); testMate.FirstName = "Miguel"; testMate.LastName = "Dev"; testMate.UserName = "******"; testMate.Password = "******"; testMate.Email = "*****@*****.**"; testMate.Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; testMate.Address = "Figueiró"; testMate.Categories = new[] { Categories.CLEANING, Categories.PLUMBING }; testMate.Rank = Ranks.SUPER_MATE; testMate.Range = 20; Mate returnedMate = MateDAO.Create(testMate); ChatDAO chatDao = new ChatDAO(_connection); int chatId = chatDao.CreateChatId(); Chat chat = new Chat(); chat.UserId = returned.Id; chat.ChatId = chatId; chatDao.CreateChat(chat); chat.UserId = returnedMate.Id; chatDao.CreateChat(chat); bool user1IsFromChat = chatDao.IsUserFromChat(999999999, returned.Id); bool user2IsFromChat = chatDao.IsUserFromChat(999999999, returnedMate.Id); Assert.False(user1IsFromChat); Assert.False(user2IsFromChat); _fixture.Dispose(); }
public void ReturnExceptionCreatingChatWithUserId0Test() { IEmployerDAO <Employer> EmployerDAO = new EmployerDAO(_connection); Employer testEmployer = new Employer(); testEmployer.FirstName = "Marcelo"; testEmployer.LastName = "Carvalho"; testEmployer.UserName = "******"; testEmployer.Password = "******"; testEmployer.Email = "*****@*****.**"; testEmployer.Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; testEmployer.Address = "Lixa"; Employer returned = EmployerDAO.Create(testEmployer); IMateDAO <Mate> MateDAO = new MateDAO(_connection); Mate testMate = new Mate(); testMate.FirstName = "Miguel"; testMate.LastName = "Dev"; testMate.UserName = "******"; testMate.Password = "******"; testMate.Email = "*****@*****.**"; testMate.Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; testMate.Address = "Figueiró"; testMate.Categories = new[] { Categories.CLEANING, Categories.PLUMBING }; testMate.Rank = Ranks.SUPER_MATE; testMate.Range = 20; Mate returnedMate = MateDAO.Create(testMate); ChatDAO chatDao = new ChatDAO(_connection); int chatId = chatDao.CreateChatId(); Chat chat = new Chat(); chat.UserId = 0; chat.ChatId = chatId; Assert.Throws <Exception>(() => chatDao.CreateChat(chat)); _fixture.Dispose(); }
public void CanGetChatsTest() { IEmployerDAO <Employer> EmployerDAO = new EmployerDAO(_connection); Employer testEmployer = new Employer(); testEmployer.FirstName = "Marcelo"; testEmployer.LastName = "Carvalho"; testEmployer.UserName = "******"; testEmployer.Password = "******"; testEmployer.Email = "*****@*****.**"; testEmployer.Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; testEmployer.Address = "Lixa"; Employer returned = EmployerDAO.Create(testEmployer); IMateDAO <Mate> MateDAO = new MateDAO(_connection); Mate testMate = new Mate(); testMate.FirstName = "Miguel"; testMate.LastName = "Dev"; testMate.UserName = "******"; testMate.Password = "******"; testMate.Email = "*****@*****.**"; testMate.Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; testMate.Address = "Figueiró"; testMate.Categories = new[] { Categories.CLEANING, Categories.PLUMBING }; testMate.Rank = Ranks.SUPER_MATE; testMate.Range = 20; Mate returnedMate = MateDAO.Create(testMate); ChatDAO chatDao = new ChatDAO(_connection); int chatId = chatDao.CreateChatId(); int chatId2 = chatDao.CreateChatId(); Chat chat = new Chat(); chat.UserId = returned.Id; chat.ChatId = chatId; chatDao.CreateChat(chat); chat.UserId = returnedMate.Id; chatDao.CreateChat(chat); Chat chat2 = new Chat(); chat2.UserId = returned.Id; chat2.ChatId = chatId2; chatDao.CreateChat(chat2); chat2.UserId = returnedMate.Id; chatDao.CreateChat(chat2); Chat[] user1ChatArray = chatDao.GetChats(returned.Id); Chat[] user2ChatArray = chatDao.GetChats(returnedMate.Id); Assert.Equal(2, user1ChatArray.Length); Assert.Equal(2, user2ChatArray.Length); Assert.Equal(chatId, user1ChatArray[0].ChatId); Assert.Equal(returnedMate.Id, user1ChatArray[0].UserId); Assert.Equal(chatId, user2ChatArray[0].ChatId); Assert.Equal(returned.Id, user2ChatArray[0].UserId); Assert.Equal(chatId2, user1ChatArray[1].ChatId); Assert.Equal(returnedMate.Id, user1ChatArray[1].UserId); Assert.Equal(chatId2, user2ChatArray[1].ChatId); Assert.Equal(returned.Id, user2ChatArray[1].UserId); _fixture.Dispose(); }
public void CanAddMessageTest() { 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(connect1.Connection, connect2.Connection, MessageSend, DateTime.Now, returnedEmployer.Id); bool addedToDb2 = chatDAO.AddMessage(connect2.Connection, connect1.Connection, MessageSend2, DateTime.Now, returnMate.Id); 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(); }
public void CanGetChatsBy_UserId() { 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); Assert.True(chatArrayEmploye.Length > 0); Assert.True(chatArrayMate.Length > 0); _fixture.Dispose(); }