コード例 #1
0
        public void testGetMessagesByUserID()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Get_messageThread_by_userID_to_database")
                          .Options;

            var mockSender   = new Mock <ApplicationUser>();
            var mockReciever = new Mock <ApplicationUser>();

            string mockSenderID   = "ab3+10ds2";
            string mockRecieverID = "rg9_x1t\a";

            mockSender.Setup(sender => sender.Id).Returns(mockSenderID);
            mockReciever.Setup(reciever => reciever.Id).Returns(mockRecieverID);

            using (ApplicationDbContext context = new ApplicationDbContext(options))
            {
                IMessageThreadRepository messageThreadRepo = new MessageThreadRepository(context);
                MessageThread            messageThread     = new MessageThread(mockSender.Object.Id, mockReciever.Object.Id);
                messageThreadRepo.AddMessageThread(messageThread);
                messageThread = new MessageThread(mockSender.Object.Id, mockReciever.Object.Id);
                messageThreadRepo.AddMessageThread(messageThread);
                messageThread = new MessageThread(mockSender.Object.Id, mockReciever.Object.Id);
                messageThreadRepo.AddMessageThread(messageThread);
                messageThreadRepo.Save();
                Assert.True(context.MessageThread.Count() == 3);
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options))
            {
                IMessageThreadRepository messageThreadRepo = new MessageThreadRepository(context);
                var messageThreadsRecieved = messageThreadRepo.GetAllMessages(mockSender.Object.Id);
                Assert.True(messageThreadsRecieved.Count() == 3);
            }

            string mockDifferentSenderID   = "eb1-15es2";
            string mockDifferentRecieverID = "va9_xxc\a";

            using (var context = new ApplicationDbContext(options))
            {
                IMessageThreadRepository messageThreadRepo = new MessageThreadRepository(context);
                MessageThread            messageThread     = new MessageThread(mockDifferentSenderID, mockDifferentRecieverID);
                messageThreadRepo.AddMessageThread(messageThread);
                messageThreadRepo.Save();
                var messageThreadsRecieved = messageThreadRepo.GetAllMessages(mockSender.Object.Id);
                Assert.True(messageThreadsRecieved.Count() == 3);
            }
        }
コード例 #2
0
        public void testDeleteMessageThread()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Delete_messageThread_to_database")
                          .Options;

            var mockSender   = new Mock <ApplicationUser>();
            var mockReciever = new Mock <ApplicationUser>();

            string mockSenderID   = "ab3+10ds2";
            string mockRecieverID = "rg9_x1t\a";

            mockSender.Setup(sender => sender.Id).Returns(mockSenderID);
            mockReciever.Setup(reciever => reciever.Id).Returns(mockRecieverID);

            MessageThread messageThread = new MessageThread(mockSender.Object.Id, mockReciever.Object.Id);

            using (ApplicationDbContext context = new ApplicationDbContext(options))
            {
                IMessageThreadRepository messageThreadRepo = new MessageThreadRepository(context);
                messageThreadRepo.AddMessageThread(messageThread);
                messageThreadRepo.Save();
                Assert.True(context.MessageThread.Count() == 1);
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options))
            {
                IMessageThreadRepository messageThreadRepo = new MessageThreadRepository(context);
                messageThreadRepo.DeleteMessageThread(messageThread.id);
                messageThreadRepo.Save();
                Assert.True(context.MessageThread.Count() == 0);
            }
        }
コード例 #3
0
        public IActionResult SendMessageToUser([Bind("AssetId, Subject, Body, SenderId, RecieverId")] MessageThreadDTO messageThreadDTO)
        {
            messageThreadDTO.RecieverId = HttpContext.Request.Cookies["RecieverId"];
            var adminUser = _context.User.Single(u => u.Email == User.Identity.Name);

            messageThreadDTO.SenderId = adminUser.Id;
            messageThreadDTO.AssetId  = 0;

            MessageRepository       messageRepo       = new MessageRepository(_context);
            MessageThreadRepository messageThreadRepo = new MessageThreadRepository(_context);

            // The reciever will always be the seller
            Message      message;
            Notification notification;

            message = new Message(messageThreadDTO.Subject, messageThreadDTO.Body, false, false);
            string messageThreadId;
            string recieverEmail;
            string redirectURL;

            try
            {
                var messageThreadRetrieved = _context.MessageThread.Single(m => m.SenderId == messageThreadDTO.SenderId && m.RecieverId == messageThreadDTO.RecieverId);
                message.messageThread = messageThreadRetrieved;
                message.messageThread.LastUpdateDate = DateTime.Now;
                messageRepo.AddMessage(message);
                messageThreadId = message.messageThread.id.ToString();

                recieverEmail = message.messageThread.RecieverEmail;
                message.messageThread.RecieverUnreadMessageCount += 1;
                message.RecieverUnread = true;
            }
            catch (InvalidOperationException)
            {
                messageRepo.AddMessage(message);
                MessageThread messageThread = new MessageThread(messageThreadDTO.SenderId, messageThreadDTO.RecieverId);
                messageThread.SenderEmail   = _context.User.Single(u => u.Id == messageThreadDTO.SenderId).Email;
                messageThread.RecieverEmail = _context.User.Single(u => u.Id == messageThreadDTO.RecieverId).Email;

                messageThreadId = messageThread.id.ToString();
                messageThread.messages.Add(message);

                recieverEmail = messageThread.RecieverEmail;
                messageThread.RecieverUnreadMessageCount += 1;
                message.RecieverUnread = true;

                messageThread.LastUpdateDate = DateTime.Now;
                messageThreadRepo.AddMessageThread(messageThread);
            }

            // This notification redirect URL should put the user to the discussion
            redirectURL  = "/MessageThreads/Details/" + message.messageThread.id.ToString();
            notification = new Notification(message.Subject, message.Body, redirectURL);
            Clients.Group(recieverEmail).AddNotificationToQueue(notification);
            _context.SaveChanges();

            Clients.Group(recieverEmail).UpdateOverallNotificationCount();

            return(RedirectToAction(nameof(AdminController.Index)));
        }