public void CanUpdateChatHubConnectionTest()
        {
            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);

            chatDao.AddChatHubConnection(returned.Id, "connection1");
            chatDao.AddChatHubConnection(returnedMate.Id, "connection2");

            chatDao.UpdateChatHubConnection(returned.Id, "connection1Replaced");
            chatDao.UpdateChatHubConnection(returnedMate.Id, "connection2Replaced");

            ChatConnection connection1 = chatDao.GetChatConnectionFromUserId(returned.Id);
            ChatConnection connection2 = chatDao.GetChatConnectionFromUserId(returnedMate.Id);

            Assert.Equal("connection1Replaced", connection1.Connection);
            Assert.Equal(returned.Id, connection1.UserId);
            Assert.Equal("connection2Replaced", connection2.Connection);
            Assert.Equal(returnedMate.Id, connection2.UserId);

            _fixture.Dispose();
        }
Esempio n. 2
0
        public ActionResult <ChatConnection> getMyConnectionId()
        {
            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);
                ChatConnection connect = chatDao.GetChatConnectionFromUserId((int)userId);

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

                return(BadRequest(new ErrorMessageModel("Não existe connection ID!")));
            }
            catch (Exception e)
            {
                return(BadRequest(new ErrorMessageModel(e.Message)));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Override de OnConnectedAsync
        /// Este metodo e chamado quando e estabelecida uma conexão ao hub e
        /// adiciona a conexão à BD
        /// </summary>
        /// <returns>Retorna um callback UserConnected com o id de conexão</returns>
        public override async Task OnConnectedAsync()
        {
            try
            {
                await Clients.All.SendAsync("UserConnected", Context.ConnectionId);

                int?userId = ClaimHelper.GetIdFromClaimIdentity((ClaimsIdentity)Context.User.Identity);
                if (userId == null)
                {
                    throw new Exception("O utilizador não existe!");
                }

                ChatDAO chatDao = new ChatDAO(_connection);

                if (chatDao.GetChatConnectionFromUserId((int)userId) == null)
                {
                    chatDao.AddChatHubConnection((int)userId, Context.ConnectionId);
                }
                else
                {
                    chatDao.UpdateChatHubConnection((int)userId, Context.ConnectionId);
                }

                await base.OnConnectedAsync();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        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();
        }