public IActionResult OnGetGetLastestChat()
        {
            try
            {
                string UserName    = User.Identity.Name;
                var    CurrentUser = _context.Users.Where(x => x.Email == UserName).FirstOrDefault();
                var    userId      = CurrentUser.Id;

                var query = from r in _context.Conversations
                            //  where (r.To == userId && r.Text != null) || r.From == userId)
                            //select r).ToList().Take(5);
                            where r.To == userId && r.Text != null || r.From == userId
                            orderby r.Date descending
                            group r by r.From into g
                            select g.OrderByDescending(x => x.Date).Take(5).FirstOrDefault();

                List <LatestConversationVM> messages = new List <LatestConversationVM>();

                foreach (var item in query)
                {
                    LatestConversationVM NM = new LatestConversationVM();
                    var user       = _context.Users.Find(item.From);
                    var fromUser   = _context.Users.Where(x => x.UserName == item.From).FirstOrDefault();
                    var fnameEmail = user.Email;
                    var fname      = fnameEmail.Split('@');
                    NM.From = item.From;
                    //NM.FromName = user.FirstName + " " + user.LastName;
                    NM.FromName = fname[0];
                    NM.Date     = item.Date;
                    if (item.Text == "")
                    {
                        NM.Text = "New Image";
                    }
                    else
                    {
                        NM.Text = item.Text;
                    }

                    NM.Image = "";// user.ProfilePic;
                    if (NM.Image == null || NM.Image.Length < 1)
                    {
                        NM.Image = "Profileplaceholder.png";
                    }
                    messages.Add(NM);
                }

                Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                //return Json(messages, JsonRequestBehavior.AllowGet);
                return(new JsonResult(messages));
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                // return Json(new { data = "Error" }, JsonRequestBehavior.AllowGet);
                return(new JsonResult("Error"));
            }
        }
示例#2
0
        public ActionResult GetLastestChat()
        {
            using (_context = new ApplicationDbContext())
            {
                try
                {
                    int userId = GetLoggedInUserId();

                    var query = from r in _context.Conversation
                                where r.To == userId && r.Text != null || r.From == userId
                                //orderby r.Date descending
                                group r by r.From into g
                                select g.OrderByDescending(x => x.Date).Take(5).FirstOrDefault();

                    List <LatestConversationVM> messages = new List <LatestConversationVM>();

                    foreach (var item in query)
                    {
                        LatestConversationVM NM = new LatestConversationVM();
                        var user = _context.Users.Find(item.From);
                        NM.From     = item.From;
                        NM.FromName = user.FullName;
                        NM.Date     = item.Date;
                        if (item.Text == "")
                        {
                            NM.Text = "New Image";
                        }
                        else
                        {
                            NM.Text = item.Text;
                        }

                        NM.Image = user.Photo;
                        if (NM.Image == null || NM.Image.Length < 1)
                        {
                            NM.Image = "Profileplaceholder.png";
                        }
                        messages.Add(NM);
                    }

                    Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                    return(Json(messages));
                }
                catch (Exception ex)
                {
                    Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                    return(Json(new { data = "Error" }));
                }
            }
        }
        public IActionResult OnGetSendMessage(string id)
        {
            //this.Users = _userManager.Users.Include(u => u.role).ToList();
            //var user = await _userManager.FindByEmailAsync("bill@gmail");
            //var roles = await _userManager.GetRolesAsync(user);

            var Coach = _context.Users.Where(x => x.Email == "*****@*****.**").FirstOrDefault();

            if (id == null)
            {
                id = Coach.Id;
            }

            // var userRoles = await UserManager.GetRolesAsync(User);
            string UserName    = User.Identity.Name;
            var    CurrentUser = _context.Users.Where(x => x.Email == UserName).FirstOrDefault();
            var    userId      = CurrentUser.Id;

            if (userId == null)
            {
                return(new JsonResult(null));
            }
            if (id != null)
            {
                if (userId == id)
                {
                    id = null;
                }
            }
            var UserId  = CurrentUser.Id;
            var UserPIC = _context.Users.Where(x => x.Id == UserId).FirstOrDefault();

            //if (UserPIC.ProfilePic != null && UserPIC.ProfilePic.Length > 1)
            //{

            //    TempData["ActiveUserpic"] = UserPIC.ProfilePic;
            //}
            //else
            //{
            //    TempData["ActiveUserpic"] = "Profileplaceholder.png";
            //}
            List <Conversations>        con      = null;
            List <LatestConversationVM> messages = new List <LatestConversationVM>();

            if (!string.IsNullOrEmpty(id))
            {
                var U = _context.Users.Where(x => x.Id == id).FirstOrDefault();
                con = _context.Conversations.Where(x => x.From == UserId || x.To == UserId).ToList();
                if (con == null)
                {
                    Conversations dcon = new Conversations();
                    dcon.To     = id;
                    dcon.From   = userId;
                    dcon.IsSeen = true;
                    dcon.Date   = DateTime.Now;
                    dcon.Text   = "Now You Can Start Chat With Me";

                    _context.Conversations.Add(dcon);
                    _context.SaveChanges();
                }
                else
                {
                    foreach (var item in con)
                    {
                        LatestConversationVM NM = new LatestConversationVM();
                        var user       = _context.Users.Find(item.From);
                        var fromUser   = _context.Users.Where(x => x.UserName == item.From).FirstOrDefault();
                        var fnameEmail = user.Email;
                        var fname      = fnameEmail.Split('@');
                        NM.From = item.From;
                        //NM.FromName = user.FirstName + " " + user.LastName;
                        NM.FromName = fname[0];
                        NM.Date     = item.Date;
                        if (item.Text == "")
                        {
                            NM.Text = "New Image";
                        }
                        else
                        {
                            NM.Text = item.Text;
                        }

                        NM.Image = "Profileplaceholder.png";// user.ProfilePic;

                        if (NM.Image == null || NM.Image.Length < 1)
                        {
                            NM.Image = "Profileplaceholder.png";
                        }
                        messages.Add(NM);
                    }
                }

                //if (U.ProfilePic != null)
                //{
                //    if (U.ProfilePic.Length < 1)
                //    {
                //        ViewBag.ActiveUserpic = U.ProfilePic;
                //    }
                //}
                //if (U.ProfilePic == null)
                //{
                //    ViewBag.ActiveUserpic = "Profileplaceholder.png";
                //}

                //var UserId = User.Identity.GetUserId();
                //var UserPIC = db.AspNetUsers.Where(x => x.Id == UserId).FirstOrDefault();
                //if (UserPIC.ProfilePic == null || UserPIC.ProfilePic.Length<1)
                //{
                //    Session["UserDP"] = "Profileplaceholder.png";
                //}
                //else
                //{
                //    Session["UserDP"] = UserPIC.ProfilePic;
                //}
                ViewData["ActiveUserId"]   = id;
                ViewData["ActiveUserName"] = U.Email;
            }
            ViewData["UserId"]          = userId;
            ViewData["CurrentUserName"] = User.Identity.Name;
            //return JsonResult(
            //new { status = "success", data = conversations },
            //JsonRequestBehavior.AllowGet);
            return(new JsonResult(messages));
        }