public void GetConversations(int page) { var principal = JwtManager.GetPrincipal(Context.QueryString.Get("Bearer")); string thisUsername = principal.Identity.Name; var thisUser = UnitOfWork.UserRepository.GetByName(thisUsername); var messages = UnitOfWork.MessageRepository .GetLastMessages(thisUser.Id, ConversationsPerPage, page); messages.ForEach(m => UnitOfWork.UserRepository .GetById(thisUser.Id != m.SenderId ? m.SenderId : m.ReceiverId)); var conversations = messages.Select(m => { //get other user var user = UnitOfWork.UserRepository .GetById(thisUser.Id != m.SenderId ? m.SenderId : m.ReceiverId); var newMessagesCount = UnitOfWork.MessageRepository .GetNewMessagesCount(user.Id, thisUser.Id); bool userIsOnline = OnlineWorker.IsOnline(user); ConversationReturnModel conversation = Mapper .Map <Message, ConversationReturnModel>(m, opt => { opt.Items["User"] = user; opt.Items["NewMessagesCount"] = newMessagesCount; opt.Items["Online"] = userIsOnline; }); return(conversation); }); var pageCount = UnitOfWork.MessageRepository. GetLastMessagesPageCount(thisUser.Id, MessagesPerPage); Clients.Caller.OnGetConversations(conversations, pageCount); }
public IActionResult Converstation() { var myUserId = Convert.ToInt32(userManager.GetUserId(User)); var conversationReturn = new List <ConversationReturnModel>(); var converstationsMySenders = unitOfWork.GetRepository <Converstation>().Get(s => s.SenderId == myUserId, null, "Recevier").ToList(); foreach (var item in converstationsMySenders) { var itemReturn = new ConversationReturnModel() { ReceiverId = item.Recevier.Id, Message = item.LastMessage, ReceiverName = item.Recevier.FullName, LastDate = item.LastDate, }; conversationReturn.Add(itemReturn); } var converstationsOrtherSenders = unitOfWork.GetRepository <Converstation>().Get(s => s.RecevierId == myUserId, null, "Sender").ToList(); foreach (var item in converstationsOrtherSenders) { var itemReturn = new ConversationReturnModel() { ReceiverId = item.Sender.Id, Message = item.LastMessage, ReceiverName = item.Sender.FullName, LastDate = item.LastDate, }; conversationReturn.Add(itemReturn); } //foreach(var item in converstationsMySenders) //{ // foreach(var item1 in converstationsOrtherSenders) // { // if(item.SenderId == item1.RecevierId && item.RecevierId == item1.SenderId) // { // var itemRemove = conversationReturn.Find(s => s.ReceiverId == item.RecevierId); // conversationReturn.Remove(itemRemove); // } // } //} var returnFinal = conversationReturn.OrderByDescending(s => s.LastDate); return(ApiResponder.RespondSuccessTo(HttpStatusCode.Ok, returnFinal)); }