示例#1
0
        public async Task <IActionResult> GetUserThreads(int userId, [FromQuery] ThreadListParams listParams)
        {
            var sender = await _repo.GetUser(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var threadsPagedList = await _repo.GetThreadList(userId, listParams);

            if (threadsPagedList == null)
            {
                return(NoContent());
            }

            return(Ok(threadsPagedList));
        }
示例#2
0
        public async Task <PagedList <ThreadDto> > GetThreadList(int requestorId, ThreadListParams listParams)
        {
            var threads = await _context.Threads
                          .Where(x => x.ThreadParticipants.Where(y => y.ParticipantId == requestorId).FirstOrDefault() != null)
                          .OrderByDescending(x => x.LastModified)
                          .Include(x => x.ThreadParticipants)
                          .Skip((listParams.PageNumber - 1) * listParams.PageSize)
                          .Take(listParams.PageSize)
                          .ToListAsync();

            var threadDtos = new List <ThreadDto>();

            var threadsNumber = await _context.Threads
                                .Where(x => x.ThreadParticipants.Where(y => y.ParticipantId == requestorId).FirstOrDefault() != null)
                                .OrderByDescending(x => x.LastModified)
                                .CountAsync();

            if (threadsNumber == 0)
            {
                return(null);
            }

            foreach (Thread thread in threads)
            {
                var user = await _context.ThreadParticipants
                           .Where(x => x.ThreadId == thread.Id)
                           .Where(x => x.ParticipantId != requestorId)
                           .Include(x => x.Participant)
                           .FirstOrDefaultAsync();

                var message = await _context.Messages
                              .Where(x => x.ThreadId == thread.Id)
                              .OrderByDescending(x => x.MessageSent)
                              .FirstOrDefaultAsync();

                var lastMesageIsMine = true;
                if (message.SenderId != requestorId)
                {
                    lastMesageIsMine = false;
                }

                var isRead = true;
                if (!message.IsRead)
                {
                    isRead = false;
                }

                var threadDto = new ThreadDto()
                {
                    UserOneId         = requestorId,
                    UserTwoId         = user.ParticipantId,
                    UserTwoName       = user.Participant.Name,
                    UserTwoSurname    = user.Participant.Surname,
                    UserTwoPhotoUrl   = user.Participant.PhotoUrl,
                    LastModified      = thread.LastModified,
                    Content           = message.Content,
                    LastMessageIsMine = lastMesageIsMine,
                    IsRead            = isRead
                };

                threadDtos.Add(threadDto);
            }



            return(new PagedList <ThreadDto>(threadDtos, threadsNumber, listParams.PageNumber, listParams.PageSize));
        }