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 {
|
2018-02-01 03:30:00 +01:00
|
|
|
gorm *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-02-01 09:31:06 +01:00
|
|
|
db.AutoMigrate(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{
|
2018-02-01 03:30:00 +01:00
|
|
|
gorm: db,
|
2018-01-29 16:52:59 +01:00
|
|
|
conf: conf,
|
|
|
|
}
|
2018-01-26 14:49:03 +01:00
|
|
|
}
|
2018-01-29 09:18:19 +01:00
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
// CountClients returns the number of clients in the datastore
|
|
|
|
func (db *DB) CountClients() (uint, error) {
|
2018-02-01 03:30:00 +01:00
|
|
|
var count uint
|
2018-02-01 09:31:06 +01:00
|
|
|
err := db.gorm.Find(&models.Client{}).Count(&count).Error
|
2018-02-01 03:30:00 +01:00
|
|
|
return count, err
|
2018-01-29 09:18:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
// CreateClient inserts a client into the datastore
|
|
|
|
func (db *DB) CreateClient(client *models.Client) error {
|
|
|
|
err := db.gorm.Create(&client).Error
|
2018-02-01 03:30:00 +01:00
|
|
|
return err
|
2018-01-29 09:18:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
// ListClients returns a slice of 'count' client, starting at 'offset'
|
|
|
|
func (db *DB) ListClients(count, offset int) ([]*models.Client, error) {
|
|
|
|
var clients = make([]*models.Client, 0)
|
2018-01-29 09:18:19 +01:00
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
err := db.gorm.Find(&clients).Limit(count).Offset(offset).Error
|
2018-02-01 03:30:00 +01:00
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
return clients, err
|
2018-01-29 09:18:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
// ListClientsForUser returns a slice of 'count' client for user 'user', starting at 'offset'
|
|
|
|
func (db *DB) ListClientsForUser(user string, count, offset int) ([]*models.Client, error) {
|
|
|
|
var clients = make([]*models.Client, 0)
|
2018-01-29 09:18:19 +01:00
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
err := db.gorm.Find(&clients).Where("user = ?", user).Limit(count).Offset(offset).Error
|
2018-01-29 09:18:19 +01:00
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
return clients, err
|
2018-02-01 03:30:00 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
// GetClientByID returns a single client by ID
|
|
|
|
func (db *DB) GetClientByID(id uint) (*models.Client, error) {
|
|
|
|
var client models.Client
|
|
|
|
err := db.gorm.Where("id = ?", id).First(&client).Error
|
|
|
|
return &client, err
|
2018-02-01 03:30:00 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
// GetClientByNameUser returns a single client by ID
|
|
|
|
func (db *DB) GetClientByNameUser(name, user string) (*models.Client, error) {
|
|
|
|
var client models.Client
|
|
|
|
err := db.gorm.Where("name = ?", name).Where("user = ?", user).First(&client).Error
|
|
|
|
return &client, err
|
2018-02-01 03:30:00 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
// DeleteClient removes a client from the datastore
|
|
|
|
func (db *DB) DeleteClient(id uint) error {
|
|
|
|
err := db.gorm.Where("id = ?", id).Delete(&models.Client{}).Error
|
2018-02-01 03:30:00 +01:00
|
|
|
return err
|
2018-01-29 09:18:19 +01:00
|
|
|
}
|