LINK do obiekty - Wybierz wszystkich znajomych Użytkownika i czat między nimi

0

Pytanie

Jak mogę wybrać wszystkich znajomych bieżącego użytkownika, który zaloguje się i Prywatne czaty (identyfikator czatu) pomiędzy użytkownikiem i jego przyjaciółmi? Mogę znaleźć znajomych użytkownika, ale u mnie również występują problemy z komunikacją między nimi.

            // Select all the User's friends and the chat (Id) between them
            var friends = await _context.Friendships // Get the Friendships
                .Include(x => x.Friend).ThenInclude(x => x.ChatUsers).ThenInclude(x => x.Chat)
                .Where(x => x.Status == StatusCode.Accepted && x.ApplicationUserId == userId)
                .Select(x => x.Friend)
                .ToListAsync();

Stół przyjaźni

    public class Friendship
    {
        // The primary keys/foreign keys of the associated tables
        public string ApplicationUserId { get; set; }
        public string ApplicationFriendUserId { get; set; }

        // Reference to the user that has the friend
        public User ApplicationUser { get; set; }

        // Reference to the friend
        public User Friend { get; set; }

        // The status of the friendship
        public StatusCode Status { get; set; }
    }

Tabela użytkowników

    public class User : IdentityUser
    {
        // Reference to all user's chats
        public ICollection<ChatUser> ChatUsers { get; set; }

        // Reference to all the user's friendships
        public ICollection<Friendship> UsersFriendships { get; set; }

        // Reference to all the friend's friendships (their point of view)
        public ICollection<Friendship> FriendsFriendships { get; set; }
    }

Tabela użytkowników czatu

    public class ChatUser
    {
        // The primary key/foreign keys of the associated tables
        public int ChatId { get; set; }
        public string UserId { get; set; }

        // The role that the User can be
        public UserRole Role { get; set; }

        // Reference to the chat
        public Chat Chat { get; set; }

        // Reference to the user
        public User User { get; set; }
    }

Czat

    
    public class Chat
    {
        // The primary key
        public int Id { get; set; }

        // The chat's name
        public string Name { get; set; }

        // The chat type, e.g room, private
        public ChatType Type { get; set; }

        // Reference to all the Chat's Users
        public ICollection<ChatUser> ChatUsers { get; set; }
    }

Dziękuję

1

Najlepsza odpowiedź

1

Ten wniosek:

var friends = await _context.Friendships // Get the Friendships
    .Include(x => x.Friend).ThenInclude(x => x.ChatUsers).ThenInclude(x => x.Chat)
    .Where(x => x.Status == StatusCode.Accepted && x.ApplicationUserId == userId)
    .Select(x => x.Friend)
    .ToListAsync();

... ładuje znajomych użytkownika z odpowiednimi rozmów znajomych, które będą obejmować rozmowy nie związane z bieżącym użytkownikiem.

Z taką strukturą domen do rozmów i przyjacielskich stosunków dość trudne spróbować połączyć je w znaczący sposób. Prawdopodobnie jest to możliwe przy prostym двухпроходном podejściu:

var friendIds = _context.Users
    .Where(x => s.UserId == userId)
    .SelectMany(x => x.UsersFriendships.Where(f => f.Status == StatusCode.Accepted).Select(f => f.ApplicationFriendUserId))
    .ToList(); // Get all accepted Friend IDs.


 var chats = _context.Chats
     .Where(x => x.ChatUsers.Any(cu => cu.UserId) && x => x.ChatUsers.Any(cu => friendIds.Contains(cu.UserId)
     .Select(x => new 
     {
         Chat = x,
         Friends = x.ChatUsers.Where(cu => friendIds.Contains(cu.UserId)).Select(cu => cu.User).ToList()
      }).ToList();

Czaty są związane z dwoma lub więcej użytkownikami, i nie są one ograniczone/nie związane z przyjacielskie stosunki.

Joe mógł przyjaźnić się z Samem, Jane i Johna i prowadzić następujące aktywne czaty:

Czat 1: Joe <-> Sam

Czat 2: Joe <-> Jane

Czat 3: Joe <-> Jane <-><-> Sam

Czat 4: Joe <-> Frank

Czat 5: Joe <-> Frank <-><-> Sam

Chcielibyśmy, aby czaty 1, 2, 3 i 5 zostały zwrócone. Z Johnem nie czatów, i nie dbamy o rozmowy z Frankiem, ale dbamy o rozmowy z Frankiem i Sam, tak jak Sam siebie. Celem byłoby to, aby ustalić, w jakich czatach Joe bierze udział z jednym lub kilkoma przyjaciółmi. Dla każdego meczu zwracamy czat i wszystkich Przyjaciół, którzy obecnie znajdują się w tej chacie.

Ostrzeżenie trybu dwuprzebiegowego podejścia polega na tym, że zakłada on, że lista znajomych zostanie wystarczająco małe, wystarczająco duże, aby przekroczyć IN() lista, który zostanie wygenerowany w celu uzyskania odpowiednich rozmów.

2021-11-23 04:50:49

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................