Init simple Go React blog with Goxygen
This commit is contained in:
parent
634a24fe19
commit
1ada5abed1
31 changed files with 873 additions and 0 deletions
36
simple-blog/server/db/db.go
Normal file
36
simple-blog/server/db/db.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue