added tests for db and user
Some checks reported errors
arbeitszeitmessung/pipeline/head Something is wrong with the build of this commit
Some checks reported errors
arbeitszeitmessung/pipeline/head Something is wrong with the build of this commit
This commit is contained in:
@@ -7,39 +7,17 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func OpenDatabase() (*sql.DB, error) {
|
||||
func OpenDatabase() (models.IDatabase, error) {
|
||||
dbHost := helper.GetEnv("POSTGRES_HOST", "localhost")
|
||||
dbName := helper.GetEnv("POSTGRES_DB", "arbeitszeitmessung")
|
||||
dbUser := helper.GetEnv("POSTGRES_API_USER", "api_nutzer")
|
||||
dbPassword := helper.GetEnv("POSTGRES_API_PASS", "password")
|
||||
|
||||
connStr := fmt.Sprintf("postgres://%s:%s@%s:5432/%s?sslmode=disable&TimeZone=Europe/Berlin", dbUser, dbPassword, dbHost, dbName)
|
||||
return sql.Open("postgres", connStr)
|
||||
}
|
||||
|
||||
func GetBookingsByCardID(db *sql.DB, card_id string) ([]models.Booking, error) {
|
||||
qStr, err := db.Prepare((`SELECT * FROM anwesenheit WHERE card_id = $1`))
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var bookings []models.Booking
|
||||
rows, err := qStr.Query(card_id)
|
||||
if err == sql.ErrNoRows {
|
||||
return bookings, err
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var booking models.Booking
|
||||
if err := rows.Scan(&booking.CounterId, &booking.Timestamp, &booking.CardUID, &booking.GeraetID, &booking.CheckInOut); err != nil {
|
||||
return bookings, err
|
||||
}
|
||||
bookings = append(bookings, booking)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return bookings, err
|
||||
}
|
||||
return bookings, nil
|
||||
defer db.Close()
|
||||
return db, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user