Files
mtusi2026lab4/internal/repository/postgres/attachment.go

189 lines
4.7 KiB
Go

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,
}
}