forked from mtusi2026/mtusi2026lab6
40 lines
1.5 KiB
Go
40 lines
1.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func (r *AttachmentsRepo) Page(ctx context.Context, after uuid.UUID) ([]*AttachmentRecord, error) {
|
|
rows, e := r.Pool.Query(ctx, "SELECT "+AttachmentColumns+" FROM attachments WHERE id>$1 ORDER BY id LIMIT 100", after)
|
|
if e != nil {
|
|
return nil, apperr.WrapPostgres(e, "scan failed")
|
|
}
|
|
defer rows.Close()
|
|
out := make([]*AttachmentRecord, 0, 100)
|
|
for rows.Next() {
|
|
a, e := ScanAttachment(rows)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
out = append(out, a)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
func (r *AttachmentsRepo) Exists(ctx context.Context, c *pgxpool.Conn, key string) (bool, error) {
|
|
var found bool
|
|
e := c.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM attachment_variants WHERE object_key=$1 UNION ALL SELECT 1 FROM attachments WHERE object_key=$1 OR staging_key=$1 OR variant_keys @> jsonb_build_object('thumbnail',$1::text) OR variant_keys @> jsonb_build_object('crop',$1::text) OR variant_keys @> jsonb_build_object('optimized',$1::text))", key).Scan(&found)
|
|
return found, e
|
|
}
|
|
func (r *AttachmentsRepo) MarkMissing(ctx context.Context, c *pgxpool.Conn, id uuid.UUID, missing bool) (bool, error) {
|
|
tag, e := c.Exec(ctx, "UPDATE attachments SET integrity_missing=$2,updated_at=clock_timestamp() WHERE id=$1 AND integrity_missing<>$2", id, missing)
|
|
return tag.RowsAffected() == 1, e
|
|
}
|
|
|
|
func (r *AttachmentsRepo) CheckConn(ctx context.Context, c *pgxpool.Conn) error {
|
|
_, e := c.Exec(ctx, "SELECT 1")
|
|
return e
|
|
}
|