コード例 #1
0
        public async Task <IActionResult> GetDisplayUser(Guid userId, Guid friendId)
        {
            var user = await _context.Users.FindAsync(friendId);

            if (user == null)
            {
                return(NotFound());
            }
            UserDisplay userResponse = new UserDisplay(user, _storageService);

            var messages = await _context.HaveMessages
                           .Where(x => (x.SenderId == userId && x.ReceiverId == friendId) ||
                                  (x.SenderId == friendId && x.ReceiverId == friendId))
                           .OrderByDescending(x => x.SentAt)
                           //.Take(20)
                           .ToListAsync();

            List <MessageResponse> messageResponses = new List <MessageResponse>();

            foreach (var item in messages)
            {
                MessageResponse res = new MessageResponse(item);
                if (item.SenderId == userId)
                {
                    res.Type = "sent";
                }
                else
                {
                    res.Type = "received";
                }
                messageResponses.Add(res);
            }
            var friendResponse = new FriendResponse()
            {
                User     = userResponse,
                Messages = messageResponses
            };

            return(Ok(friendResponse));
        }
コード例 #2
0
        public async Task <IActionResult> GetFriendList(Guid userId, [FromQuery] PagingRequest request)
        {
            request.PageSize = 20;
            var messages       = new List <MessageResponse>();
            var tempMessages   = new List <MessageResponse>();
            var userList       = new List <Guid>();
            var recentMessages = await _context.HaveMessages
                                 .Where(x => x.SenderId == userId || x.ReceiverId == userId)
                                 .OrderByDescending(x => x.SentAt)
                                 .ToListAsync();

            foreach (var item in recentMessages)
            {
                var m = new MessageResponse(item);
                if (item.SenderId == userId)
                {
                    m.Type = "sent";
                }
                else
                {
                    m.Type = "received";
                }
                tempMessages.Add(m);
            }

            foreach (MessageResponse message in tempMessages)
            {
                if (message.Type == "sent")
                {
                    if (!messages.Any(x => x.ReceiverId == message.ReceiverId || x.ReceiverId == message.SenderId))
                    {
                        messages.Add(message);
                    }
                }
                else
                {
                    if (!messages.Any(x => x.SenderId == message.ReceiverId || x.SenderId == message.SenderId))
                    {
                        messages.Add(message);
                    }
                }
            }

            messages = messages
                       .Skip((request.PageIndex - 1) * request.PageSize)
                       .Take(request.PageSize).ToList();
            var friendList = new List <FriendResponse>();

            foreach (MessageResponse message in messages)
            {
                AppUser user = new AppUser();
                if (message.SenderId == userId)
                {
                    user = await _context.Users.FindAsync(message.ReceiverId);
                }
                else
                {
                    user = await _context.Users.FindAsync(message.SenderId);
                }

                var userDisplay      = new UserDisplay(user, _storageService);
                var messageResponses = new List <MessageResponse>();
                messageResponses.Add(message);

                var friend = new FriendResponse()
                {
                    Messages = messageResponses,
                    User     = userDisplay
                };

                friendList.Add(friend);
            }

            return(Ok(friendList));
        }