Implement lab2 messenger API with PostgreSQL Redis and Docker
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"mtusi2026lab2/internal/model"
|
||||
)
|
||||
|
||||
var ErrMessageMemberNotFound = errors.New("sender is not a chat member")
|
||||
|
||||
type MessageStore struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewMessageStore(pool *pgxpool.Pool) *MessageStore {
|
||||
return &MessageStore{pool: pool}
|
||||
}
|
||||
|
||||
func (s *MessageStore) CreateMessage(
|
||||
ctx context.Context,
|
||||
message model.Message,
|
||||
) error {
|
||||
const query = `
|
||||
INSERT INTO messages (
|
||||
id, chat_id, sender_id, text, created_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`
|
||||
|
||||
_, err := s.pool.Exec(ctx, query,
|
||||
message.ID,
|
||||
message.ChatID,
|
||||
message.SenderID,
|
||||
message.Text,
|
||||
message.CreatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23503" {
|
||||
return ErrMessageMemberNotFound
|
||||
}
|
||||
return fmt.Errorf("create message: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListMessages implements keyset pagination.
|
||||
// Cursor is the created_at + id of the previous page's last item.
|
||||
func (s *MessageStore) ListMessages(
|
||||
ctx context.Context,
|
||||
chatID uuid.UUID,
|
||||
limit int,
|
||||
cursorTime *time.Time,
|
||||
cursorID *uuid.UUID,
|
||||
) ([]model.Message, bool, error) {
|
||||
var rows pgx.Rows
|
||||
var err error
|
||||
|
||||
if cursorTime == nil || cursorID == nil {
|
||||
rows, err = s.pool.Query(ctx, `
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2
|
||||
`, chatID, limit+1)
|
||||
} else {
|
||||
rows, err = s.pool.Query(ctx, `
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
AND (created_at, id) > ($2, $3)
|
||||
ORDER BY created_at, id
|
||||
LIMIT $4
|
||||
`, chatID, *cursorTime, *cursorID, limit+1)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("list messages: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
messages := make([]model.Message, 0, limit+1)
|
||||
|
||||
for rows.Next() {
|
||||
var message model.Message
|
||||
|
||||
if err := rows.Scan(
|
||||
&message.ID,
|
||||
&message.ChatID,
|
||||
&message.SenderID,
|
||||
&message.Text,
|
||||
&message.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, false, fmt.Errorf("scan message: %w", err)
|
||||
}
|
||||
|
||||
messages = append(messages, message)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, false, fmt.Errorf("iterate messages: %w", err)
|
||||
}
|
||||
|
||||
hasMore := len(messages) > limit
|
||||
if hasMore {
|
||||
messages = messages[:limit]
|
||||
}
|
||||
|
||||
return messages, hasMore, nil
|
||||
}
|
||||
Reference in New Issue
Block a user