82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.maximotejeda.com/maximo/cedulados/internal/application/core/domain"
|
|
)
|
|
|
|
// QueryFotoByCedula
|
|
// Query a foto by its cedula
|
|
func (a Adapter) QueryFotoByCedula(ctx context.Context, cedula *domain.Cedula) (*domain.Foto, error) {
|
|
|
|
stmt, err := a.db.PrepareContext(
|
|
ctx,
|
|
`
|
|
SELECT imagen AS i
|
|
FROM fotos
|
|
WHERE mun_ced = ?
|
|
AND seq_ced = ?
|
|
AND ver_ced = ?
|
|
LIMIT 1
|
|
`)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer stmt.Close()
|
|
i := []byte{}
|
|
err = stmt.QueryRowContext(ctx, cedula.MunCed, cedula.SeqCed, cedula.VerCed).Scan(&i)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
foto := &domain.Foto{
|
|
MunCed: cedula.MunCed,
|
|
SeqCed: cedula.SeqCed,
|
|
VerCed: cedula.VerCed,
|
|
Imagen: i,
|
|
}
|
|
return foto, nil
|
|
|
|
}
|
|
|
|
// QueryFotoById
|
|
// Query fotos by a knowing id
|
|
func (a Adapter) QueryFotoById(ctx context.Context, id int64) (*domain.Foto, error) {
|
|
foto := &domain.Foto{}
|
|
stmt, err := a.db.PrepareContext(
|
|
ctx,
|
|
`
|
|
SELECT mun_ced, seq_ced, ver_ced, imagen AS i
|
|
FROM fotos
|
|
WHERE id = ?
|
|
`)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer stmt.Close()
|
|
err = stmt.QueryRow(id).Scan(&foto.MunCed, &foto.SeqCed, &foto.VerCed, foto.Imagen)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return foto, nil
|
|
|
|
}
|
|
|
|
// QueryAllCedulas
|
|
// query a list of cedulas
|
|
func (a Adapter) QueryAllCedulas(ctx context.Context, cedulas []*domain.Cedula) ([]*domain.Foto, error) {
|
|
if len(cedulas) == 0 {
|
|
return nil, fmt.Errorf("empty cedulas list")
|
|
}
|
|
images := []*domain.Foto{}
|
|
for _, v := range cedulas {
|
|
img, err := a.QueryFotoByCedula(ctx, v)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
images = append(images, img)
|
|
}
|
|
return images, nil
|
|
}
|