- Updated CardAnnuncio and related components to use a new images structure instead of url_immagini. - Removed references to url_immagini and replaced them with images array containing img and thumb properties. - Adjusted API responses to include images in the new format. - Modified middleware and storage service to accommodate new image handling. - Cleaned up unused code and schemas related to old image references. - Updated Docker configuration for storage service.
53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package queries
|
|
|
|
import (
|
|
"backend/typesdefs"
|
|
"fmt"
|
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
func SetImagesToDB(pool *[]typesdefs.UploadedImage) error {
|
|
batch := &pgx.Batch{}
|
|
|
|
for _, data := range *pool {
|
|
batch.Queue(`
|
|
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
|
|
`, data.Codice, data.Order, data.ImgId, data.ThumbId, data.OGUrl)
|
|
}
|
|
|
|
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 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)
|
|
}
|
|
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
|
|
}
|
|
|
|
func ClearImagesRefsByCodice(codice string) error {
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.images_refs WHERE codice = $1", codice)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to clear images refs by codice: %w", err)
|
|
}
|
|
return nil
|
|
}
|