infoalloggi-monorepo/apps/backend/queries/annunci.go
Marco Pedone 8fe0b6636d Refactor TypeScript schemas and enums for consistency and readability
- Updated enum definitions in PaymentStatusEnum and TipologiaPosizioneEnum to use single quotes and consistent formatting.
- Refactored Payments, Prezziario, Ratelimiter, Servizio, and other schema files to ensure consistent indentation and formatting.
- Added `updated_at` field to various queries in interests and servizio controllers.
- Removed unused imageCache utility file.
2025-10-21 18:42:03 +02:00

281 lines
8.2 KiB
Go

package queries
import (
"backend/typesdefs"
"fmt"
"slices"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5"
)
func AnnunciGetActive() ([]string, error) {
var annunci []string
err := pgxscan.Select(Db.Ctx, Db.Dbpool, &annunci, "SELECT codice FROM public.annunci WHERE web = true and stato = 'Attivo' ")
if err != nil && err != pgx.ErrNoRows {
return nil, fmt.Errorf("failed to get active annunci: %w", err)
}
return annunci, nil
}
func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool) error {
var err error
if resetAllBeforeUpdate {
//SET ALL WEB TO FALSE
_, err = Db.Dbpool.Exec(Db.Ctx, "UPDATE public.annunci SET web = false, homepage = false, stato='Sospeso'")
if err != nil {
return fmt.Errorf("failed to set all web to false: %w", err)
}
}
//GET ALL CODICI
var codici []string
err = pgxscan.Select(Db.Ctx, Db.Dbpool, &codici, "SELECT DISTINCT codice FROM public.annunci")
if err != nil {
return fmt.Errorf("failed to get all codici: %w", err)
}
batch := &pgx.Batch{}
for _, annuncio := range annunci.Annuncio {
if slices.Contains(codici, annuncio.Codice) {
batch.Queue(`
UPDATE public.annunci
SET locatore = $1,
numero = $2,
idlocatore = $3,
email = $4,
creato_il = $5,
modificato_il = $6,
indirizzo = $7,
civico = $8,
comune = $9,
cap = $10,
provincia = $11,
regione = $12,
lat = $13,
lon = $14,
indirizzo_secondario = $15,
civico_secondario = $16,
lat_secondario = $17,
lon_secondario = $18,
tipo = $19,
categorie = $20,
prezzo = $21,
anno = $22,
consegna = $23,
classe = $24,
mq = $25,
piano = $26,
piano_palazzo = $27,
unita_condominio = $28,
numero_vani = $29,
numero_camere = $30,
numero_bagni = $31,
numero_balconi = $32,
numero_terrazzi = $33,
numero_box = $34,
numero_postiauto = $35,
caratteristiche = $36,
accessori = $37,
titolo_it = $38,
desc_it = $39,
titolo_en = $40,
desc_en = $41,
stato = $42,
web = $43,
homepage = $44,
url_immagini = $45,
url_video = $46,
og_url = $48,
disponibile_da = $49,
permanenza = $50,
persone = $51,
updated_at = NOW()
WHERE codice = $47
`, annuncio.Locatore, annuncio.Numero, annuncio.Idlocatore,
annuncio.Email, annuncio.Creato_il, annuncio.Modificato_il,
annuncio.Indirizzo, annuncio.Civico, annuncio.Comune, annuncio.Cap,
annuncio.Provincia, annuncio.Regione, annuncio.Lat, annuncio.Lon,
annuncio.Indirizzo_secondario, annuncio.Civico_secondario, annuncio.Lat_secondario,
annuncio.Lon_secondario, annuncio.Tipo, annuncio.Categorie, annuncio.Prezzo, annuncio.Anno,
annuncio.Consegna, annuncio.Classe, annuncio.Mq, annuncio.Piano, annuncio.Piano_palazzo,
annuncio.Unita_condominio, annuncio.Numero_vani, annuncio.Numero_camere, annuncio.Numero_bagni,
annuncio.Numero_balconi, annuncio.Numero_terrazzi, annuncio.Numero_box, annuncio.Numero_postiauto,
annuncio.Caratteristiche, annuncio.Accessori, annuncio.Titolo_it, annuncio.Desc_it, annuncio.Titolo_en,
annuncio.Desc_en, annuncio.Stato, annuncio.Web, annuncio.Homepage, annuncio.Url_foto,
annuncio.Url_video, annuncio.Codice, annuncio.Og_url, annuncio.Disponibile_da, annuncio.Permanenza, annuncio.Persone)
} else {
batch.Queue(`
INSERT INTO public.annunci(
locatore,
numero,
idlocatore,
email,
creato_il,
modificato_il,
indirizzo,
civico,
comune,
cap,
provincia,
regione,
lat,
lon,
indirizzo_secondario,
civico_secondario,
lat_secondario,
lon_secondario,
tipo,
categorie,
prezzo,
anno,
consegna,
classe,
mq,
piano,
piano_palazzo,
unita_condominio,
numero_vani,
numero_camere,
numero_bagni,
numero_balconi,
numero_terrazzi,
numero_box,
numero_postiauto,
caratteristiche,
accessori,
titolo_it,
desc_it,
titolo_en,
desc_en,
stato,
web,
homepage,
url_immagini,
url_video,
codice,
og_url,
disponibile_da,
permanenza,
persone,
updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47, $48, $49, $50, $51, NOW())
`, annuncio.Locatore, annuncio.Numero, annuncio.Idlocatore,
annuncio.Email, annuncio.Creato_il, annuncio.Modificato_il,
annuncio.Indirizzo, annuncio.Civico, annuncio.Comune, annuncio.Cap,
annuncio.Provincia, annuncio.Regione, annuncio.Lat, annuncio.Lon,
annuncio.Indirizzo_secondario, annuncio.Civico_secondario, annuncio.Lat_secondario,
annuncio.Lon_secondario, annuncio.Tipo, annuncio.Categorie, annuncio.Prezzo, annuncio.Anno,
annuncio.Consegna, annuncio.Classe, annuncio.Mq, annuncio.Piano, annuncio.Piano_palazzo,
annuncio.Unita_condominio, annuncio.Numero_vani, annuncio.Numero_camere, annuncio.Numero_bagni,
annuncio.Numero_balconi, annuncio.Numero_terrazzi, annuncio.Numero_box, annuncio.Numero_postiauto,
annuncio.Caratteristiche, annuncio.Accessori, annuncio.Titolo_it, annuncio.Desc_it, annuncio.Titolo_en,
annuncio.Desc_en, annuncio.Stato, annuncio.Web, annuncio.Homepage, annuncio.Url_foto,
annuncio.Url_video, annuncio.Codice, annuncio.Og_url, annuncio.Disponibile_da, annuncio.Permanenza, annuncio.Persone)
}
}
err = Db.Dbpool.SendBatch(Db.Ctx, batch).Close()
if err != nil {
return fmt.Errorf("failed to insert/update annunci batch: %w", err)
}
fmt.Println("Annunci insert/update done")
return nil
}
func clearRecords() {
//SET ALL WEB TO FALSE
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.annunci")
if err != nil {
panic(err)
}
}
func SetFromBkp(annunci typesdefs.AnnunciParsed) error {
clearRecords()
batch := &pgx.Batch{}
for _, annuncio := range annunci.Annuncio {
batch.Queue(`
INSERT INTO public.annunci(
locatore,
numero,
idlocatore,
email,
creato_il,
modificato_il,
indirizzo,
civico,
comune,
cap,
provincia,
regione,
lat,
lon,
indirizzo_secondario,
civico_secondario,
lat_secondario,
lon_secondario,
tipo,
categorie,
prezzo,
anno,
consegna,
classe,
mq,
piano,
piano_palazzo,
unita_condominio,
numero_vani,
numero_camere,
numero_bagni,
numero_balconi,
numero_terrazzi,
numero_box,
numero_postiauto,
caratteristiche,
accessori,
titolo_it,
desc_it,
titolo_en,
desc_en,
stato,
web,
homepage,
url_immagini,
url_video,
codice
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47)
`, annuncio.Locatore, annuncio.Numero, annuncio.Idlocatore,
annuncio.Email, annuncio.Creato_il, annuncio.Modificato_il,
annuncio.Indirizzo, annuncio.Civico, annuncio.Comune, annuncio.Cap,
annuncio.Provincia, annuncio.Regione, annuncio.Lat, annuncio.Lon,
annuncio.Indirizzo_secondario, annuncio.Civico_secondario, annuncio.Lat_secondario,
annuncio.Lon_secondario, annuncio.Tipo, annuncio.Categorie, annuncio.Prezzo, annuncio.Anno,
annuncio.Consegna, annuncio.Classe, annuncio.Mq, annuncio.Piano, annuncio.Piano_palazzo,
annuncio.Unita_condominio, annuncio.Numero_vani, annuncio.Numero_camere, annuncio.Numero_bagni,
annuncio.Numero_balconi, annuncio.Numero_terrazzi, annuncio.Numero_box, annuncio.Numero_postiauto,
annuncio.Caratteristiche, annuncio.Accessori, annuncio.Titolo_it, annuncio.Desc_it, annuncio.Titolo_en,
annuncio.Desc_en, annuncio.Stato, annuncio.Web, annuncio.Homepage, annuncio.Url_foto,
annuncio.Url_video, annuncio.Codice)
}
err := Db.Dbpool.SendBatch(Db.Ctx, batch).Close()
if err != nil {
return fmt.Errorf("failed to insert/update annunci batch: %w", err)
}
fmt.Println("Annunci insert/update done")
return nil
}
func AnnunciResetImages() error {
_, err := Db.Dbpool.Exec(Db.Ctx, "UPDATE public.annunci SET url_immagini = NULL, url_video = NULL")
if err != nil {
return fmt.Errorf("failed to reset images: %w", err)
}
return nil
}