ЛР 2: закрыть уровень 4
Завершить PostgreSQL, Redis-сессии, миграции, таймауты и диагностику. Зафиксировать собственные тесты и доказательства финального прогона: 43 PASS, 0 FAIL, 0 SKIP; 768 HTTP-запросов без ошибок. Добавить официальный отчёт с запуском, архитектурой, SQL-планами и ограничениями. PR отложен до завершения уровня 5.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/model"
|
||||
"highload/internal/repository"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type MessageRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func NewMessageRepository(queries *sqlc.Queries) *MessageRepository {
|
||||
return &MessageRepository{
|
||||
queries: queries,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MessageRepository) CreateMessage(ctx context.Context, chatID, senderID, text string) (*model.Message, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
uuidChatID, err := getUUIDFromString(chatID)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
uuidSenderID, err := getUUIDFromString(senderID)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
sqlcMessage, err := r.queries.CreateMessage(dbCtx, sqlc.CreateMessageParams{ChatID: *uuidChatID, SenderID: *uuidSenderID, Text: text})
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
message := &model.Message{
|
||||
ID: sqlcMessage.ID.String(),
|
||||
ChatID: sqlcMessage.ChatID.String(),
|
||||
SenderID: sqlcMessage.SenderID.String(),
|
||||
Text: sqlcMessage.Text,
|
||||
CreatedAt: sqlcMessage.CreatedAt.Time,
|
||||
}
|
||||
|
||||
return message, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *MessageRepository) GetMessagesByChatID(ctx context.Context, chatID string, limit int, after *repository.MessagePosition) ([]*model.Message, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
uuidChatID, err := getUUIDFromString(chatID)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
afterCreatedAt := pgtype.Timestamptz{Valid: false}
|
||||
afterID := pgtype.UUID{Valid: false}
|
||||
|
||||
if after != nil {
|
||||
afterCreatedAt = pgtype.Timestamptz{Time: after.CreatedAt, Valid: true}
|
||||
if uuidFromString, err := getUUIDFromString(after.ID); err == nil {
|
||||
afterID = *uuidFromString
|
||||
} else {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
}
|
||||
|
||||
sqlcMessages, err := r.queries.GetMessagesByChatID(dbCtx, sqlc.GetMessagesByChatIDParams{ChatID: *uuidChatID, AfterID: afterID, AfterCreatedAt: afterCreatedAt, LimitCount: int32(limit)})
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
messages := make([]*model.Message, len(sqlcMessages))
|
||||
|
||||
for index, sqlcMessage := range sqlcMessages {
|
||||
messages[index] = &model.Message{
|
||||
ID: sqlcMessage.ID.String(),
|
||||
ChatID: sqlcMessage.ChatID.String(),
|
||||
SenderID: sqlcMessage.SenderID.String(),
|
||||
Text: sqlcMessage.Text,
|
||||
CreatedAt: sqlcMessage.CreatedAt.Time}
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user