Exemplo n.º 1
0
        public async void Returns_ThreadOutWithMessages_when_ThreadExistsAndContainsMessages()
        {
            ThreadOut threadOut = new ThreadOut
            {
                ID       = Guid.NewGuid(),
                Messages = new List <MessageOut>()
                {
                    new MessageOut
                    {
                        ID       = Guid.NewGuid(),
                        SentDate = DateTime.Now
                    },
                    new MessageOut
                    {
                        ID       = Guid.NewGuid(),
                        SentDate = DateTime.Now
                    }
                }
            };

            StatusCode <ThreadOut> status         = new StatusCode <ThreadOut>(StatusCodes.Status200OK, threadOut);
            Mock <IMessageService> messageService = new Mock <IMessageService>();

            messageService.Setup(_ => _.GetMessagesByThreadAndFilterAndUser(It.IsAny <Guid>(), It.IsAny <MessageFilter>(), It.IsAny <string>()))
            .Returns(Task.FromResult(status));

            MessagesController messagesController = new MessagesController(messageService.Object, UserManagerHelper.GetUserManager(ADMIN_USERNAME));

            messagesController.Authenticate(ADMIN_USERNAME);

            MessageFilter messageFilter = new MessageFilter
            {
                NumberOfLastMessage = 5
            };

            IActionResult result = await messagesController.GetMessagesFromThread(Guid.NewGuid(), messageFilter);

            Assert.NotNull(result);
            OkObjectResult okObjectResult = result as OkObjectResult;

            Assert.NotNull(okObjectResult);
            Assert.NotNull(okObjectResult.Value);
            Assert.IsType <ThreadOut>(okObjectResult.Value);
            Assert.Equal(2, (okObjectResult.Value as ThreadOut).Messages.Count);
        }
        public void Test02()
        {
            count = 5;

            for (int i = 0; i < count; ++i)
            {
                ThreadOut to     = new ThreadOut(i * 10, 10);
                Thread    thread = new Thread(new ThreadStart(to.Out));
                thread.Start();
            }

            while (count != 0)
            {
                Thread.Sleep(10);
            }

            Console.WriteLine("End");
        }
Exemplo n.º 3
0
        public async Task <StatusCode <ThreadOut> > GetMessagesByThreadAndFilterAndUser(Guid threadId, MessageFilter messageFilter, string username)
        {
            string userId = (await _databaseContext.Users
                             .FirstOrDefaultAsync(_ => _.UserName == username))?.Id;

            MessageThread messageThread = await _databaseContext.MessageThreads
                                          .FirstOrDefaultAsync(_ => _.ID == threadId);

            if (messageThread == null)
            {
                return(new StatusCode <ThreadOut>(StatusCodes.Status404NotFound, $"Thread {threadId} not found"));
            }

            List <MessageThreadParticipant> messageThreadParticipants = await _databaseContext.MessageThreadParticipants
                                                                        .Include(_ => _.User)
                                                                        .Where(_ => _.ThreadID == threadId)
                                                                        .ToListAsync();

            if (messageThreadParticipants == null)
            {
                return(new StatusCode <ThreadOut>(StatusCodes.Status404NotFound, $"Thread participants not found"));
            }

            if (!messageThreadParticipants.Any(_ => _.UserID == userId))
            {
                return(new StatusCode <ThreadOut>(StatusCodes.Status404NotFound, $"Thread {threadId} not found"));
            }

            List <Message> messages = await _databaseContext.Messages
                                      .Include(_ => _.SendingUser)
                                      .Where(_ => _.ThreadID == threadId)
                                      .ToListAsync();

            if (messages == null)
            {
                return(new StatusCode <ThreadOut>(StatusCodes.Status404NotFound, $"Thread messages not found"));
            }

            for (int i = 0; i < messages.Count; ++i)
            {
                messages[i].MessageReadStates = await _databaseContext.MessageReadStates
                                                .Where(_ => _.MessageID == messages[i].ID)
                                                .ToListAsync();

                if (!messages[i].MessageReadStates.Any(_ => _.UserID == userId))
                {
                    MessageReadState messageReadState = new MessageReadState
                    {
                        MessageID = messages[i].ID,
                        ReadDate  = DateTime.Now,
                        UserID    = userId
                    };

                    _databaseContext.MessageReadStates
                    .Add(messageReadState);

                    messages[i].MessageReadStates.Add(messageReadState);

                    await _databaseContext.SaveChangesAsync();
                }
            }

            /*
             * int numberOfUnreadMessages = messageThread.Messages
             *  .Where(_ => !_.MessageReadStates.Any(_ => _.UserID == userId))
             *  .Count();
             */

            if (messageFilter.NumberOfLastMessage < 1)
            {
                messageFilter.NumberOfLastMessage = 1;
            }

            messageThread.Messages = messageThread.Messages
                                     .OrderBy(_ => _.SentDate)
                                     .TakeLast(messageFilter.NumberOfLastMessage)
                                     .ToList();

            ThreadOut result = _mapper.Map <ThreadOut>(messageThread);

            return(new StatusCode <ThreadOut>(StatusCodes.Status200OK, result));
        }