feat: add DbHandler and helpers for database operations with logging and utility functions
This commit is contained in:
parent
0837f447fc
commit
080002f4ae
3 changed files with 8 additions and 16 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
package main
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"backend/dbhelpers"
|
|
||||||
"backend/typedefs"
|
"backend/typedefs"
|
||||||
|
"backend/utils"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
@ -35,7 +35,7 @@ func NewDbHandler() *DbHandler {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
config.ConnConfig.Tracer = dbhelpers.NewMultiQueryTracer(dbhelpers.NewLoggingQueryTracer(slog.Default()))
|
config.ConnConfig.Tracer = NewMultiQueryTracer(NewLoggingQueryTracer(slog.Default()))
|
||||||
|
|
||||||
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
||||||
pgxuuid.Register(conn.TypeMap())
|
pgxuuid.Register(conn.TypeMap())
|
||||||
|
|
@ -88,7 +88,7 @@ func (h *DbHandler) InsertAnnunciInDB(annunci typedefs.AnnunciParsed) {
|
||||||
batch := &pgx.Batch{}
|
batch := &pgx.Batch{}
|
||||||
for _, annuncio := range annunci.Annuncio {
|
for _, annuncio := range annunci.Annuncio {
|
||||||
|
|
||||||
if contains(codici, annuncio.Codice) {
|
if utils.Contains(codici, annuncio.Codice) {
|
||||||
batch.Queue(`
|
batch.Queue(`
|
||||||
UPDATE public.annunci
|
UPDATE public.annunci
|
||||||
SET locatore = $1,
|
SET locatore = $1,
|
||||||
|
|
@ -225,15 +225,6 @@ func (h *DbHandler) InsertAnnunciInDB(annunci typedefs.AnnunciParsed) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(s []string, str string) bool {
|
|
||||||
for _, v := range s {
|
|
||||||
if v == str {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *DbHandler) ClearRecords() {
|
func (h *DbHandler) ClearRecords() {
|
||||||
|
|
||||||
//SET ALL WEB TO FALSE
|
//SET ALL WEB TO FALSE
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package dbhelpers
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"backend/db"
|
||||||
storagehandlers "backend/storage_handlers"
|
storagehandlers "backend/storage_handlers"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -23,7 +24,7 @@ var (
|
||||||
imageOption bool
|
imageOption bool
|
||||||
concurrentImages int = 1
|
concurrentImages int = 1
|
||||||
storagepath string
|
storagepath string
|
||||||
Db *DbHandler
|
Db *db.DbHandler
|
||||||
Miogest *MiogestHandler
|
Miogest *MiogestHandler
|
||||||
ImageManager *ImageRefManager
|
ImageManager *ImageRefManager
|
||||||
Storage storagehandlers.StorageHandler
|
Storage storagehandlers.StorageHandler
|
||||||
|
|
@ -63,7 +64,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SETUP DATABASE HANDLER
|
// SETUP DATABASE HANDLER
|
||||||
Db = NewDbHandler()
|
Db = db.NewDbHandler()
|
||||||
if Db == nil {
|
if Db == nil {
|
||||||
log.Fatal("Error connecting to db")
|
log.Fatal("Error connecting to db")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue