infoalloggi-monorepo/apps/backend/queries/images.go
Marco Pedone 2725098203 Refactor database handling and queries
- Updated DbHandler to accept configuration parameters instead of using environment variables directly.
- Moved database queries from handler.go to a dedicated queries package for better organization and separation of concerns.
- Implemented error handling in query functions to provide more informative error messages.
- Refactored image handling to utilize the new queries package for database interactions.
- Cleaned up unused code and comments across various files.
- Improved overall code readability and maintainability.
2025-08-22 10:11:07 +02:00

44 lines
1.1 KiB
Go

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
}