Files
mtusi2026lab5-fork/backend/internal/storage/postgres/attachment_variants.go
T

48 lines
1.6 KiB
Go

package postgres
import (
"context"
"high-loud-apps-labs/internal/domain"
"github.com/jackc/pgx/v5/pgxpool"
)
type AttachmentVariantRepo struct {
pool *pgxpool.Pool
}
func NewAttachmentVariantRepo(pool *pgxpool.Pool) *AttachmentVariantRepo {
return &AttachmentVariantRepo{pool: pool}
}
func (r *AttachmentVariantRepo) Upsert(ctx context.Context, tx domain.Tx, v *domain.AttachmentVariant) error {
const q = `
INSERT INTO attachment_variants
(attachment_id, name, object_key, content_type, size_bytes, width, height, sha256, created_at)
VALUES ($1::uuid, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (attachment_id, name) DO NOTHING
`
return tx.Exec(ctx, q, v.AttachmentID, v.Name, v.ObjectKey, v.ContentType, v.SizeBytes, v.Width, v.Height, v.SHA256, v.CreatedAt)
}
func (r *AttachmentVariantRepo) ListByAttachment(ctx context.Context, id string) ([]domain.AttachmentVariant, error) {
const q = `SELECT attachment_id, name, object_key, content_type, size_bytes, width, height, sha256, created_at FROM attachment_variants WHERE attachment_id = $1::uuid`
rows, err := r.pool.Query(ctx, q, id)
if err != nil {
return nil, mapError(err)
}
defer rows.Close()
var out []domain.AttachmentVariant
for rows.Next() {
var v domain.AttachmentVariant
if err := rows.Scan(&v.AttachmentID, &v.Name, &v.ObjectKey, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256, &v.CreatedAt); err != nil {
return nil, mapError(err)
}
out = append(out, v)
}
return out, mapError(rows.Err())
}
var _ domain.AttachmentVariantRepository = (*AttachmentVariantRepo)(nil)