public void ConnectDoctor(string username) { business = new ConversationBusiness(_db); var id = Context.ConnectionId; var doctor = _db.Doctors.Where(x=>x.Username.Equals(username)).FirstOrDefault(); var doctorDetail = Doctors.Where(x => x.Username.Equals(username)).FirstOrDefault(); //Update Doctor list status if (doctorDetail == null) { doctorDetail = new DoctorDetail { ConnectionId = id, CountMessageUnRead = business.CountMessageUnRead(doctor), ProfilePicture = doctor.ProfilePicture, IsOnline = doctor.IsOnline, FullName = doctor.FullName, Username = doctor.Username, SpeciatyField = doctor.SpecialtyField.Name }; Doctors.Add(doctorDetail); } else { doctorDetail.CountMessageUnRead = business.CountMessageUnRead(doctor); doctorDetail.IsOnline = doctor.IsOnline; } //Add User UserDetail userDetail = new UserDetail { ConnectionId = id, CountMessageUnRead = business.CountMessageUnRead(doctor), ProfilePicture = doctor.ProfilePicture, FullName = doctor.FullName, Username = doctor.Username, }; //Show List of Lastest contact var userDetailList = new List<UserDetail>(); var conversations = _db.Conversations.Where( x => x.DoctorId == doctor.UserId). OrderByDescending(x=>x.LatestTimeFromPatient ).ToList(); foreach (var conversation in conversations) { var existUser = userDetailList.Where (x => x.Username == conversation.Patient.Username).FirstOrDefault(); if (existUser == null) { //Check user online or not var connectedUser = ConnectedUsers.Where (x => x.Username == conversation.Patient.Username).FirstOrDefault(); var IsOnline = (connectedUser != null && connectedUser.IsOnline) ? true: false; UserDetail userDetailCon = new UserDetail { FullName = conversation.Patient.FullName, LastestContent = conversation.LatestContentFromPatient, LastestTime = conversation.LatestTimeFromPatient, ProfilePicture = conversation.Patient.ProfilePicture, Username = conversation.Patient.Username, IsRead = conversation.IsRead, IsOnline = IsOnline }; userDetailList.Add(userDetailCon); } } ConnectedUsers.Add(userDetail); Debug.WriteLine("Size of userDetailList: " + userDetailList.Count); // send to all except caller client Clients.Caller.onGetConversationList(userDetailList); Clients.AllExcept(id).onGetDoctorList(Doctors); }
public void ConnectPatient(string username) { business = new ConversationBusiness(_db); var id = Context.ConnectionId; var userDetail = ConnectedUsers.Where(x => x.Username.Equals(username)).FirstOrDefault(); if (userDetail == null) { var user = _db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault(); ConnectedUsers.Add(new UserDetail { ConnectionId = id, Username = username, CountMessageUnRead = 0, FullName = user.FullName, ProfilePicture = user.ProfilePicture, IsOnline = true }); var DoctorListDB = _db.Doctors.ToList(); foreach (var doctor in DoctorListDB) { if (Doctors.Where(d => d.Username.Equals(doctor.Username)).FirstOrDefault() == null) { DoctorDetail doctorDetail = new DoctorDetail { FullName = doctor.FullName, IsOnline = false, SpeciatyField = doctor.SpecialtyField.Name, Username = doctor.Username, ProfilePicture = doctor.ProfilePicture }; Doctors.Add(doctorDetail); } } // send to caller Clients.Caller.onGetDoctorList(Doctors); } }