exportação csv
This commit is contained in:
parent
65118f2838
commit
6be329f7e6
3 changed files with 281 additions and 95 deletions
|
|
@ -100,6 +100,10 @@ type ListarRespostasFiltro struct {
|
|||
|
||||
func (f *ListarRespostasFiltro) normalizar() { (*contratos.ListarRespostasFiltro)(f).Normalizar() }
|
||||
|
||||
type ExportarRespostasFiltro struct {
|
||||
SomenteNotasBaixas bool
|
||||
}
|
||||
|
||||
// ListarRespostas retorna respostas respondidas, com paginação e filtro.
|
||||
func (s *Store) ListarRespostas(ctx context.Context, tabela string, filtro ListarRespostasFiltro) ([]contratos.RespostaPainel, error) {
|
||||
// Segurança: a tabela é um identificador interpolado. Validamos sempre.
|
||||
|
|
@ -165,6 +169,45 @@ LIMIT $1 OFFSET $2`, tabela, cond)
|
|||
return respostas, rows.Err()
|
||||
}
|
||||
|
||||
// ExportarRespostas abre um cursor/stream de respostas (sem paginação) para export.
|
||||
//
|
||||
// Importante: o caller deve SEMPRE fechar o rows.
|
||||
func (s *Store) ExportarRespostas(ctx context.Context, tabela string, filtro ExportarRespostasFiltro) (pgx.Rows, error) {
|
||||
// Segurança: a tabela é um identificador interpolado. Validamos sempre.
|
||||
if !db.TableNameValido(tabela) {
|
||||
return nil, fmt.Errorf("tabela invalida")
|
||||
}
|
||||
|
||||
cond := "status='respondido' AND valida=true"
|
||||
if filtro.SomenteNotasBaixas {
|
||||
cond += " AND nota BETWEEN 1 AND 6"
|
||||
}
|
||||
|
||||
// Sem LIMIT/OFFSET (export completo).
|
||||
q := fmt.Sprintf(`
|
||||
SELECT
|
||||
id,
|
||||
respondido_em,
|
||||
pedido_criado_em,
|
||||
inquilino_codigo,
|
||||
inquilino_nome,
|
||||
usuario_codigo,
|
||||
usuario_nome,
|
||||
usuario_email,
|
||||
usuario_telefone,
|
||||
nota,
|
||||
justificativa
|
||||
FROM %s
|
||||
WHERE %s
|
||||
ORDER BY respondido_em DESC NULLS LAST`, tabela, cond)
|
||||
|
||||
rows, err := s.pool.Query(ctx, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// ensure interface imports
|
||||
var _ = pgx.ErrNoRows
|
||||
var _ = time.Second
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue