2018-01-26 14:49:03 +01:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2018-01-29 09:18:19 +01:00
|
|
|
"errors"
|
2018-01-26 14:49:03 +01:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"git.klink.asia/paul/certman/models"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
2018-01-29 09:18:19 +01:00
|
|
|
// Error Definitions
|
|
|
|
var (
|
|
|
|
ErrNotImplemented = errors.New("Not implemented")
|
|
|
|
)
|
|
|
|
|
2018-01-29 16:52:59 +01:00
|
|
|
type DBConfig struct {
|
|
|
|
Type string
|
|
|
|
DSN string
|
|
|
|
Log bool
|
|
|
|
}
|
2018-01-29 09:18:19 +01:00
|
|
|
|
|
|
|
// DB is a wrapper around gorm.DB to provide custom methods
|
|
|
|
type DB struct {
|
|
|
|
*gorm.DB
|
2018-01-26 14:49:03 +01:00
|
|
|
|
2018-01-29 16:52:59 +01:00
|
|
|
conf *DBConfig
|
|
|
|
}
|
2018-01-26 14:49:03 +01:00
|
|
|
|
2018-01-29 16:52:59 +01:00
|
|
|
func NewDB(conf *DBConfig) *DB {
|
2018-01-26 14:49:03 +01:00
|
|
|
// Establish connection
|
2018-01-29 16:52:59 +01:00
|
|
|
db, err := gorm.Open(conf.Type, conf.DSN)
|
2018-01-26 14:49:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not open database: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate models
|
2018-01-29 09:18:19 +01:00
|
|
|
db.AutoMigrate(models.User{}, models.Client{})
|
2018-01-29 16:52:59 +01:00
|
|
|
db.LogMode(conf.Log)
|
2018-01-26 14:49:03 +01:00
|
|
|
|
2018-01-29 16:52:59 +01:00
|
|
|
return &DB{
|
|
|
|
DB: db,
|
|
|
|
conf: conf,
|
|
|
|
}
|
2018-01-26 14:49:03 +01:00
|
|
|
}
|
2018-01-29 09:18:19 +01:00
|
|
|
|
|
|
|
// CountUsers returns the number of Users in the datastore
|
|
|
|
func (db *DB) CountUsers() (uint, error) {
|
|
|
|
return 0, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUser inserts a user into the datastore
|
|
|
|
func (db *DB) CreateUser(*models.User) (*models.User, error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListUsers returns a slice of 'count' users, starting at 'offset'
|
|
|
|
func (db *DB) ListUsers(count, offset int) ([]*models.User, error) {
|
|
|
|
var users = make([]*models.User, 0)
|
|
|
|
|
|
|
|
return users, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByID returns a single user by ID
|
|
|
|
func (db *DB) GetUserByID(id uint) (*models.User, error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByEmail returns a single user by email
|
|
|
|
func (db *DB) GetUserByEmail(email string) (*models.User, error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUser removes a user from the datastore
|
|
|
|
func (db *DB) DeleteUser(id uint) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|