private DialogInfoModel createInfoAboutUserCommunication(MyDBModels.Communication currentCommunication, int myProfileId) { var participantProfileId = currentCommunication.ParticipantProfileIdArray; DialogInfoModel dialogInfoModel = new DialogInfoModel(); dialogInfoModel.FullName = currentCommunication.Name; dialogInfoModel.PhotoUrl = currentCommunication.PhotoUrl; var participantCount = participantProfileId.Count(); if (currentCommunication.IsGroup) { dialogInfoModel.SecondInfoCount = String.Format("{0} {1}", participantCount, "members"); dialogInfoModel.ProfileShortModelList = createProfileShortModelList(participantProfileId, currentCommunication.CreaterProfileId); } else { var dateUtils = new DateUtils(); var profileFriend = participantProfileId.Where(p => p != myProfileId).First(); var db = new MyDBModels.DB(); var profileModel = db.profile.Where(p => p.ProfileId == profileFriend).First(); dialogInfoModel.SecondInfoTime = profileModel.TimeLastActive;// dateUtils.stateLastActivityProfile(profileFriend); dialogInfoModel.ProfileId = profileFriend; } return(dialogInfoModel); }
private void removeCommunication(MyDBModels.DB db, MyDBModels.Communication communicationModel, int profileId) { var dateUtils = new DateUtils(); int indexRemoveProfile = Array.IndexOf(communicationModel.ParticipantProfileIdArray, profileId); var participantProfileIdList = communicationModel.ParticipantProfileIdArray.ToList(); participantProfileIdList.Remove(profileId); communicationModel.ParticipantProfileIdArray = participantProfileIdList.ToArray(); var addProfileTimestampList = communicationModel.AddProfileTimestampArray.ToList(); addProfileTimestampList.Remove(communicationModel.AddProfileTimestampArray.ElementAt(indexRemoveProfile)); communicationModel.AddProfileTimestampArray = addProfileTimestampList.ToArray(); var userModel = db.user.Where(u => u.ProfileId == profileId).First(); var communicationIdArray = userModel.CommunicationIdArray; var communicationIdList = communicationIdArray.ToList(); communicationIdList.Remove(communicationModel.CommunicationId); userModel.CommunicationIdArray = communicationIdList.ToArray(); if (db.user.Where(u => u.ProfileId == profileId).First().CommunicationPinIdArray.Contains(communicationModel.CommunicationId)) { var communicationPinIdArray = userModel.CommunicationPinIdArray; var communicationPinIdList = communicationPinIdArray.ToList(); communicationPinIdList.Remove(communicationModel.CommunicationId); userModel.CommunicationPinIdArray = communicationPinIdList.ToArray(); } db.SaveChanges(); }
public void createCommunication(int userId, CommunicationShortModel model) { var db = new MyDBModels.DB(); var keyDailog = generateKeyDialog(); var dateUtils = new DateUtils(); var userModel = db.user.Where(u => u.UserId == userId).First(); MyDBModels.Communication communication = new MyDBModels.Communication(); communication.KeyDialog = keyDailog; communication.Name = model.Name; communication.PhotoUrl = model.PhotoUrl; communication.ParticipantProfileIdArray = model.ParticipantProfileIdArray; communication.CreaterProfileId = userModel.ProfileId; communication.AddProfileTimestampArray = Enumerable.Repeat(dateUtils.DateTimeToUnixTimeStamp(DateTime.Now), model.ParticipantProfileIdArray.Count()).ToArray(); if (model.ParticipantProfileIdArray.Count() > 2) { communication.IsGroup = true; } else if (model.ParticipantProfileIdArray.Count() < 3) { foreach (int profileId in model.ParticipantProfileIdArray) { var profile = db.profile.Where(p => p.ProfileId == profileId).First(); var searchFullName = String.Format("{0} {1}", profile.Name, profile.LastName); if (model.Name.Contains(searchFullName)) { communication.IsGroup = false; break; } else { communication.IsGroup = true; } } ; } else { communication.IsGroup = false; } var systemMessageCreateId = createSystemMessage(userId, String.Format("{0} {1}", "Created communication", model.Name), 2); communication.MessageIdArray = new int[] { systemMessageCreateId }; db.communication.Add(communication); db.SaveChanges(); int communicationId = db.communication.Where(c => c.KeyDialog == keyDailog).First().CommunicationId; model.ParticipantProfileIdArray.ToList().ForEach(delegate(int profileId) { addCommunicationToUserByProfile(db, profileId, communicationId); }); }
private CommunicationDataModel createCommunicationDataFromDb(MyDBModels.Communication modelDb, int myProfileId, int countEnd) { var db = new MyDBModels.DB(); var dateUtils = new DateUtils(); CommunicationDataModel model = new CommunicationDataModel(); model.CommunicationId = modelDb.CommunicationId; model.Name = modelDb.Name; model.PhotoUrl = modelDb.PhotoUrl; model.IsGroup = modelDb.IsGroup; if (modelDb.MessageIdArray.Count() > 0) { var lastMessageId = modelDb.MessageIdArray.ElementAt(modelDb.MessageIdArray.Count() - countEnd); var lastMessageModel = db.message.Where(m => m.MessageId == lastMessageId).First(); if (lastMessageModel.TypeMessage == 1) { model.LastWrittenName = lastMessageModel.ProfileId == myProfileId ? "You" : modelDb.IsGroup ? db.profile.Where(p => p.ProfileId == lastMessageModel.ProfileId).First().Name : ""; model.LastMessage = lastMessageModel.DataMessage; model.LastDate = lastMessageModel.TimeWritten; model.IsRead = lastMessageModel.ProfileId == myProfileId?lastMessageModel.IsReadProfileId.Count() > 1 : true; model.CountUnreadMessage = calculateCountUnreadMessages(modelDb.MessageIdArray, myProfileId); } else if (lastMessageModel.TypeMessage == 2) { model.LastWrittenName = ""; model.LastMessage = lastMessageModel.DataMessage; model.LastDate = lastMessageModel.TimeWritten; model.IsRead = lastMessageModel.ProfileId == myProfileId?lastMessageModel.IsReadProfileId.Count() > 1 : true; model.CountUnreadMessage = calculateCountUnreadMessages(modelDb.MessageIdArray, myProfileId); } else if (lastMessageModel.TypeMessage == 3) { if (lastMessageModel.ProfileId == myProfileId) { model.LastWrittenName = ""; model.LastMessage = lastMessageModel.DataMessage; model.LastDate = lastMessageModel.TimeWritten; model.IsRead = true; model.CountUnreadMessage = 0; } else { model = createCommunicationDataFromDb(modelDb, myProfileId, 2); } } } return(model); }