package storage import ( "slices" "sync" "gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common" "github.com/google/uuid" ) type Entity = common.CreatedAtIDable type UserEntity interface { Entity GetUsername() string } type ChatEntity interface { Entity GetMemberIDs() []uuid.UUID } type MessageEntity interface { Entity GetChatID() uuid.UUID } type Store struct { mu sync.RWMutex users map[uuid.UUID]UserEntity usernames map[string]uuid.UUID chats map[uuid.UUID]ChatEntity userChats map[uuid.UUID][]uuid.UUID chatMessages map[uuid.UUID][]MessageEntity } func NewStore() *Store { return &Store{ users: make(map[uuid.UUID]UserEntity), usernames: make(map[string]uuid.UUID), chats: make(map[uuid.UUID]ChatEntity), userChats: make(map[uuid.UUID][]uuid.UUID), chatMessages: make(map[uuid.UUID][]MessageEntity), } } var GlobalStore = NewStore() type ReadOnlyTx interface { GetUser(id uuid.UUID) (UserEntity, bool) GetChat(id uuid.UUID) (ChatEntity, bool) UsernameExists(username string) bool IsMember(userID, chatID uuid.UUID) bool GetChatMessages(chatID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.PaginatedResponse[Entity], error) GetUserChats(userID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.PaginatedResponse[Entity], error) } type Tx interface { ReadOnlyTx CreateUser(user UserEntity) CreateChat(chat ChatEntity) CreateMessage(msg MessageEntity) } func (s *Store) View(apply func(tx ReadOnlyTx) error) error { s.mu.RLock() defer s.mu.RUnlock() return apply(s) } func (s *Store) Update(apply func(tx Tx) error) error { s.mu.Lock() defer s.mu.Unlock() return apply(s) } func (s *Store) CreateUser(user UserEntity) { s.users[user.GetID()] = user s.usernames[user.GetUsername()] = user.GetID() s.userChats[user.GetID()] = []uuid.UUID{} } func (s *Store) UsernameExists(username string) bool { _, exists := s.usernames[username] return exists } func (s *Store) GetUser(id uuid.UUID) (UserEntity, bool) { user, ok := s.users[id] return user, ok } func (s *Store) CreateChat(chat ChatEntity) { s.chats[chat.GetID()] = chat for _, memberID := range chat.GetMemberIDs() { s.userChats[memberID] = append(s.userChats[memberID], chat.GetID()) } s.chatMessages[chat.GetID()] = []MessageEntity{} } func (s *Store) GetChat(id uuid.UUID) (ChatEntity, bool) { chat, ok := s.chats[id] return chat, ok } func (s *Store) IsMember(userID, chatID uuid.UUID) bool { chat, ok := s.chats[chatID] if !ok { return false } return slices.Contains(chat.GetMemberIDs(), userID) } func (s *Store) CreateMessage(msg MessageEntity) { s.chatMessages[msg.GetChatID()] = append(s.chatMessages[msg.GetChatID()], msg) } func (s *Store) GetUserChats(userID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.PaginatedResponse[Entity], error) { chatIDs := s.userChats[userID] userChats := make([]Entity, 0, len(chatIDs)) for _, chatID := range chatIDs { if chat, exists := s.chats[chatID]; exists { userChats = append(userChats, chat) } } return common.Paginate(userChats, limit, cursor, userID) } func (s *Store) GetChatMessages(chatID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.PaginatedResponse[Entity], error) { chatMessages := s.chatMessages[chatID] messages := make([]Entity, 0, len(chatMessages)) for _, message := range chatMessages { messages = append(messages, message) } return common.Paginate(messages, limit, cursor, chatID) }