public HttpResponseMessage getChatRoomId(int memberId, int otherMemberId) { VolunteerMatchDbContext db = new VolunteerMatchDbContext(); try { var chats = db.ChatHistories.Where(x => (x.fromMemberId == memberId && x.toMemberId == otherMemberId) || (x.fromMemberId == otherMemberId && x.toMemberId == memberId)).Select(x => new ChatRoomDTO() { chatRoomId = (int)x.chatRoomId, otherMemberId = otherMemberId, otherMemberName = db.Members.Where(z => z.id == otherMemberId).Select(y => y.fullName).FirstOrDefault(), otherMemberImage = db.Members.Where(z => z.id == otherMemberId).Select(y => y.pictureUrl).FirstOrDefault() }).FirstOrDefault(); if (chats == null) { ChatRoomDTO newChatRoom = new ChatRoomDTO(); newChatRoom.otherMemberId = otherMemberId; newChatRoom.otherMemberName = db.Members.Where(z => z.id == otherMemberId).Select(y => y.fullName).FirstOrDefault(); newChatRoom.otherMemberImage = db.Members.Where(z => z.id == otherMemberId).Select(y => y.pictureUrl).FirstOrDefault(); newChatRoom.chatRoomId = (int)db.ChatHistories.Max(x => x.chatRoomId) + 1; return(Request.CreateResponse(HttpStatusCode.OK, newChatRoom)); } return(Request.CreateResponse(HttpStatusCode.OK, chats)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
public IActionResult Overview(int chatRoomId) { var sessionUser = _userService.GetSessionUser(); ChatRoomDTO chatOverviewDTO = _chatService.CreateChatRoomDTO(chatRoomId, sessionUser); return(View(chatOverviewDTO)); }
//Dinamik olarak ChatRoom yaratan metodum. public ChatRoomDTO GetChatRoom(string userName, string userName2) { ChatRoom chat = null; ChatRoomDTO model = new ChatRoomDTO(); List <ChatRoom> chatRooms = new List <ChatRoom>(); var user1 = _uow.User.Find(x => x.UserName == userName); var user2 = _uow.User.Find(x => x.UserName == userName2); var chatBoxUser = _uow.ChatRoomUsers.FindByList(x => x.UserId == user1.Id); if (chatBoxUser.Count != 0) { foreach (var item in chatBoxUser) { chatRooms.AddRange(_uow.ChatRoom.FindByList(x => x.Id == item.ChatRoomId)); } foreach (var item in chatRooms) { if (_uow.ChatRoomUsers.Any(x => x.ChatRoomId == item.Id && x.UserId == user2.Id)) { var chatroomuser = chatBoxUser.Where(x => x.ChatRoomId == item.Id).FirstOrDefault(); chat = _uow.ChatRoom.GetById(chatroomuser.ChatRoomId); } else { chat = new ChatRoom(); _uow.ChatRoom.Add(chat); ChatRoomUsers chatRoom = new ChatRoomUsers(); chatRoom.ChatRoomId = chat.Id; chatRoom.UserId = user1.Id; _uow.ChatRoomUsers.Add(chatRoom); ChatRoomUsers chatRoom1 = new ChatRoomUsers(); chatRoom1.ChatRoomId = chat.Id; chatRoom1.UserId = user2.Id; _uow.ChatRoomUsers.Add(chatRoom1); _uow.SaveChange(); } } } else { chat = new ChatRoom(); _uow.ChatRoom.Add(chat); ChatRoomUsers chatRoom = new ChatRoomUsers(); chatRoom.ChatRoomId = chat.Id; chatRoom.UserId = user1.Id; _uow.ChatRoomUsers.Add(chatRoom); ChatRoomUsers chatRoom1 = new ChatRoomUsers(); chatRoom1.ChatRoomId = chat.Id; chatRoom1.UserId = user2.Id; _uow.ChatRoomUsers.Add(chatRoom1); _uow.SaveChange(); } model = _mapper.Map <ChatRoomDTO>(chat); return(model); }
public HttpResponseMessage getRoomChats(int memberId) { VolunteerMatchDbContext db = new VolunteerMatchDbContext(); try { var allChatRoomsId = db.ChatHistories.Where(x => x.fromMemberId == memberId || x.toMemberId == memberId).Select(y => y.chatRoomId).ToList(); List <int> relevantChatRoomsId = new List <int>(); List <ChatRoomDTO> chatRooms = new List <ChatRoomDTO>(); foreach (int chatRoomId in allChatRoomsId) { if (!relevantChatRoomsId.Contains(chatRoomId)) { relevantChatRoomsId.Add(chatRoomId); } } foreach (var chatRoomId in relevantChatRoomsId) { int firstMemberId = (int)db.ChatHistories.Where(x => x.chatRoomId == chatRoomId).Select(y => y.fromMemberId).FirstOrDefault(); int SecondMemberId = (int)db.ChatHistories.Where(x => x.chatRoomId == chatRoomId).Select(y => y.toMemberId).FirstOrDefault(); int otherMemberId; if (firstMemberId == memberId) { otherMemberId = SecondMemberId; } else { otherMemberId = firstMemberId; }; string otherMemberName = db.Members.Where(x => x.id == otherMemberId).Select(y => y.fullName).FirstOrDefault(); string otherMemberImage = db.Members.Where(x => x.id == otherMemberId).Select(y => y.pictureUrl).FirstOrDefault(); ChatHistory chatHistory = new ChatHistory(); //CHECK IF CAN BE OPTIMIZED var chat = db.ChatHistories.Where(x => x.chatRoomId == chatRoomId).Select(y => y.text).ToList(); var chatDate = db.ChatHistories.Where(x => x.chatRoomId == chatRoomId).Select(y => y.datetime).ToList(); int numOfMassages = chat.Count(); string lastSentence = chat[numOfMassages - 1]; var chatIds = db.ChatHistories.Where(x => x.chatRoomId == chatRoomId).Select(y => y.fromMemberId).ToList(); int lastMessageSenderId = (int)chatIds[numOfMassages - 1]; var messageMarkAsReadList = db.ChatHistories.Where(x => x.chatRoomId == chatRoomId).Select(y => y.markAsRead).ToList(); bool lastMessageMarkedAsRead = (bool)messageMarkAsReadList[numOfMassages - 1]; int lastUnixDate = (int)chatDate[numOfMassages - 1]; DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); dtDateTime = dtDateTime.AddSeconds(lastUnixDate).ToLocalTime(); //Checking if last massage was sent over the last 24 hrs string lastDate; DateTime now = DateTime.Now; if (dtDateTime > now.AddHours(-24) && dtDateTime <= now) { decimal hours = Math.Floor((decimal)(now - dtDateTime).TotalHours); int hoursDiffInt = (int)hours; if (hoursDiffInt == 0) { lastDate = (now - dtDateTime).Minutes.ToString(); if (lastDate == "0") { lastDate = "Now"; } else { lastDate += " min ago"; } } else { lastDate = $"{hoursDiffInt}h ago"; } } else { string year = dtDateTime.Year.ToString(); string month = dtDateTime.Month.ToString(); string day = dtDateTime.Day.ToString(); lastDate = $"{day}/{month}/{year}"; }; ChatRoomDTO chatRoomDTO = new ChatRoomDTO() { chatRoomId = chatRoomId, otherMemberId = otherMemberId, otherMemberName = otherMemberName, otherMemberImage = otherMemberImage, latstSentence = lastSentence, lastDate = lastDate, lastMessageSenderId = lastMessageSenderId, lastMessageMarkedAsRead = lastMessageMarkedAsRead, lastUnixDate = lastUnixDate }; chatRooms.Add(chatRoomDTO); } List <ChatRoomDTO> SortedList = chatRooms.OrderByDescending(o => o.lastUnixDate).ToList(); return(Request.CreateResponse(HttpStatusCode.OK, SortedList)); } catch (DbEntityValidationException ex) { string errors = ""; foreach (DbEntityValidationResult vr in ex.EntityValidationErrors) { foreach (DbValidationError er in vr.ValidationErrors) { errors += $"PropertyName - {er.PropertyName }, Error {er.ErrorMessage} <br/>"; } } return(Request.CreateResponse(HttpStatusCode.BadRequest, errors)); } catch (DbUpdateException ex) { DbUpdateException e = (DbUpdateException)ex; string errors = ""; foreach (DbEntityEntry entry in e.Entries) { errors += $"Error in entity - {entry.Entity.GetType().Name}, entity state - {entry.State} <br/>"; foreach (string prop in entry.CurrentValues.PropertyNames) { errors += $"for column - {prop}, value - {entry.CurrentValues[prop]} <br/>"; } errors += "---------------"; } return(Request.CreateResponse(HttpStatusCode.BadRequest, errors)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message)); } }