package repositories import ( "context" "errors" "time" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgxpool" ) type Chat struct { ID string `json:"id"` Title string `json:"title"` MemberIDs []string `json:"member_ids"` CreatedAt time.Time `json:"created_at"` } type ChatStore struct { db *pgxpool.Pool } func NewChatStore(db *pgxpool.Pool) *ChatStore { return &ChatStore{db: db} } var ErrUnknownMember = errors.New("unknown member") func (s *ChatStore) Create(ctx context.Context, title string, memberIDs []string) (Chat, error) { ctx, cancel := boundedQuery(ctx) defer cancel() membersCopy := append([]string(nil), memberIDs...) chat := Chat{ ID: uuid.New().String(), Title: title, MemberIDs: membersCopy, CreatedAt: time.Now().UTC().Truncate(time.Microsecond), } tx, err := s.db.Begin(ctx) if err != nil { return Chat{}, err } defer tx.Rollback(ctx) for _, id := range membersCopy { var exists bool if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM users WHERE id=$1)", id).Scan(&exists); err != nil { return Chat{}, err } if !exists { return Chat{}, ErrUnknownMember } } if _, err = tx.Exec(ctx, "INSERT INTO chats(id,title,created_at) VALUES($1,$2,$3)", chat.ID, title, chat.CreatedAt); err != nil { return Chat{}, err } for _, id := range membersCopy { if _, err = tx.Exec(ctx, "INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)", chat.ID, id); err != nil { return Chat{}, err } } if err = tx.Commit(ctx); err != nil { return Chat{}, err } return chat, nil } func (s *ChatStore) GetByID(ctx context.Context, id string) (Chat, error) { ctx, cancel := boundedQuery(ctx) defer cancel() var chat Chat err := s.db.QueryRow(ctx, "SELECT id,title,created_at FROM chats WHERE id=$1", id).Scan(&chat.ID, &chat.Title, &chat.CreatedAt) if err != nil { return Chat{}, err } rows, err := s.db.Query(ctx, "SELECT user_id FROM chat_members WHERE chat_id=$1 ORDER BY user_id", id) if err != nil { return Chat{}, err } defer rows.Close() chat.MemberIDs = []string{} for rows.Next() { var member string if err = rows.Scan(&member); err != nil { return Chat{}, err } chat.MemberIDs = append(chat.MemberIDs, member) } return chat, rows.Err() } func (s *ChatStore) ListPageForUser(ctx context.Context, userID string, cursor *ChatCursor, limit int) ([]Chat, bool, error) { ctx, cancel := boundedQuery(ctx) defer cancel() afterTime := time.Time{} afterID := "00000000-0000-0000-0000-000000000000" if cursor != nil { afterTime = cursor.CreatedAt afterID = cursor.ChatID } rows, err := s.db.Query(ctx, `SELECT c.id,c.title,c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=$1 AND (c.created_at,c.id)>($2,$3::uuid) ORDER BY c.created_at,c.id LIMIT $4`, userID, afterTime, afterID, limit+1) if err != nil { return nil, false, err } defer rows.Close() chats := []Chat{} for rows.Next() { var c Chat if err = rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil { return nil, false, err } chats = append(chats, c) } if err = rows.Err(); err != nil { return nil, false, err } more := len(chats) > limit if more { chats = chats[:limit] } for i := range chats { chat, err := s.GetByID(ctx, chats[i].ID) if err != nil { return nil, false, err } chats[i] = chat } return chats, more, nil }