1
0
Fork 0
Learning-GoLang/simple-blog/server/db/db.go

36 lines
630 B
Go

package db
import (
"database/sql"
"simple-blog/model"
)
type DB interface {
GetTechnologies() ([]*model.Technology, error)
}
type PostgresDB struct {
db *sql.DB
}
func NewDB(db *sql.DB) DB {
return PostgresDB{db: db}
}
func (d PostgresDB) GetTechnologies() ([]*model.Technology, error) {
rows, err := d.db.Query("select name, details from technologies")
if err != nil {
return nil, err
}
defer rows.Close()
var tech []*model.Technology
for rows.Next() {
t := new(model.Technology)
err = rows.Scan(&t.Name, &t.Details)
if err != nil {
return nil, err
}
tech = append(tech, t)
}
return tech, nil
}