Files
locative-service/src/locsrv/database/database.go
Wolfgang Hottgenroth 5546c208de
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
still database stuff, fix 3
2024-01-13 18:56:26 +01:00

53 lines
986 B
Go

package database
import (
"log"
//"time"
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type DatabaseHandle struct {
initialized bool
dbh *gorm.DB
}
func New() *DatabaseHandle {
var db DatabaseHandle
// inject the whole database configuration via the well-known PG* env variables
conn, err := gorm.Open(postgres.Open(""))
if err != nil {
log.Printf("Unable to open database connection: %s", err)
db.initialized = false
} else {
db.dbh = conn
db.initialized = true
//log.Println("Database connection opened")
}
return &db
}
func (self *DatabaseHandle) GetPersonById(id string) (string, error) {
if ! self.initialized {
err := fmt.Errorf("Database connection not initialized")
return "", err
}
var person Person
result := self.dbh.
Where("id = ?", id).
First(&person)
if result.Error != nil {
err := fmt.Errorf("Query failed: %s", result.Error)
return "", err
}
return person.Name, nil
}