infoalloggi-monorepo/apps/backend/queries/annunci.go
2026-05-26 10:42:00 +02:00

111 lines
4.2 KiB
Go

package queries
import (
models "backend/gen/postgres/postgres/public/model"
. "backend/gen/postgres/postgres/public/table"
"backend/typesdefs"
"fmt"
"log"
"time"
. "github.com/go-jet/jet/v2/postgres"
"github.com/lib/pq"
)
func AnnunciGetActive() ([]string, error) {
var annunci []string
stmt := SELECT(Annunci.Codice).FROM(Annunci).WHERE(AND(Annunci.Web.IS_TRUE(), Annunci.Stato.EQ(Text("Attivo"))))
err := stmt.Query(postgres, &annunci)
if err != nil {
return nil, fmt.Errorf("failed to get active annunci: %w", err)
}
return annunci, nil
}
func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool, isDryRun bool) error {
var err error
if resetAllBeforeUpdate {
//SET ALL WEB TO FALSE
stmt := Annunci.UPDATE(Annunci.Web, Annunci.Homepage, Annunci.Stato).SET(false, false, "Sospeso").WHERE(Annunci.Codice.IS_NOT_NULL())
if isDryRun {
log.Printf("Dry run: Setting all web to false")
} else {
_, err = stmt.Exec(postgres)
if err != nil {
return fmt.Errorf("failed to set all web to false: %w", err)
}
}
}
var to_insert []models.Annunci
for _, annuncio := range annunci.Annuncio {
value := models.Annunci{
Locatore: annuncio.Locatore,
Numero: annuncio.Numero,
Idlocatore: annuncio.Idlocatore,
Email: annuncio.Email,
CreatoIl: annuncio.Creato_il,
ModificatoIl: annuncio.Modificato_il,
Indirizzo: annuncio.Indirizzo,
Civico: annuncio.Civico,
Comune: annuncio.Comune,
Cap: annuncio.Cap,
Provincia: annuncio.Provincia,
Regione: annuncio.Regione,
Lat: annuncio.Lat,
Lon: annuncio.Lon,
IndirizzoSecondario: annuncio.Indirizzo_secondario,
CivicoSecondario: annuncio.Civico_secondario,
LatSecondario: annuncio.Lat_secondario,
LonSecondario: annuncio.Lon_secondario,
Tipo: annuncio.Tipo,
Categorie: (*pq.StringArray)(annuncio.Categorie),
Prezzo: int32(annuncio.Prezzo),
Anno: annuncio.Anno,
Consegna: intToint32Ptr(annuncio.Consegna),
Classe: annuncio.Classe,
Mq: annuncio.Mq,
Piano: annuncio.Piano,
PianoPalazzo: strPtrToInt32Ptr(annuncio.Piano_palazzo),
UnitaCondominio: strPtrToInt32Ptr(annuncio.Unita_condominio),
NumeroVani: strPtrToInt32Ptr(annuncio.Numero_vani),
NumeroCamere: strPtrToInt32Ptr(annuncio.Numero_camere),
NumeroBagni: strPtrToInt32Ptr(annuncio.Numero_bagni),
NumeroBalconi: strPtrToInt32Ptr(annuncio.Numero_balconi),
NumeroTerrazzi: strPtrToInt32Ptr(annuncio.Numero_terrazzi),
NumeroBox: strPtrToInt32Ptr(annuncio.Numero_box),
NumeroPostiauto: strPtrToInt32Ptr(annuncio.Numero_postiauto),
Caratteristiche: mapToJSONStrPtr(annuncio.Caratteristiche),
Accessori: (*pq.StringArray)(annuncio.Accessori),
TitoloIt: annuncio.Titolo_it,
DescIt: annuncio.Desc_it,
TitoloEn: annuncio.Titolo_en,
DescEn: annuncio.Desc_en,
Stato: annuncio.Stato,
Web: annuncio.Web,
Homepage: annuncio.Homepage,
ExternalVideos: (*pq.StringArray)(annuncio.External_videos),
Codice: annuncio.Codice,
DisponibileDa: strPtrToTimePtrSilent(annuncio.Disponibile_da),
Permanenza: intSliceToPQInt32Array(annuncio.Permanenza),
Persone: (*pq.StringArray)(annuncio.Persone),
TipoLocatore: annuncio.Tipo_locatore,
MediaUpdatedAt: Ptr(time.Now()),
}
to_insert = append(to_insert, value)
}
if len(to_insert) > 0 {
stmt := Annunci.INSERT(Annunci.MutableColumns).MODELS(to_insert).ON_CONFLICT(Annunci.Codice).DO_UPDATE(SET(Annunci.MutableColumns.SET(Annunci.EXCLUDED.MutableColumns)))
if isDryRun {
log.Printf("Dry run: would have inserted %d records\n", len(to_insert))
} else {
_, err = stmt.Exec(postgres)
if err != nil {
return fmt.Errorf("failed to insert/update annunci batch: %w", err)
}
}
}
fmt.Println("Annunci insert/update done")
return nil
}