コード例 #1
0
        public static ChatViewModel Get(IRepository <ChatRoom> ChatRepository, IRepository <Message> MessageRepository, Guid id, string userids, UserManager <AppUser> usermanager)
        {
            var chatroom = ChatRepository.GetAll().Where(x => x.UserIDs == userids).FirstOrDefault();

            var model = new ChatViewModel
            {
                Chats = ChatRepository.GetAll()
                        .OrderBy(u => u.ID).Where(x => x.SenderID == id || x.ReceiverID == id)
                        .Select(u => new ListItem
                {
                    ID             = u.ID,
                    UserIDs        = u.UserIDs,
                    SenderID       = u.SenderID,
                    ReceiverID     = u.ReceiverID,
                    SenderStatus   = u.SenderStatus,
                    ReceiverStatus = u.ReceiverStatus,
                    Sender         = u.SenderID != null ? usermanager.FindByIdAsync(u.SenderID.ToString()).Result.UserName: "******",
                    Receiver       = u.ReceiverID != null ? usermanager.FindByIdAsync(u.ReceiverID.ToString()).Result.UserName:"******",
                    UnReaded       = MessageRepository.GetAll().Where(x => x.ChatRoomID == u.ID && x.Status == true).Count()
                }).ToList(),
                Messages = chatroom != null?MessageRepository.GetAll()
                           .OrderBy(m => m.ID).Where(x => x.ChatRoomID == chatroom.ID)
                           .Select(m => new CusMessage
                {
                    ID           = m.ID,
                    ChatRoomID   = m.ChatRoomID,
                    Mes          = m.Mes,
                    SenderID     = m.SenderID,
                    SenderName   = usermanager.FindByIdAsync(m.SenderID).Result.UserName,
                    Status       = m.Status,
                    CreatedDate  = m.CreatedDate,
                    ModifiedDate = m.ModifiedDate
                }).ToList() : new List <CusMessage>(),
                               Chatroom = chatroom != null ? new ListItem()
                {
                    ID         = chatroom.ID,
                    UserIDs    = chatroom.UserIDs,
                    SenderID   = chatroom.SenderID,
                    ReceiverID = chatroom.ReceiverID,
                    Sender     = usermanager.FindByIdAsync(chatroom.SenderID.ToString()).Result.UserName,
                    Receiver   = usermanager.FindByIdAsync(chatroom.ReceiverID.ToString()).Result.UserName
                } : new ListItem(),
                Message = new Message()
            };

            return(model);
        }
コード例 #2
0
        public static ChatViewModel GetList(IRepository <ChatRoom> ChatRepository, IRepository <Message> MessageRepository, Guid id, UserManager <AppUser> usermanager)
        {
            var model = new ChatViewModel
            {
                Chats = ChatRepository.GetAll()
                        .OrderBy(u => u.ID).Where(x => x.SenderID == id || x.ReceiverID == id)
                        .Select(u => new ListItem
                {
                    ID         = u.ID,
                    UserIDs    = u.UserIDs,
                    SenderID   = u.SenderID,
                    ReceiverID = u.ReceiverID,
                    Sender     = usermanager.FindByIdAsync(u.SenderID.ToString()).Result.UserName,
                    Receiver   = usermanager.FindByIdAsync(u.ReceiverID.ToString()).Result.UserName,
                    UnReaded   = MessageRepository.GetAll().Where(x => x.ChatRoomID == u.ID && x.Status == true).Count()
                }).ToList(),
            };

            return(model);
        }