Also formatted the logging, for now its good, we will add analytics to it later tho
48 lines
736 B
Go
48 lines
736 B
Go
package models
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"log"
|
|
"thermalFax/config"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
type Model struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
type DBAccess interface {
|
|
openDB() (*sql.DB, error)
|
|
}
|
|
type DB struct{}
|
|
|
|
func NewModel(dacc DBAccess) (*Model, error) {
|
|
m := new(Model)
|
|
|
|
// Try to load the DB
|
|
db, err := dacc.openDB()
|
|
if err != nil {
|
|
log.Println("failed to create new model instance: ", err)
|
|
return nil, errors.New("failed to create model instance")
|
|
}
|
|
|
|
// Store the values
|
|
m.DB = db
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (d *DB) openDB() (*sql.DB, error) {
|
|
var c *config.Configuration
|
|
cfg, _ := c.GetInstance("", nil)
|
|
|
|
db, err := sql.Open("sqlite3", cfg.DBPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db, nil
|
|
}
|