Ejemplo n.º 1
0
        public async void Returns_Status404NotFound_when_ThreadNotBelongsToUser()
        {
            IDatabaseContext    databaseContext     = DatabaseTestHelper.GetContext();
            MapperConfiguration mapperConfiguration = new MapperConfiguration(conf =>
            {
                conf.AddProfile(new MessageThread_to_ThreadOut());
                conf.AddProfile(new MessageReadState_to_MessageReadStateOut());
                conf.AddProfile(new Message_to_MessageOut());
                conf.AddProfile(new MessageThreadParticipant_to_MessageThreadParticipantOut());
            });

            IMapper         mapper         = mapperConfiguration.CreateMapper();
            IMessageService messageService = new MessageService(databaseContext, mapper);

            string adminId = (await databaseContext.Users
                              .FirstOrDefaultAsync(_ => _.UserName == ADMIN_USERNAME))?.Id;

            await DatabaseTestHelper.AddNewUser(USER_USERNAME, databaseContext);

            string userId = (await databaseContext.Users
                             .FirstOrDefaultAsync(_ => _.UserName == USER_USERNAME))?.Id;

            MessageReadState messageReadState = new MessageReadState
            {
                ReadDate = DateTime.Now,
                UserID   = adminId
            };

            Message messageFirst = new Message
            {
                SendingUserID = adminId,
                SentDate      = DateTime.Now,
                Content       = "First message's content"
            };

            Message messageLast = new Message
            {
                SendingUserID     = adminId,
                SentDate          = DateTime.Now,
                Content           = "Last message's content",
                MessageReadStates = new List <MessageReadState>()
                {
                    messageReadState
                }
            };

            MessageThread thread = new MessageThread
            {
                Messages = new List <Message>()
                {
                    messageFirst,
                    messageLast
                },
                MessageThreadParticipants = new List <MessageThreadParticipant>()
                {
                    new MessageThreadParticipant
                    {
                        UserID = Guid.NewGuid().ToString()
                    },
                    new MessageThreadParticipant
                    {
                        UserID = Guid.NewGuid().ToString()
                    }
                }
            };

            await databaseContext.MessageThreads
            .AddAsync(thread);

            await databaseContext.SaveChangesAsync();

            int           numberOfMessageToGetFromThread = 1;
            MessageFilter messageFilter = new MessageFilter
            {
                NumberOfLastMessage = numberOfMessageToGetFromThread
            };

            StatusCode <ThreadOut> status = await messageService.GetMessagesByThreadAndFilterAndUser(thread.ID, messageFilter, USER_USERNAME);

            Assert.NotNull(status);
            Assert.Equal(StatusCodes.Status404NotFound, status.Code);
        }
Ejemplo n.º 2
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));
        }