feat: update GetAnnuncioParsed and ParseAnnunci methods for improved filtering and error handling

This commit is contained in:
Marco Pedone 2025-10-15 14:52:30 +02:00
parent 4524f1b4bc
commit 8b2de9472e
3 changed files with 60 additions and 43 deletions

View file

@ -102,39 +102,32 @@ func (m *MiogestHandler) GetAnnunciParsed(i *ImageRefManager) typesdefs.AnnunciP
}
// versione per singolo annuncio
func (m *MiogestHandler) GetAnnuncioParsed(cod string, i *ImageRefManager) typesdefs.AnnuncioParsed {
data := m.ParseAnnunci2(cod, i)
func (m *MiogestHandler) GetAnnuncioParsed(codFilter string, i *ImageRefManager) typesdefs.AnnunciParsed {
data := m.ParseAnnunci(codFilter, i)
if len(data.Annuncio) == 0 {
return typesdefs.AnnuncioParsed{}
}
return data.Annuncio[0]
}
func (m *MiogestHandler) ParseAnnunci2(codFilter string, i *ImageRefManager) typesdefs.AnnunciParsed {
annunci := m.GetAnnunci()
if codFilter != "" {
if len(annunci.Annuncio) == 0 {
return typesdefs.AnnunciParsed{}
}
if slices.ContainsFunc(annunci.Annuncio, func(a typesdefs.AnnuncioXML) bool {
return data
return *a.Codice == codFilter
}) {
annunci.Annuncio = []typesdefs.AnnuncioXML{annunci.Annuncio[slices.IndexFunc(annunci.Annuncio, func(a typesdefs.AnnuncioXML) bool {
return *a.Codice == codFilter
})]}
//go on
} else {
return typesdefs.AnnunciParsed{}
}
}
return typesdefs.AnnunciParsed{}
}
func (m *MiogestHandler) ParseAnnunci(codFilter string, i *ImageRefManager) typesdefs.AnnunciParsed {
annunci := m.GetAnnunci()
if len(annunci.Annuncio) == 0 {
return typesdefs.AnnunciParsed{}
}
if codFilter != "" {
idx := slices.IndexFunc(annunci.Annuncio, func(a typesdefs.AnnuncioXML) bool {
return *a.Codice == codFilter
})
if idx == -1 {
return typesdefs.AnnunciParsed{}
}
annunci.Annuncio = []typesdefs.AnnuncioXML{annunci.Annuncio[idx]}
}
image_codes_to_process := []string{}
if config.Cfg.Images.Enabled {
var err error
@ -142,16 +135,26 @@ func (m *MiogestHandler) ParseAnnunci(codFilter string, i *ImageRefManager) type
if err != nil {
log.Fatalf("Failed to find images to update: %v", err.Error())
}
if codFilter == "" {
// if processing all, also check for missing folders
missing_codes, err := i.MissingImageFolders()
if err != nil {
log.Fatalf("Failed to find missing image folders: %v", err.Error())
}
if len(missing_codes) > 0 {
image_codes_to_process = append(image_codes_to_process, missing_codes...)
image_codes_to_process = slices.Compact(image_codes_to_process)
}
// Clear images folder of codes that need to be updated
} else {
// if processing single codice, ensure it's in the list
if !slices.Contains(image_codes_to_process, codFilter) {
image_codes_to_process = append(image_codes_to_process, codFilter)
}
}
// Clears images folder of codes that need to be updated
if err := i.ClearImages(image_codes_to_process); err != nil {
log.Fatalf("Failed to clear images: %v", err.Error())
}
@ -284,9 +287,17 @@ func extractData_update(annuncio *typesdefs.AnnuncioXML, updateImages bool, m *M
folders := utils.GetPathDirsArray(filepath.Join(config.Cfg.Images.Path, tmp.Codice))
for _, folder := range folders {
folderpath := filepath.Join(filepath.Join(config.Cfg.Images.Path, tmp.Codice), folder)
exists, err := utils.PathExists(folderpath)
if err != nil {
log.Fatalf("Failed to check if folder exists: %v", err)
}
if !exists {
// something went wrong, skip
continue
}
files, err := os.ReadDir(folderpath)
if err != nil {
panic(err)
log.Fatalf("Failed to read dir: %v", err)
}
for _, file := range files {
if strings.Contains(file.Name(), "webp") && !strings.HasPrefix(file.Name(), "thumbnail") {

View file

@ -18,14 +18,16 @@ func AnnunciGetActive() ([]string, error) {
return annunci, nil
}
func AnnunciInsert(annunci typesdefs.AnnunciParsed) error {
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'")
_, 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")

View file

@ -124,7 +124,7 @@ func (s *Server) SetupRoutes() *echo.Echo {
})
e.GET("/update", func(c echo.Context) error {
var data = s.miogest.GetAnnunciParsed(s.imageManager)
err := queries.AnnunciInsert(data)
err := queries.AnnunciInsert(data, true)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
@ -134,7 +134,11 @@ func (s *Server) SetupRoutes() *echo.Echo {
cod := c.Param("cod")
var _ = s.miogest.GetAnnuncioParsed(cod, s.imageManager)
var data = s.miogest.GetAnnuncioParsed(cod, s.imageManager)
err := queries.AnnunciInsert(data, false)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, "Updated")
})