infoalloggi-monorepo/apps/backend/queries/images.go

45 lines
1.1 KiB
Go
Raw Normal View History

package queries
import (
"backend/typedefs"
"fmt"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5"
)
func ImagesGetAll() ([]typedefs.MiogestImagesRef_Table, error) {
var images []typedefs.MiogestImagesRef_Table
err := pgxscan.Select(Db.Ctx, Db.Dbpool, &images, "SELECT * FROM public.miogest_images_ref")
if err != nil && err != pgx.ErrNoRows {
return nil, fmt.Errorf("failed to get all images: %w", err)
}
return images, nil
}
func ImagesBatchInsert(annunci *typedefs.AnnunciXML) error {
batch := &pgx.Batch{}
for _, annuncio := range annunci.Annuncio {
batch.Queue(`
INSERT INTO public.miogest_images_ref (codice, foto)
VALUES ($1, $2)
ON CONFLICT (codice) DO UPDATE SET foto = $2
`, &annuncio.Codice, &annuncio.Foto)
}
if err := Db.Dbpool.SendBatch(Db.Ctx, batch).Close(); err != nil {
return fmt.Errorf("failed to insert/update images batch: %w", err)
}
return nil
}
func ImagesClear() error {
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.miogest_images_ref")
if err != nil {
return fmt.Errorf("failed to clear images: %w", err)
}
return nil
}