public IHttpActionResult CheckExistConversation(string conName)
 {
     try
     {
         var loginUserId  = User.Identity.GetUserId();
         var conversation = _context.Conversations.FirstOrDefault(c => c.Name == conName);
         if (conversation != null)
         {
             var model = ConversationModel.Create(_context, conversation.Id);
             return(Json(model));
         }
         var newConName      = String.Join("_", conName.Split('_').Reverse());
         var newConversation = _context.Conversations.FirstOrDefault(c => c.Name == newConName);
         if (newConversation != null)
         {
             var model = ConversationModel.Create(_context, newConversation.Id);
             return(Json(model));
         }
         return(Json(false));
     }
     catch (Exception ex)
     {
         return(Json("error"));
     }
 }
        public static ConversationModel Create(ApplicationDbContext _context, string conversationId)
        {
            var model = new ConversationModel();

            model.ConversationId = conversationId;
            model.Messages       = _context.Messages.Where(m => m.ConversationId == conversationId).OrderByDescending(m => m.SendTime).Take(100).ToList().Select(m =>
            {
                var message          = new MessageModel();
                message.Id           = m.Id;
                message.Content      = m.Content;
                message.SentUserId   = m.SentUserId;
                message.SentUserName = _context.Users.FirstOrDefault(u => u.Id == m.SentUserId)?.UserName;
                return(message);
            }).Reverse().ToList();
            return(model);
        }
 public IHttpActionResult GetOldMessagesByConversationId(string conId)
 {
     try
     {
         var loginUserId  = User.Identity.GetUserId();
         var conversation = _context.Conversations.FirstOrDefault(c => c.Id == conId);
         if (conversation != null)
         {
             var model = new ConversationModel();
             model = ConversationModel.Create(_context, conversation.Id);
             return(Json(model));
         }
         return(Json(false));
     }
     catch (Exception ex)
     {
         return(Json("error"));
     }
 }