//To make a PrivateChat //Assuming No chat between User1 and User2 Exist private async Task <PrivateChat> MakePrivateChat(ApplicationUser User1, ApplicationUser User2) { PrivateChat Chat = new PrivateChat { Key = Guid.NewGuid().ToString() }; ChatMap temp = new ChatMap { Key = Guid.NewGuid().ToString(), UserId = User1.Id, UserId_2 = User2.Id, ChatId = Chat.Key }; ChatMap temp2 = new ChatMap { Key = Guid.NewGuid().ToString(), UserId = User2.Id, UserId_2 = User1.Id, ChatId = Chat.Key }; try{ await Context.Chats.AddAsync(Chat); await Context.ChatMaps.AddAsync(temp); await Context.ChatMaps.AddAsync(temp2); await Context.SaveChangesAsync(); return(Chat); } catch (Exception e) { Console.WriteLine(e.Message); // change it to log later return(null); } }
//To Check if the relationship exist between two users private string GetPrivateChatId(ApplicationUser user1, ApplicationUser user2) { try{ ChatMap ch = Context.ChatMaps.Where(x => ((x.UserId == user1.Id) && (x.UserId_2 == user2.Id))).SingleOrDefault(); return(ch.ChatId); } catch (Exception e) { Console.WriteLine(e.Message); return(null); } }