forked from mtusi2026/mtusi2026lab2
164 lines
4.1 KiB
Go
164 lines
4.1 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
|
)
|
|
|
|
type Chats struct {
|
|
conn
|
|
}
|
|
|
|
func (c *Chats) CreateChat(ctx context.Context, callerID, title string, memberIDs []string) (domain.Chat, error) {
|
|
if strings.TrimSpace(title) == "" {
|
|
return domain.Chat{}, domain.ErrInvalidArgument
|
|
}
|
|
if len(memberIDs) == 0 || len(memberIDs) > 100 {
|
|
return domain.Chat{}, domain.ErrInvalidArgument
|
|
}
|
|
seen := make(map[string]struct{}, len(memberIDs))
|
|
callerInMembers := false
|
|
for _, id := range memberIDs {
|
|
if _, dup := seen[id]; dup {
|
|
return domain.Chat{}, domain.ErrInvalidArgument
|
|
}
|
|
seen[id] = struct{}{}
|
|
if id == callerID {
|
|
callerInMembers = true
|
|
}
|
|
}
|
|
if !callerInMembers {
|
|
return domain.Chat{}, domain.ErrInvalidArgument
|
|
}
|
|
|
|
ctx, cancel := c.withTimeout(ctx)
|
|
defer cancel()
|
|
|
|
tx, err := c.pool.Begin(ctx)
|
|
if err != nil {
|
|
return domain.Chat{}, mapErr(err)
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
chat := domain.Chat{Title: title}
|
|
err = tx.QueryRow(ctx, `
|
|
INSERT INTO chats (title)
|
|
VALUES ($1)
|
|
RETURNING id::text, created_at`,
|
|
title,
|
|
).Scan(&chat.ID, &chat.CreatedAt)
|
|
if err != nil {
|
|
return domain.Chat{}, mapErr(err)
|
|
}
|
|
|
|
_, err = tx.Exec(ctx, `
|
|
INSERT INTO chat_members (chat_id, user_id)
|
|
SELECT $1, unnest($2::uuid[])`,
|
|
chat.ID, memberIDs,
|
|
)
|
|
if err != nil {
|
|
return domain.Chat{}, mapErr(err)
|
|
}
|
|
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return domain.Chat{}, mapErr(err)
|
|
}
|
|
|
|
chat.MemberIDs = append([]string(nil), memberIDs...)
|
|
chat.CreatedAt = chat.CreatedAt.UTC()
|
|
return chat, nil
|
|
}
|
|
|
|
func (c *Chats) ChatByID(ctx context.Context, callerID, chatID string) (domain.Chat, error) {
|
|
ctx, cancel := c.withTimeout(ctx)
|
|
defer cancel()
|
|
|
|
chat := domain.Chat{}
|
|
err := c.pool.QueryRow(ctx, `
|
|
SELECT c.id::text, c.title, c.created_at,
|
|
(SELECT array_agg(m.user_id::text ORDER BY m.user_id)
|
|
FROM chat_members m WHERE m.chat_id = c.id)
|
|
FROM chats c
|
|
JOIN chat_members me ON me.chat_id = c.id AND me.user_id = $2
|
|
WHERE c.id = $1`,
|
|
chatID, callerID,
|
|
).Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs)
|
|
if err != nil {
|
|
err = mapErr(err)
|
|
if err == domain.ErrInvalidArgument {
|
|
return domain.Chat{}, domain.ErrNotFound
|
|
}
|
|
return domain.Chat{}, err
|
|
}
|
|
chat.CreatedAt = chat.CreatedAt.UTC()
|
|
return chat, nil
|
|
}
|
|
|
|
func (c *Chats) ListChats(ctx context.Context, callerID, cursor string, limit int) (domain.Page[domain.Chat], error) {
|
|
if limit <= 0 || limit > 100 {
|
|
return domain.Page[domain.Chat]{}, domain.ErrInvalidArgument
|
|
}
|
|
cur, err := domain.DecodeCursor(cursor)
|
|
if err != nil {
|
|
return domain.Page[domain.Chat]{}, err
|
|
}
|
|
scope := "chats:" + callerID
|
|
if cursor != "" && cur.Scope != scope {
|
|
return domain.Page[domain.Chat]{}, domain.ErrInvalidArgument
|
|
}
|
|
afterTime, afterID := cursorArgs(cur)
|
|
|
|
ctx, cancel := c.withTimeout(ctx)
|
|
defer cancel()
|
|
|
|
rows, err := c.pool.Query(ctx, `
|
|
SELECT c.id::text, c.title, c.created_at,
|
|
(SELECT array_agg(m.user_id::text ORDER BY m.user_id)
|
|
FROM chat_members m WHERE m.chat_id = c.id)
|
|
FROM chat_members me
|
|
JOIN chats c ON c.id = me.chat_id
|
|
WHERE me.user_id = $1
|
|
AND ($2::timestamptz IS NULL OR (c.created_at, c.id) > ($2, $3::uuid))
|
|
ORDER BY c.created_at, c.id
|
|
LIMIT $4`,
|
|
callerID, afterTime, afterID, limit+1,
|
|
)
|
|
if err != nil {
|
|
return domain.Page[domain.Chat]{}, mapErr(err)
|
|
}
|
|
out, err := pgx.CollectRows(rows, func(r pgx.CollectableRow) (domain.Chat, error) {
|
|
var ch domain.Chat
|
|
err := r.Scan(&ch.ID, &ch.Title, &ch.CreatedAt, &ch.MemberIDs)
|
|
ch.CreatedAt = ch.CreatedAt.UTC()
|
|
return ch, err
|
|
})
|
|
if err != nil {
|
|
return domain.Page[domain.Chat]{}, mapErr(err)
|
|
}
|
|
|
|
page := domain.Page[domain.Chat]{}
|
|
if len(out) > limit {
|
|
last := out[limit-1]
|
|
page.NextCursor = domain.EncodeCursor(domain.Cursor{
|
|
CreatedAt: last.CreatedAt,
|
|
ID: last.ID,
|
|
Scope: scope,
|
|
})
|
|
out = out[:limit]
|
|
}
|
|
page.Items = out
|
|
return page, nil
|
|
}
|
|
|
|
func cursorArgs(c domain.Cursor) (*time.Time, *string) {
|
|
if c.IsZero() {
|
|
return nil, nil
|
|
}
|
|
return &c.CreatedAt, &c.ID
|
|
}
|