ЛР 4: вложения в S3 и двусторонняя сверка хранилищ
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/model"
|
||||
"highload/internal/repository"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const attachmentTransactionTimeout = 5 * time.Second
|
||||
|
||||
type AttachmentRepository struct {
|
||||
queries *sqlc.Queries
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewAttachmentRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *AttachmentRepository {
|
||||
return &AttachmentRepository{
|
||||
queries: queries,
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) CreateAttachment(ctx context.Context, params repository.CreateAttachmentParams) (*model.Attachment, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
chatUUID, err := getUUIDFromString(params.ChatID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerUUID, err := getUUIDFromString(params.OwnerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlcParams := sqlc.CreateAttachmentParams{
|
||||
ChatID: *chatUUID,
|
||||
OwnerID: *ownerUUID,
|
||||
Filename: params.Filename,
|
||||
ObjectKey: params.ObjectKey,
|
||||
ContentType: params.ContentType,
|
||||
SizeBytes: params.SizeBytes,
|
||||
Sha256: params.Sha256,
|
||||
}
|
||||
|
||||
sqlcAttachment, err := r.queries.CreateAttachment(dbCtx, sqlcParams)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
attachment := mapAttachmentSqlcToModel(sqlcAttachment)
|
||||
return attachment, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) GetAttachmentForUser(ctx context.Context, userID, attachmentID string) (*model.Attachment, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
userUUID, err := getUUIDFromString(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
attachmentUUID, err := getUUIDFromString(attachmentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := sqlc.GetAttachmentForUserParams{
|
||||
AttachmentID: *attachmentUUID,
|
||||
UserID: *userUUID,
|
||||
}
|
||||
|
||||
sqlcAttachment, err := r.queries.GetAttachmentForUser(dbCtx, params)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
attachment := mapAttachmentSqlcToModel(sqlcAttachment)
|
||||
return attachment, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) ExistsByObjectKey(ctx context.Context, key string) (bool, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
exists, err := r.queries.AttachmentExistsByObjectKey(dbCtx, key)
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) ListAttachmentKeys(ctx context.Context, afterKey string, pageSize int32) ([]repository.AttachmentKey, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
rows, err := r.queries.ListAttachmentKeys(dbCtx, sqlc.ListAttachmentKeysParams{AfterKey: afterKey, PageSize: pageSize})
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
keys := make([]repository.AttachmentKey, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
keys = append(keys, repository.AttachmentKey{
|
||||
ID: row.ID.String(), ObjectKey: row.ObjectKey, CreatedAt: row.CreatedAt.Time,
|
||||
})
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
attachmentID, err := getUUIDFromString(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
row, err := r.queries.GetAttachmentByID(dbCtx, *attachmentID)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
return mapAttachmentSqlcToModel(row), nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) WithObjectKeyLock(ctx context.Context, key string, fn func(context.Context, repository.AttachmentTx) error) (bool, error) {
|
||||
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := r.pool.BeginTx(txCtx, pgx.TxOptions{IsoLevel: pgx.ReadCommitted})
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
rollbackCtx, rollbackCancel := context.WithTimeout(
|
||||
context.WithoutCancel(ctx),
|
||||
time.Second,
|
||||
)
|
||||
defer rollbackCancel()
|
||||
|
||||
_ = tx.Rollback(rollbackCtx)
|
||||
}()
|
||||
txQueries := r.queries.WithTx(tx)
|
||||
|
||||
locked, err := txQueries.TryLockAttachmentObjectKey(txCtx, key)
|
||||
|
||||
if err != nil {
|
||||
return false, convertDBError(err)
|
||||
}
|
||||
|
||||
if !locked {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
txRepo := &AttachmentRepository{queries: txQueries}
|
||||
|
||||
if err = fn(txCtx, txRepo); err != nil {
|
||||
return true, err
|
||||
}
|
||||
|
||||
if err = tx.Commit(txCtx); err != nil {
|
||||
return true, convertDBError(err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func mapAttachmentSqlcToModel(sa sqlc.Attachment) *model.Attachment {
|
||||
return &model.Attachment{
|
||||
ID: sa.ID.String(),
|
||||
ChatID: sa.ChatID.String(),
|
||||
OwnerID: sa.OwnerID.String(),
|
||||
Filename: sa.Filename,
|
||||
ObjectKey: sa.ObjectKey,
|
||||
ContentType: sa.ContentType,
|
||||
SizeBytes: sa.SizeBytes,
|
||||
SHA256: sa.Sha256,
|
||||
CreatedAt: sa.CreatedAt.Time,
|
||||
UpdatedAt: sa.UpdatedAt.Time,
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/model"
|
||||
"highload/internal/repository"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type ChatRepository struct {
|
||||
queries *sqlc.Queries
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewChatRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *ChatRepository {
|
||||
return &ChatRepository{
|
||||
queries: queries,
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ChatRepository) CreateChat(ctx context.Context, title string, memberIDs []string) (*model.Chat, error) {
|
||||
txCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
tx, err := r.pool.Begin(txCtx)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
rollbackCtx, rollbackCancel := context.WithTimeout(
|
||||
context.WithoutCancel(ctx),
|
||||
time.Second,
|
||||
)
|
||||
defer rollbackCancel()
|
||||
|
||||
_ = tx.Rollback(rollbackCtx)
|
||||
}()
|
||||
txQueries := r.queries.WithTx(tx)
|
||||
|
||||
sqlcChat, err := txQueries.CreateChat(txCtx, title)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
chat := &model.Chat{
|
||||
ID: sqlcChat.ID.String(),
|
||||
Title: sqlcChat.Title,
|
||||
CreatedAt: sqlcChat.CreatedAt.Time,
|
||||
}
|
||||
|
||||
for _, id := range memberIDs {
|
||||
userID, err := getUUIDFromString(id)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
err = txQueries.CreateChatMember(txCtx, sqlc.CreateChatMemberParams{ChatID: sqlcChat.ID, UserID: *userID})
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
}
|
||||
|
||||
chat.MemberIDs = slices.Clone(memberIDs)
|
||||
|
||||
err = tx.Commit(txCtx)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *ChatRepository) GetChatByID(ctx context.Context, id string) (*model.Chat, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
uuidID, err := getUUIDFromString(id)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
sqlcChat, err := r.queries.GetChatByID(dbCtx, *uuidID)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
chat := mapChatSqlcToModel(&sqlcChat)
|
||||
|
||||
return chat, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *ChatRepository) GetChatsByUserID(ctx context.Context, userID string, limit int, after *repository.ChatPosition) ([]*model.Chat, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
uuidUserID, err := getUUIDFromString(userID)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
chats, err := r.queries.GetChatsByUserID(dbCtx, sqlc.GetChatsByUserIDParams{UserID: *uuidUserID, AfterCreatedAt: afterCreatedAt, AfterID: afterID, LimitCount: int32(limit)})
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
resultChats := make([]*model.Chat, len(chats))
|
||||
for index, sqlcChat := range chats {
|
||||
memberIDs := make([]string, len(sqlcChat.MemberIds))
|
||||
|
||||
for i, memberID := range sqlcChat.MemberIds {
|
||||
memberIDs[i] = memberID.String()
|
||||
}
|
||||
|
||||
resultChats[index] = &model.Chat{
|
||||
ID: sqlcChat.ID.String(),
|
||||
MemberIDs: memberIDs,
|
||||
CreatedAt: sqlcChat.CreatedAt.Time,
|
||||
Title: sqlcChat.Title,
|
||||
}
|
||||
}
|
||||
|
||||
return resultChats, nil
|
||||
}
|
||||
|
||||
func mapChatSqlcToModel(sqlcChat *sqlc.GetChatByIDRow) *model.Chat {
|
||||
|
||||
memberIDs := make([]string, len(sqlcChat.MemberIds))
|
||||
|
||||
for i, memberID := range sqlcChat.MemberIds {
|
||||
memberIDs[i] = memberID.String()
|
||||
}
|
||||
|
||||
return &model.Chat{
|
||||
ID: sqlcChat.ID.String(),
|
||||
MemberIDs: memberIDs,
|
||||
CreatedAt: sqlcChat.CreatedAt.Time,
|
||||
Title: sqlcChat.Title,
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"highload/internal/repository"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
func convertDBError(err error) error {
|
||||
var pgErr *pgconn.PgError
|
||||
var netErr *net.OpError
|
||||
var dnsErr *net.DNSError
|
||||
|
||||
switch {
|
||||
case errors.Is(err, context.DeadlineExceeded):
|
||||
return fmt.Errorf("%w: %v", repository.ErrServiceUnavailable, err)
|
||||
case errors.Is(err, pgx.ErrNoRows):
|
||||
return repository.ErrNotFound
|
||||
case errors.As(err, &pgErr):
|
||||
switch pgErr.Code {
|
||||
case "23505":
|
||||
return repository.ErrAlreadyExists
|
||||
case "53300", "57P03", "08006", "08001", "57P01", "57P02":
|
||||
return repository.ErrServiceUnavailable
|
||||
}
|
||||
case errors.Is(err, io.EOF) ||
|
||||
errors.Is(err, io.ErrUnexpectedEOF) ||
|
||||
errors.Is(err, pgconn.ErrConnClosed):
|
||||
return repository.ErrServiceUnavailable
|
||||
case errors.Is(err, repository.ErrInvalidInput):
|
||||
return repository.ErrInvalidInput
|
||||
case errors.As(err, &netErr):
|
||||
return repository.ErrServiceUnavailable
|
||||
case errors.As(err, &dnsErr):
|
||||
return repository.ErrServiceUnavailable
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w: %v", repository.ErrDatabase, err)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/model"
|
||||
"highload/internal/repository"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type MessageRepository struct {
|
||||
queries *sqlc.Queries
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewMessageRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *MessageRepository {
|
||||
return &MessageRepository{
|
||||
queries: queries,
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MessageRepository) CreateMessage(ctx context.Context, chatID, senderID, text, key string, attachmentIDs []string) (*model.Message, error) {
|
||||
|
||||
uuidChatID, err := getUUIDFromString(chatID)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
txCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
uuidSenderID, err := getUUIDFromString(senderID)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
tx, err := r.pool.Begin(txCtx)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
rollbackCtx, rollbackCancel := context.WithTimeout(
|
||||
context.WithoutCancel(ctx),
|
||||
time.Second,
|
||||
)
|
||||
defer rollbackCancel()
|
||||
|
||||
_ = tx.Rollback(rollbackCtx)
|
||||
}()
|
||||
|
||||
txQueries := r.queries.WithTx(tx)
|
||||
|
||||
sqlcMessage, err := txQueries.CreateMessage(txCtx, sqlc.CreateMessageParams{ChatID: *uuidChatID, SenderID: *uuidSenderID, Text: text})
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
requestedIDs := make([]string, 0, len(attachmentIDs))
|
||||
for index, attID := range attachmentIDs {
|
||||
attUUID, err := getUUIDFromString(attID)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
requestedIDs = append(requestedIDs, attUUID.String())
|
||||
|
||||
cmaParams := sqlc.CreateMessagesAttachmentsParams{MessageID: sqlcMessage.ID, AttachmentID: *attUUID, ChatID: sqlcMessage.ChatID, Position: int16(index)}
|
||||
rowsAffected, err := txQueries.CreateMessagesAttachments(txCtx, cmaParams)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
rowsAffected, err := txQueries.InsertMessageIdempotency(txCtx, sqlc.InsertMessageIdempotencyParams{UserID: sqlcMessage.SenderID, ChatID: sqlcMessage.ChatID, IdempotencyKey: key, MessageID: sqlcMessage.ID})
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
resultIDs := requestedIDs
|
||||
|
||||
switch rowsAffected {
|
||||
case 1:
|
||||
err := tx.Commit(txCtx)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
case 0:
|
||||
rollbackCtx, rollbackCancel := context.WithTimeout(context.WithoutCancel(ctx), time.Second)
|
||||
err := tx.Rollback(rollbackCtx)
|
||||
rollbackCancel()
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
sqlcMessage, err = r.queries.GetIdempotentMessage(dbCtx, sqlc.GetIdempotentMessageParams{UserID: sqlcMessage.SenderID, ChatID: sqlcMessage.ChatID, IdempotencyKey: key})
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
rows, err := r.queries.GetMessagesAttachments(dbCtx, sqlcMessage.ID)
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
savedIDs := make([]string, 0, len(rows))
|
||||
|
||||
for _, row := range rows {
|
||||
savedIDs = append(savedIDs, row.AttachmentID.String())
|
||||
}
|
||||
|
||||
if sqlcMessage.Text != text || !slices.Equal(savedIDs, requestedIDs) {
|
||||
return nil, repository.ErrConflict
|
||||
}
|
||||
resultIDs = savedIDs
|
||||
|
||||
default:
|
||||
return nil, repository.ErrServerError
|
||||
}
|
||||
|
||||
message := &model.Message{
|
||||
ID: sqlcMessage.ID.String(),
|
||||
ChatID: sqlcMessage.ChatID.String(),
|
||||
SenderID: sqlcMessage.SenderID.String(),
|
||||
Text: sqlcMessage.Text,
|
||||
AttachmentIDs: resultIDs,
|
||||
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)
|
||||
}
|
||||
|
||||
messageIDs := make([]pgtype.UUID, len(sqlcMessages))
|
||||
for index, _ := range sqlcMessages {
|
||||
messageIDs[index] = sqlcMessages[index].ID
|
||||
}
|
||||
|
||||
messageRow, err := r.queries.GetAttachmentsForMessages(dbCtx, messageIDs)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
mapMessagesAtt := make(map[string][]string)
|
||||
for index, _ := range messageRow {
|
||||
messageID := messageRow[index].MessageID.String()
|
||||
attachmentID := messageRow[index].AttachmentID.String()
|
||||
|
||||
mapMessagesAtt[messageID] = append(mapMessagesAtt[messageID], attachmentID)
|
||||
|
||||
}
|
||||
|
||||
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,
|
||||
AttachmentIDs: mapMessagesAtt[sqlcMessage.ID.String()],
|
||||
CreatedAt: sqlcMessage.CreatedAt.Time}
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/model"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func NewUserRepository(queries *sqlc.Queries) *UserRepository {
|
||||
return &UserRepository{queries: queries}
|
||||
}
|
||||
|
||||
func (r *UserRepository) Create(ctx context.Context, username, displayName string, passwordHash string) (*model.User, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
sqlcUser, err := r.queries.CreateUser(dbCtx, sqlc.CreateUserParams{Username: username, DisplayName: displayName, PasswordHash: passwordHash})
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
user := &model.User{
|
||||
ID: sqlcUser.ID.String(),
|
||||
Username: sqlcUser.Username,
|
||||
DisplayName: sqlcUser.DisplayName,
|
||||
CreatedAt: sqlcUser.CreatedAt.Time,
|
||||
PasswordHash: sqlcUser.PasswordHash,
|
||||
}
|
||||
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetUserByID(ctx context.Context, id string) (*model.User, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
uuidID, err := getUUIDFromString(id)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
sqlcUser, err := r.queries.GetUserByID(dbCtx, *uuidID)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
user := &model.User{
|
||||
ID: sqlcUser.ID.String(),
|
||||
Username: sqlcUser.Username,
|
||||
DisplayName: sqlcUser.DisplayName,
|
||||
CreatedAt: sqlcUser.CreatedAt.Time,
|
||||
PasswordHash: sqlcUser.PasswordHash,
|
||||
}
|
||||
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetUserByUsername(ctx context.Context, username string) (*model.User, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
sqlcUser, err := r.queries.GetUserByUsername(dbCtx, username)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
user := &model.User{
|
||||
ID: sqlcUser.ID.String(),
|
||||
Username: sqlcUser.Username,
|
||||
DisplayName: sqlcUser.DisplayName,
|
||||
CreatedAt: sqlcUser.CreatedAt.Time,
|
||||
PasswordHash: sqlcUser.PasswordHash,
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"highload/internal/repository"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func getUUIDFromString(id string) (*pgtype.UUID, error) {
|
||||
var uuidID pgtype.UUID
|
||||
|
||||
if err := uuidID.Scan(id); err != nil {
|
||||
return nil, repository.ErrInvalidInput
|
||||
}
|
||||
|
||||
return &uuidID, nil
|
||||
}
|
||||
Reference in New Issue
Block a user