From 65e58ab2db8b46f7a726c3c78fca39beaebaaf97 Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Mon, 16 Mar 2026 10:21:21 +0100 Subject: [PATCH] refactor: remove backup handling code and related HTML view --- apps/backend/from_bkp.go | 277 ------------------- apps/backend/public/views/backup-upload.html | 59 ---- apps/backend/public/views/main.html | 18 -- apps/backend/queries/annunci.go | 77 ------ apps/backend/server.go | 43 --- apps/backend/typesdefs/models.go | 78 ------ 6 files changed, 552 deletions(-) delete mode 100644 apps/backend/from_bkp.go delete mode 100644 apps/backend/public/views/backup-upload.html diff --git a/apps/backend/from_bkp.go b/apps/backend/from_bkp.go deleted file mode 100644 index fa130be..0000000 --- a/apps/backend/from_bkp.go +++ /dev/null @@ -1,277 +0,0 @@ -package main - -import ( - "backend/parser" - "backend/typesdefs" - "backend/utils" - "encoding/xml" - "fmt" - "io" - "os" - "slices" - "strconv" - "strings" - "sync" - "time" -) - -func From_Backup() typesdefs.XmlBkp { - //open the bkp.xml file - xmlFile, err := os.Open("bkp.xml") - if err != nil { - fmt.Println(err) - } - defer xmlFile.Close() - - byteValue, err := io.ReadAll(xmlFile) - if err != nil { - fmt.Println(err) - } - var miogest typesdefs.XmlBkp - xml.Unmarshal(byteValue, &miogest) - return miogest - -} - -func ParseUpdateBkp(m *MiogestHandler) typesdefs.AnnunciParsed { - - miogest := From_Backup() - - var AnnunciArray typesdefs.AnnunciParsed - extractedData := make(chan typesdefs.AnnuncioParsed) - defaultCaratteristiche := m.GetCaratteristiche() - defaultCategorie := m.GetCategorie() - - var wg sync.WaitGroup - wg.Add(len(miogest.Annunci.Record)) - - for i := 0; i < len(miogest.Annunci.Record); i++ { - go func(i int) { - defer wg.Done() - extractData_bkp(miogest.Annunci.Record[i], extractedData, defaultCaratteristiche, defaultCategorie) - }(i) - } - - go func() { - wg.Wait() - close(extractedData) - }() - - for result := range extractedData { - AnnunciArray.Annuncio = append(AnnunciArray.Annuncio, result) - } - - return AnnunciArray -} - -func extractData_bkp(annuncio typesdefs.AnnuncioBKP, extractedData chan typesdefs.AnnuncioParsed, defaultCaratteristiche typesdefs.ListaCaratteristiche, defaultCategorie []typesdefs.Categoria) { - - var tmp typesdefs.AnnuncioParsed = typesdefs.AnnuncioParsed{} - - codice := utils.RemoveWhitespace(utils.GetStringValue(annuncio.Codice)) - tmp.Codice = codice - tmp.Email = nil - if annuncio.Proprietario != nil && annuncio.Proprietario.Cliente != nil && annuncio.Proprietario.Cliente.Nome != nil { - tmp.Locatore = annuncio.Proprietario.Cliente.Nome - } - tmp.Numero = nil - if annuncio.Proprietario != nil && annuncio.Proprietario.Cliente != nil && annuncio.Proprietario.Cliente.Nome != nil { - tmp.Idlocatore = annuncio.Proprietario.Cliente.ID - } - - upperIndirizzo := utils.MakeUppercase(utils.GetStringValue(annuncio.Indirizzo)) - tmp.Indirizzo = &upperIndirizzo - tmp.Civico = annuncio.Civico - upperComune := utils.MakeUppercase(utils.GetStringValue(annuncio.Comune)) - tmp.Comune = &upperComune - tmp.Cap = annuncio.Cap - tmp.Provincia = annuncio.Provincia - tmp.Regione = annuncio.Regione - tmp.Lat = utils.CommaToDot(annuncio.Lat) - tmp.Lon = utils.CommaToDot(annuncio.Lon) - tmp.Indirizzo_secondario = nil - tmp.Civico_secondario = nil - tmp.Lat_secondario = nil - tmp.Lon_secondario = nil - - var tipo = tipologiaParser(*annuncio.Tipo) - tmp.Tipo = &tipo - - tmpanno := utils.GetStringValue(annuncio.Anno) - if tmpanno != "0" && tmpanno != "" { - tmp.Anno = &tmpanno - } - tmp.Prezzo = parser.ParsePrezzo(annuncio.Prezzo) - tmp.Consegna = parser.ParseConsegna(annuncio.Consegna) - tmp.Classe = annuncio.Classe - - if annuncio.Categorie != nil && annuncio.Categorie.Categoria != nil { - resultArray := make([]string, 0) - cats := *annuncio.Categorie.Categoria - for _, cat := range cats { - for _, catdef := range defaultCategorie { - if cat.ID == catdef.Id { - resultArray = append(resultArray, catdef.Nome) - } - } - } - if tmp.Categorie == nil { - tmp.Categorie = &[]string{} - } - *tmp.Categorie = resultArray - } - - if annuncio.Mq != nil { - replaced := strings.ReplaceAll(*annuncio.Mq, ",", ".") - if s, err := strconv.ParseFloat(replaced, 64); err == nil { - tmp.Mq = &s - } - - } - - tmp.Piano = annuncio.Piano - tmp.Piano_palazzo = annuncio.PianoPalazzo - tmp.Unita_condominio = annuncio.UnitaImmobiliari - tmp.Numero_vani = annuncio.NumeroVani - tmp.Numero_camere = annuncio.NumeroCamere - tmp.Numero_bagni = annuncio.NumeroBagni - tmp.Numero_balconi = annuncio.NumeroBalconi - tmp.Numero_terrazzi = annuncio.NumeroTerrazzi - tmp.Numero_box = annuncio.NumeroBox - tmp.Numero_postiauto = annuncio.NumeroPostiauto - tmpstato := utils.GetStringValue(annuncio.Stato) - var tmpstatoString string - if tmpstato == "1" { - tmpstatoString = "Attivo" - } - if tmpstato == "2" { - tmpstatoString = "Trattativa" - } else { - tmpstatoString = "Sospeso" - } - tmp.Stato = &tmpstatoString - home := false - tmp.Homepage = &home - - layout := "20060102150405" - if annuncio.Inserito != nil { - t, err := time.Parse(layout, *annuncio.Inserito) - if err != nil { - tmp.Creato_il = nil - } else { - tmp.Creato_il = &t - } - } - - tmp.Modificato_il = nil - - var itlingua string = "it" - var enlingua string = "en" - var tmpTitoloIt *string = nil - var tmpTitoloEn *string = nil - var tmpDescIt *string = nil - var tmpDescEn *string = nil - - if annuncio.Descrizioni != nil { - for _, desc := range *annuncio.Descrizioni.Descrizione { - - if desc.Lingua != nil { - if *desc.Lingua == itlingua { - tmpTitoloIt = desc.Titolo - tmpDescIt = desc.Testo - } - if *desc.Lingua == enlingua { - tmpTitoloEn = desc.Titolo - tmpDescEn = desc.Testo - } - } - - } - } - - tmp.Titolo_it = tmpTitoloIt - tmp.Titolo_en = tmpTitoloEn - tmp.Desc_it = tmpDescIt - tmp.Desc_en = tmpDescEn - w := false - tmp.Web = &w - - tmp.External_videos = nil - - if annuncio.Accessori != nil && annuncio.Accessori.Accessorio != nil { - tmpAcc := make([]string, 0) - for _, acc := range *annuncio.Accessori.Accessorio { - if acc.Nome != nil { - tmpAcc = append(tmpAcc, *acc.Nome) - } - } - if tmp.Accessori == nil { - tmp.Accessori = &[]string{} - } - *tmp.Accessori = tmpAcc - } - - tmpCaratteristiche := make(map[string]interface{}) - - if annuncio.Schede != nil && annuncio.Schede.Scheda != nil { - for _, scheda := range *annuncio.Schede.Scheda { - if scheda.Valore != nil { - var cossriponding_name *string = nil - for _, def := range defaultCaratteristiche.Caratteristiche { - if scheda.ID == def.Id { - cossriponding_name = &def.Tagxml - } - } - if cossriponding_name != nil { - tmpCaratteristiche[*cossriponding_name] = *scheda.Valore - } - } - } - } - - for _, def := range defaultCaratteristiche.Caratteristiche { - - if _, ok := tmpCaratteristiche[def.Tagxml]; !ok { - tmpCaratteristiche[def.Tagxml] = nil - } - - } - tmp.Caratteristiche = &tmpCaratteristiche - - tmp.External_videos = nil - - extractedData <- tmp - -} - -func TestTipologieBkp() { - - bkp := From_Backup() - var tipologie []string - for _, r := range bkp.Annunci.Record { - if r.Tipo != nil { - if !slices.Contains(tipologie, *r.Tipo) { - tipologie = append(tipologie, *r.Tipo) - } - } - } - fmt.Println(strings.Join(tipologie, ",")) - -} - -func tipologiaParser(tipo string) string { - var result string - switch tipo { - case "Affitto Residenziale", "Affitto Turistico": - result = "Transitorio" - case "Vendita Residenziale", "Vendita Commerciale": - result = "Vendita" - case "Affitto Commerciale", "Affitto": - result = "Stabile" - case "Cessione Commerciale", "Cessione": - result = "Cessione" - default: - result = "Errore" - } - return result -} diff --git a/apps/backend/public/views/backup-upload.html b/apps/backend/public/views/backup-upload.html deleted file mode 100644 index 40f8bfd..0000000 --- a/apps/backend/public/views/backup-upload.html +++ /dev/null @@ -1,59 +0,0 @@ -{{define "backup-upload"}} - - - - - - Backup Upload - - - - -
-

Backup Upload

-
-
- - - -
-
-
- - - - -{{end}} \ No newline at end of file diff --git a/apps/backend/public/views/main.html b/apps/backend/public/views/main.html index 32f8af8..08de6b3 100644 --- a/apps/backend/public/views/main.html +++ b/apps/backend/public/views/main.html @@ -94,24 +94,6 @@ -
-
- Upload Bkp -

Carica il file di backup

-
-
- Open Bkp -

Legge il file di backup

-
-
- Parse Bkp -

Prende i dati dal file di backup e li eabora

-
-
- Set Bkp in db -

Aggiorna i dati nel database con i dati provenienti dal backup

-
-
Fetch MioGest diff --git a/apps/backend/queries/annunci.go b/apps/backend/queries/annunci.go index 215bfb7..1ccdd4e 100644 --- a/apps/backend/queries/annunci.go +++ b/apps/backend/queries/annunci.go @@ -189,80 +189,3 @@ func clearRecords() { } } - -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, - external_videos, - 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) - `, 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.External_videos, 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 -} diff --git a/apps/backend/server.go b/apps/backend/server.go index c3638fe..af8c8a2 100644 --- a/apps/backend/server.go +++ b/apps/backend/server.go @@ -212,49 +212,6 @@ func (s *Server) SetupRoutes() *echo.Echo { return c.String(http.StatusOK, "Updated") }) - // BACKUP ROUTES - /* - e.GET("/upload-backup", func(c echo.Context) error { - return c.Render(http.StatusOK, "backup-upload", nil) - }) - e.POST("/upload-backup", func(c echo.Context) error { - file, err := c.FormFile("file") - if err != nil { - return err - } - src, err := file.Open() - if err != nil { - return err - } - defer src.Close() - dst, err := os.Create("bkp.xml") - if err != nil { - return err - } - defer dst.Close() - if _, err = io.Copy(dst, src); err != nil { - return err - } - return c.HTML(http.StatusOK, fmt.Sprintf("

File %s uploaded successfully.

", file.Filename)) - }) - e.GET("/bkp", func(c echo.Context) error { - data := From_Backup() - return c.JSONPretty(http.StatusOK, data, " ") - }) - e.GET("/parsebkp", func(c echo.Context) error { - var data = ParseUpdateBkp(s.miogest) - return c.JSONPretty(http.StatusOK, data, " ") - }) - e.GET("/setbkp", func(c echo.Context) error { - var data = ParseUpdateBkp(s.miogest) - err := queries.SetFromBkp(data) - if err != nil { - return c.String(http.StatusInternalServerError, err.Error()) - } - return c.String(http.StatusOK, "Setted") - }) - */ - //IMAGE ROUTES e.GET("/initcwebp", func(c echo.Context) error { Conversion("test.jpg") diff --git a/apps/backend/typesdefs/models.go b/apps/backend/typesdefs/models.go index 9df0d6e..b6529be 100644 --- a/apps/backend/typesdefs/models.go +++ b/apps/backend/typesdefs/models.go @@ -5,84 +5,6 @@ import ( "time" ) -type XmlBkp struct { - Annunci struct { - Record []AnnuncioBKP `xml:"record"` - } `xml:"annunci"` -} - -type AnnuncioBKP struct { - Codice *string `xml:"codice"` - Proprietario *struct { - Cliente *struct { - ID *string `xml:"id"` - Nome *string `xml:"nome"` - } `xml:"cliente"` - } `xml:"proprietario"` - Nazione *string `xml:"nazione"` - Regione *string `xml:"regione"` - Provincia *string `xml:"provincia"` - Comune *string `xml:"comune"` - Cap *string `xml:"cap"` - Indirizzo *string `xml:"indirizzo"` - Civico *string `xml:"civico"` - Lat *string `xml:"lat"` - Lon *string `xml:"lon"` - Tipo *string `xml:"tipo"` - Categorie *struct { - Categoria *[]struct { - ID string `xml:"id"` - Nome string `xml:"nome"` - } `xml:"categoria"` - } `xml:"categorie"` - Prezzo *string `xml:"prezzo"` - Piani *string `xml:"piani"` - Anno *string `xml:"anno"` - Consegna *string `xml:"consegna"` - Classe *string `xml:"classe"` - Mq *string `xml:"mq"` - Piano *string `xml:"piano"` - PianoPalazzo *string `xml:"piano_palazzo"` - UnitaImmobiliari *string `xml:"unita_immobiliari"` - NumeroVani *string `xml:"numero_vani"` - NumeroCamere *string `xml:"numero_camere"` - NumeroLetti *string `xml:"numero_letti"` - NumeroBagni *string `xml:"numero_bagni"` - NumeroBalconi *string `xml:"numero_balconi"` - NumeroTerrazzi *string `xml:"numero_terrazzi"` - NumeroBox *string `xml:"numero_box"` - NumeroPostiauto *string `xml:"numero_postiauto"` - Schede *struct { - Scheda *[]struct { - ID string `xml:"id"` - Nome string `xml:"nome"` - Valore *string `xml:"valore"` - } `xml:"scheda"` - } `xml:"schede"` - Accessori *struct { - Accessorio *[]struct { - ID string `xml:"id"` - Nome *string `xml:"nome"` - } `xml:"accessorio"` - } `xml:"accessori"` - Web *string `xml:"web"` - Descrizioni *struct { - Descrizione *[]struct { - Lingua *string `xml:"lingua"` - Titolo *string `xml:"titolo"` - Testo *string `xml:"testo"` - } `xml:"descrizione"` - } `xml:"descrizioni"` - Allegati *struct { - Foto *[]struct { - Nome *string `xml:"nome"` - URL *string `xml:"url"` - } `xml:"foto"` - } `xml:"allegati"` - Inserito *string `xml:"inserito"` - Stato *string `xml:"stato"` -} - type AnnunciXML struct { Annuncio []AnnuncioXML `xml:"Annuncio"` }