public tbChats CreateOrGetChat(int firstUserID, int secondUserID, string name = null) { using CloudWorkContext context = new CloudWorkContext(); int[] userIDs = { firstUserID, secondUserID }; int chatID = 0; tbChats chat = new tbChats(); chatID = context.Chats.Where(x => userIDs.Contains(x.firstUserID) && userIDs.Contains(x.secondUserID)).Select(x => x.ID).FirstOrDefault(); if (chatID == 0) { var firstUserName = context.Users.Where(x => x.ID == firstUserID).Select(x => x.fullname).FirstOrDefault().ToString(); var secondUserName = context.Users.Where(x => x.ID == secondUserID).Select(x => x.fullname).FirstOrDefault().ToString(); var newChatObj = new tbChats() { firstUserID = firstUserID, firstUserName = firstUserName, secondUserName = secondUserName, secondUserID = secondUserID, Name = name, createDate = DateTime.Now }; context.Chats.Add(newChatObj); context.SaveChanges(); chat = newChatObj; chat.Name = getChatName(firstUserID, chat); } else { chat = GetChat(firstUserID, chatID); } return(chat); }
public void SendMessage(string messageText, int userID, int chatID) { using CloudWorkContext context = new CloudWorkContext(); //int[] userIDs = { firstUserID, secondUserID }; if (string.IsNullOrEmpty(messageText)) { ResponseBuilder.throwError("შეიყვანეთ მესიჯის ტექსტი"); } if (chatID > 0 && ChatExists(userID, chatID)) { var fromUserName = context.Users.Where(x => x.ID == userID).Select(x => x.fullname).FirstOrDefault().ToString(); //var secondUserName = context.Users.Where(x => x.ID == secondUserID).Select(x => x.fullname).FirstOrDefault().ToString(); var newMessageObj = new tbMessages() { chatID = chatID, fromUserID = userID, fromUserName = fromUserName, message = messageText, read = 0, createDate = DateTime.Now }; context.Messages.Add(newMessageObj); tbChats newChatObj = GetChat(userID, chatID); newChatObj.lastMessage = messageText; newChatObj.lastMessageDate = DateTime.Now; context.Chats.Update(newChatObj); context.SaveChanges(); } else { ResponseBuilder.throwError("ჩატი არ არსებობს"); } }
public string getChatName(int userID, tbChats chat) { if (string.IsNullOrEmpty(chat.Name)) { chat.Name = chat.firstUserID == userID ? chat.secondUserName : chat.firstUserName; } return(chat.Name); }
public tbChats CreateOrGetChat(Chat pChat) { tbChats chat = new tbChats(); if (provider.CanContact(authUser.ID, pChat.chatUserID)) { chat = provider.CreateOrGetChat(authUser.ID, pChat.chatUserID); } else { throwError("ამ ოპერაციის განხორციელების უფლება არ გაქვთ."); } return(chat); }