ovpn-certman/services/db.go

107 lines
2.5 KiB
Go
Raw Normal View History

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")
)
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 {
2018-02-01 03:30:00 +01:00
gorm *gorm.DB
2018-01-26 14:49:03 +01:00
conf *DBConfig
}
2018-01-26 14:49:03 +01:00
func NewDB(conf *DBConfig) *DB {
2018-01-26 14:49:03 +01:00
// Establish connection
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{})
db.LogMode(conf.Log)
2018-01-26 14:49:03 +01:00
return &DB{
2018-02-01 03:30:00 +01:00
gorm: 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) {
2018-02-01 03:30:00 +01:00
var count uint
err := db.gorm.Find(&models.User{}).Count(&count).Error
return count, err
2018-01-29 09:18:19 +01:00
}
// CreateUser inserts a user into the datastore
2018-02-01 03:30:00 +01:00
func (db *DB) CreateUser(user *models.User) error {
err := db.gorm.Create(&user).Error
return err
2018-01-29 09:18:19 +01:00
}
// 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)
2018-02-01 03:30:00 +01:00
err := db.gorm.Find(&users).Limit(count).Offset(offset).Error
return users, err
2018-01-29 09:18:19 +01:00
}
// GetUserByID returns a single user by ID
func (db *DB) GetUserByID(id uint) (*models.User, error) {
2018-02-01 03:30:00 +01:00
var user models.User
err := db.gorm.Where("id = ?", id).First(&user).Error
return &user, err
2018-01-29 09:18:19 +01:00
}
// GetUserByEmail returns a single user by email
func (db *DB) GetUserByEmail(email string) (*models.User, error) {
2018-02-01 03:30:00 +01:00
var user models.User
err := db.gorm.Where("email = ?", email).First(&user).Error
return &user, err
2018-01-29 09:18:19 +01:00
}
// DeleteUser removes a user from the datastore
func (db *DB) DeleteUser(id uint) error {
2018-02-01 03:30:00 +01:00
var user models.User
err := db.gorm.Where("id = ?", id).Delete(&user).Error
return err
}
// CreatePasswordReset creates a new password reset token
func (db *DB) CreatePasswordReset(pwReset *models.PasswordReset) error {
err := db.gorm.Create(&pwReset).Error
return err
}
// GetPasswordResetByToken retrieves a PasswordReset by token
func (db *DB) GetPasswordResetByToken(token string) (*models.PasswordReset, error) {
var pwReset models.PasswordReset
err := db.gorm.Where("token = ?", token).First(&pwReset).Error
return &pwReset, err
}
// DeletePasswordResetsByUserID deletes all pending password resets for a user
func (db *DB) DeletePasswordResetsByUserID(uid uint) error {
err := db.gorm.Where("user_id = ?", uid).Delete(&models.PasswordReset{}).Error
return err
2018-01-29 09:18:19 +01:00
}