Example #1
0
        public JsonResult getUsersITalkedTo()
        {
            ApplicationDbContext db = new ApplicationDbContext();

            var manager     = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()));
            var currentUser = manager.FindById(User.Identity.GetUserId());

            if (currentUser == null)
            {
                return(Json(""));
            }

            HashSet <UsersChatListViewModel> users = new HashSet <UsersChatListViewModel>();

            foreach (Message m in db.messages)
            {
                UsersChatListViewModel tmp = new UsersChatListViewModel();

                if (m.fromUserId == currentUser.Id || m.toUserId == currentUser.Id)
                {
                    if (m.fromUserId != currentUser.Id)
                    {
                        tmp.userId = m.fromUserId;
                    }
                    else if (m.toUserId != currentUser.Id)
                    {
                        tmp.userId = m.toUserId;
                    }



                    using (ApplicationDbContext dbList = new ApplicationDbContext())
                    {
                        var userN = dbList.Users.ToList().Find(usr => usr.Id == tmp.userId);
                        if (userN != null)
                        {
                            tmp.userName = userN.user;
                        }

                        tmp.unseenMsgCount = dbList.messages.Where(msg => msg.seen == false && tmp.userId == msg.toUserId && msg.fromUserId == currentUser.Id).Count();
                    }


                    tmp.msgNumber = "0";
                    tmp.unreadMsg = "non";

                    users.Add(tmp);
                }
            }

            return(Json(users));
        }
        public JsonResult getUsersITalkedTo()
        {
            ApplicationDbContext db = new ApplicationDbContext();

            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var currentUser = manager.FindById(User.Identity.GetUserId());

            if (currentUser == null)
                return Json("");

            HashSet<UsersChatListViewModel> users = new HashSet<UsersChatListViewModel>();

            foreach (Message m in db.messages)
            {
                UsersChatListViewModel tmp = new UsersChatListViewModel();

                if(m.fromUserId == currentUser.Id || m.toUserId == currentUser.Id)
                {
                    if (m.fromUserId != currentUser.Id)
                        tmp.userId = m.fromUserId;
                    else if (m.toUserId != currentUser.Id)
                        tmp.userId = m.toUserId;

                    using (ApplicationDbContext dbList = new ApplicationDbContext())
                    {
                        var userN = dbList.Users.ToList().Find(usr => usr.Id == tmp.userId);
                        if(userN != null)
                            tmp.userName = userN.user;

                        tmp.unseenMsgCount = dbList.messages.Where(msg => msg.seen == false && tmp.userId == msg.toUserId && msg.fromUserId == currentUser.Id).Count();
                    }

                    tmp.msgNumber = "0";
                    tmp.unreadMsg = "non";

                    users.Add(tmp);
                }
            }

            return Json(users);
        }