Example #1
0
        public ActionResult <Message> getLastMessageFromChat(int id)
        {
            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);
                Message message = chatDao.GetLastMessageFromChat(id, (int)userId);

                if (message != null)
                {
                    return(Ok(message));
                }

                return(BadRequest(new ErrorMessageModel("Não existe connection ID!")));
            }
            catch (Exception e)
            {
                return(BadRequest(new ErrorMessageModel(e.Message)));
            }
        }
Example #2
0
        public void CanGetLastMessageFromChatTest()
        {
            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";

            chatDAO.AddMessage(connect1.Connection, connect2.Connection, MessageSend, DateTime.Now, returnedEmployer.Id);
            chatDAO.AddMessage(connect2.Connection, connect1.Connection, MessageSend2, DateTime.Now, returnMate.Id);

            Message message  = chatDAO.GetLastMessageFromChat(chatId, returnedEmployer.Id);
            Message message2 = chatDAO.GetLastMessageFromChat(chatId, returnMate.Id);

            Assert.Equal("message test 2", message.MessageSend);
            Assert.Equal("message test 2", message2.MessageSend);

            _fixture.Dispose();
        }