64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
|
|
package db
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/flatrender/render-svc/internal/models"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
const exportCols = `id, tenant_id, user_id, saved_project_id, project_id, render_job_id,
|
||
|
|
image, path, file_extension, file_type::text, render_quality::text,
|
||
|
|
create_type::text, size_bytes, duration_sec, width, height,
|
||
|
|
produce_date, auto_delete_date, delete_notified, created_at, deleted_at`
|
||
|
|
|
||
|
|
// ListAllExports returns every export across users (admin view), paginated.
|
||
|
|
func (s *Store) ListAllExports(ctx context.Context, page, pageSize int) ([]*models.Export, int64, error) {
|
||
|
|
var total int64
|
||
|
|
_ = s.pool.QueryRow(ctx, `SELECT COUNT(*) FROM render.exports WHERE deleted_at IS NULL`).Scan(&total)
|
||
|
|
|
||
|
|
rows, err := s.pool.Query(ctx,
|
||
|
|
`SELECT `+exportCols+`
|
||
|
|
FROM render.exports WHERE deleted_at IS NULL
|
||
|
|
ORDER BY produce_date DESC LIMIT $1 OFFSET $2`,
|
||
|
|
pageSize, (page-1)*pageSize)
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
defer rows.Close()
|
||
|
|
exports, err := scanExports(rows)
|
||
|
|
return exports, total, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetExportByIDAny fetches an export regardless of owner (admin).
|
||
|
|
func (s *Store) GetExportByIDAny(ctx context.Context, id uuid.UUID) (*models.Export, error) {
|
||
|
|
rows, err := s.pool.Query(ctx,
|
||
|
|
`SELECT `+exportCols+` FROM render.exports WHERE id = $1 AND deleted_at IS NULL`, id)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
defer rows.Close()
|
||
|
|
exports, err := scanExports(rows)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if len(exports) == 0 {
|
||
|
|
return nil, fmt.Errorf("export not found")
|
||
|
|
}
|
||
|
|
return exports[0], nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// SoftDeleteExportAny deletes an export regardless of owner (admin).
|
||
|
|
func (s *Store) SoftDeleteExportAny(ctx context.Context, id uuid.UUID) error {
|
||
|
|
tag, err := s.pool.Exec(ctx,
|
||
|
|
`UPDATE render.exports SET deleted_at = NOW() WHERE id = $1 AND deleted_at IS NULL`, id)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if tag.RowsAffected() == 0 {
|
||
|
|
return fmt.Errorf("export not found")
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|