From 4298ce49c5e471376fa58f3881e55168e9bef017 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Mon, 13 Oct 2025 11:28:21 +0200 Subject: [PATCH] feat(refactor): adding database package main code --- cmd/api/database.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 cmd/api/database.go diff --git a/cmd/api/database.go b/cmd/api/database.go new file mode 100644 index 0000000..c0bf61b --- /dev/null +++ b/cmd/api/database.go @@ -0,0 +1,58 @@ +package database + +import ( + "database/sql" + "fmt" + + "forge.cadoles.com/LaCanne/realZ/internal/config" + _ "github.com/lib/pq" // Driver PostgreSQL +) + +// Service holds the database connection pool. +type Service struct { + db *sql.DB +} + +// NewService initializes and returns a new database service. +func NewService(cfg *config.DatabaseConfig) (*Service, error) { + connStr := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%d sslmode=%s", + cfg.User, cfg.Password, cfg.Dbname, + cfg.Host, cfg.Port, cfg.Sslmode, + ) + + db, err := sql.Open("postgres", connStr) + if err != nil { + return nil, fmt.Errorf("could not open database connection: %w", err) + } + + if err = db.Ping(); err != nil { + db.Close() // Close connection on ping failure + return nil, fmt.Errorf("could not ping database: %w", err) + } + + return &Service{db: db}, nil +} + +// Close closes the database connection. +func (s *Service) Close() error { + return s.db.Close() +} + +// QueryOrthometricCorrection queries the database for the orthometric correction for given coordinates. +func (s *Service) QueryOrthometricCorrection(lat, lon float64, srcProj, dstProj int) (float64, error) { + var correction float64 + + query := ` + SELECT ST_Value(rast, ST_Transform(ST_SetSRID(ST_MakePoint($2, $1), $3::integer), $4::integer)) AS pixel_value + FROM raf20lamber93 + WHERE ST_Intersects(rast, ST_Transform(ST_SetSRID(ST_MakePoint($2, $1), $3::integer), $4::integer)); + ` + + // QueryRow is ideal as we expect at most one row. + err := s.db.QueryRow(query, lat, lon, srcProj, dstProj).Scan(&correction) + if err != nil { + // If no rows are found, Scan will return sql.ErrNoRows. + return 0, err + } + return correction, nil +}