Exemple #1
0
        public async Task <List <ChatRoomResult> > GetAllRooms(Guid profileId, string name)
        {
            var listRooms = await _chatRoomListRepository.GetAllAsync();

            var usersFull = await _userRepository.GetUsersFull();

            var me    = usersFull.FirstOrDefault(e => e.ProfileId == profileId);
            var users = usersFull
                        .Where(e => e.Account.ToUpper().Contains(name.ToUpper()) && e.ProfileId != profileId)
                        .ToList();
            var messages = await _chatMessageRepository.GetAllAsync();

            List <ChatRoomResult> chats = new List <ChatRoomResult>();

            foreach (var temp in users)
            {
                var tempRoom = listRooms.Where(e => e.UserId == temp.Id).ToList();
                if (tempRoom.Count > 0)
                {
                    var roomExist = listRooms
                                    .FirstOrDefault(e => e.UserId == me.Id && tempRoom
                                                    .FirstOrDefault(a => a.ChatRoomId == e.ChatRoomId) != null);
                    if (roomExist != null)
                    {
                        var tempRoomForUser = tempRoom
                                              .FirstOrDefault(a => a.ChatRoomId == roomExist.ChatRoomId);
                        if (tempRoomForUser != null)
                        {
                            var lastMessage = messages
                                              .OrderByDescending(e => e.DateTime)
                                              .FirstOrDefault(e => e.ChatRoomId == tempRoomForUser.ChatRoomId);
                            var tempUser = usersFull
                                           .FirstOrDefault(e => e.Id.CompareTo(tempRoomForUser.UserId) == 0);

                            var tempRoomDto = DomainEntityConverter.ConvertToResult(
                                user: tempUser,
                                chatRoomList: tempRoomForUser,
                                chatMessage: lastMessage
                                );

                            chats.Add(tempRoomDto);
                        }
                    }
                    else
                    {
                        var tempRoomDto = DomainEntityConverter.ConvertToResult(
                            user: temp,
                            chatRoomList: null,
                            chatMessage: null
                            );
                        chats.Add(tempRoomDto);
                    }
                }
                else
                {
                    var tempRoomDto = DomainEntityConverter.ConvertToResult(
                        user: temp,
                        chatRoomList: null,
                        chatMessage: null
                        );
                    chats.Add(tempRoomDto);
                }
            }
            return(chats);
        }