2025-08-22 10:11:07 +02:00
|
|
|
package queries
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-22 12:57:33 +02:00
|
|
|
"backend/typesdefs"
|
2025-08-22 10:11:07 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
func ImagesGetAll() ([]typesdefs.MiogestImagesRef_Table, error) {
|
|
|
|
|
var images []typesdefs.MiogestImagesRef_Table
|
2025-08-22 10:11:07 +02:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
func ImagesBatchInsert(annunci *typesdefs.AnnunciXML) error {
|
2025-08-22 10:11:07 +02:00
|
|
|
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
|
|
|
|
|
}
|
2025-10-15 14:27:06 +02:00
|
|
|
|
|
|
|
|
func ImagesClearByCodice(codice string) error {
|
|
|
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.miogest_images_ref WHERE codice = $1", codice)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to clear images by codice: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|