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-11-18 15:20:14 +01:00
|
|
|
func SetImagesToDB(pool *[]typesdefs.UploadedMedia) error {
|
2025-08-22 10:11:07 +02:00
|
|
|
batch := &pgx.Batch{}
|
|
|
|
|
|
2025-10-27 17:58:42 +01:00
|
|
|
for _, data := range *pool {
|
2025-08-22 10:11:07 +02:00
|
|
|
batch.Queue(`
|
2025-10-27 17:58:42 +01:00
|
|
|
INSERT INTO public.images_refs (codice, ordine, img, thumb, og_url)
|
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
|
|
|
ON CONFLICT (codice, ordine) DO UPDATE SET img = $3, thumb = $4
|
2025-11-18 15:20:14 +01:00
|
|
|
`, data.Codice, data.Order, data.MediaId, data.ThumbId, data.OGUrl)
|
2025-08-22 10:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := Db.Dbpool.SendBatch(Db.Ctx, batch).Close(); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to insert/update images batch: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-27 17:58:42 +01:00
|
|
|
func GetImagesFromDB() ([]typesdefs.ImagesRefs, error) {
|
|
|
|
|
var images []typesdefs.ImagesRefs
|
|
|
|
|
err := pgxscan.Select(Db.Ctx, Db.Dbpool, &images, "SELECT * FROM public.images_refs ORDER BY ordine ASC")
|
|
|
|
|
if err != nil && err != pgx.ErrNoRows {
|
|
|
|
|
return nil, fmt.Errorf("failed to get images: %w", err)
|
2025-08-22 10:11:07 +02:00
|
|
|
}
|
2025-10-27 17:58:42 +01:00
|
|
|
return images, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetCodiceImagesFromDB(codice string) ([]typesdefs.ImagesRefs, error) {
|
|
|
|
|
var images []typesdefs.ImagesRefs
|
|
|
|
|
err := pgxscan.Select(Db.Ctx, Db.Dbpool, &images, "SELECT * FROM public.images_refs WHERE codice = $1 ORDER BY ordine ASC", codice)
|
|
|
|
|
if err != nil && err != pgx.ErrNoRows {
|
|
|
|
|
return nil, fmt.Errorf("failed to get images: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return images, nil
|
2025-08-22 10:11:07 +02:00
|
|
|
}
|
2025-10-15 14:27:06 +02:00
|
|
|
|
2025-10-27 17:58:42 +01:00
|
|
|
func ClearImagesRefsByCodice(codice string) error {
|
|
|
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.images_refs WHERE codice = $1", codice)
|
2025-10-15 14:27:06 +02:00
|
|
|
if err != nil {
|
2025-10-27 17:58:42 +01:00
|
|
|
return fmt.Errorf("failed to clear images refs by codice: %w", err)
|
2025-10-15 14:27:06 +02:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|