public static void Map(LMYFrameWorkMVCEntities dbContext, string fromUserId, LMYFrameWorkMVC.Common.DAL.Message src, MessageModel dest) { if (src == null || dest == null) { return; } dest.CopyPropertyValues(src); UserMapper.Map(dbContext, src.AspNetUser, dest.FromUserModel); UserMapper.Map(dbContext, src.AspNetUser1, dest.ToUserModel); //dest.ToUsersListItems = dbContext.AspNetUsers.Select(x => new UserModel { Id = x.Id, EnglishName = x.EnglishName, ArabicName = x.ArabicName,ro }).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id, Selected = dest.ToUsersIDs.Any(y => y == x.Id) }).ToList(); dest.IsFromMe = fromUserId == dest.FromUserID; dest.NotFromMeAndNotRead = !dest.IsFromMe && !dest.Read; }
public static void Map(LMYFrameWorkMVCEntities dbContext, MessageModel src, LMYFrameWorkMVC.Common.DAL.Message dest) { if (src == null || dest == null) { return; } dest.CopyPropertyValues(src, new List <string>() { src.nameof(x => x.CreatedAt) }); //the below code is important for audittrail and other matters such as get message after add it directly so the navigation properties be available dest.AspNetUser = dbContext.AspNetUsers.Where(x => x.Id == src.FromUserID).FirstOrDefault(); dest.AspNetUser1 = dbContext.AspNetUsers.Where(x => x.Id == src.ToUserID).FirstOrDefault(); }
public void Create(MessageModel messageModel) { try { if (Validate(messageModel)) { return; } // Action action = (() => // { using (var transaction = dbContext.Database.BeginTransaction()) { try { LMYFrameWorkMVC.Common.DAL.Message message = new LMYFrameWorkMVC.Common.DAL.Message(); messageModel.ID = Guid.NewGuid().ToString(); MessageMapper.Map(dbContext, messageModel, message); dbContext.Messages.Add(message); base.SaveChanges(); } catch (Exception ex) { this.HandleError(null, CommonLayer.LookUps.ErrorType.Exception, ex); } transaction.Commit(); } messageModel.AddSuccess(Resources.MessageSent, LookUps.SuccessType.Full); // }); //base.AddWaitingAction(action); } catch (Exception ex) { base.HandleError(messageModel, CommonLayer.LookUps.ErrorType.Exception, ex); base.UndoUpdates(); } }
public void GetMessageModel(MessageModel messageModel, string MessageViewRequestUserID) { try { //the below include is because a bug in EF6 LMYFrameWorkMVC.Common.DAL.Message message = dbContext.Messages.Where(x => x.ID == messageModel.ID).FirstOrDefault(); if (message == null) { base.HandleError(messageModel, CommonLayer.LookUps.ErrorType.Critical, null, Resources.NotFound); } else { MessageMapper.Map(dbContext, MessageViewRequestUserID, message, messageModel); } } catch (Exception ex) { base.HandleError(messageModel, CommonLayer.LookUps.ErrorType.Critical, ex); } }
public GenericListModel <UserModel> ListUsersWithLastMessageByCurrentUser() { GenericListModel <UserModel> baseListModel = new GenericListModel <UserModel>(); try { //all users that have conversations with current user IQueryable <AspNetUser> aspNetUsers = dbContext.AspNetUsers.Where(x => x.Id != contextInfo.UserID && ( x.Messages.Any(c => c.ToUserID == contextInfo.UserID || c.FromUserID == contextInfo.UserID) || x.Messages1.Any(c => c.ToUserID == contextInfo.UserID || c.FromUserID == contextInfo.UserID) ) ); UserMapper.Map(dbContext, aspNetUsers.ToList(), baseListModel.List); foreach (UserModel userModel in baseListModel.List) { //get last message between two users LMYFrameWorkMVC.Common.DAL.Message message = dbContext.Messages.Where(x => (x.FromUserID == contextInfo.UserID || x.ToUserID == contextInfo.UserID) && (x.FromUserID == userModel.Id || x.ToUserID == userModel.Id) ).OrderByDescending(x => x.CreatedAt).FirstOrDefault(); MessageMapper.Map(dbContext, contextInfo.UserID, message, userModel.LastMessageModel); } baseListModel.List = baseListModel.List.OrderByDescending(x => x.LastMessageModel.CreatedAt).ToList(); baseListModel.Total = aspNetUsers.Count(); } catch (Exception ex) { base.HandleError(baseListModel, CommonLayer.LookUps.ErrorType.Critical, ex); } return(baseListModel); }
public int GetNotFromMeAndNotReadCountByUserID(string userId) { try { int count = 0; //the below include is because a bug in EF6 List <string> aspNetUsersIDs = dbContext.AspNetUsers.Where(x => x.Id != contextInfo.UserID && ( x.Messages.Any(c => c.ToUserID == userId || c.FromUserID == userId) || x.Messages1.Any(c => c.ToUserID == userId || c.FromUserID == userId) ) ).Select(x => x.Id).ToList(); foreach (string aspNetUserID in aspNetUsersIDs) { //get last message between two users LMYFrameWorkMVC.Common.DAL.Message message = dbContext.Messages.Where(x => (x.FromUserID == userId || x.ToUserID == userId) && (x.FromUserID == aspNetUserID || x.ToUserID == aspNetUserID) ).OrderByDescending(x => x.CreatedAt).FirstOrDefault(); if (message != null && message.FromUserID != userId && message.Read == false) { count++; } } return(count); } catch (Exception ex) { base.HandleError(null, CommonLayer.LookUps.ErrorType.Critical, ex); } return(0); }