131 lines
3.1 KiB
Go
131 lines
3.1 KiB
Go
package orm
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"path"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"git.andreafazzi.eu/andrea/oef/config"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/jinzhu/inflection"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
)
|
|
|
|
type IDer interface {
|
|
GetID() uint
|
|
}
|
|
|
|
type GetFn func(*Database, map[string]string, http.ResponseWriter, *http.Request) (interface{}, error)
|
|
|
|
type Database struct {
|
|
Config *config.ConfigT
|
|
|
|
models []interface{}
|
|
_db *gorm.DB
|
|
fns map[string]func(*Database, map[string]string, http.ResponseWriter, *http.Request) (interface{}, error)
|
|
}
|
|
|
|
func NewDatabase(config *config.ConfigT, models []interface{}) (*Database, error) {
|
|
var err error
|
|
|
|
db := new(Database)
|
|
|
|
db.fns = make(map[string]func(*Database, map[string]string, http.ResponseWriter, *http.Request) (interface{}, error), 0)
|
|
db.mapHandlers(models)
|
|
|
|
db._db, err = gorm.Open("mysql", fmt.Sprintf("%s?%s", config.Orm.Connection, config.Orm.Options))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return db, err
|
|
}
|
|
|
|
func (db *Database) AutoMigrate() {
|
|
if err := db._db.AutoMigrate(db.models...).Error; err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
}
|
|
|
|
func (db *Database) GetUser(username, password string) (*User, error) {
|
|
var user *User
|
|
if err := db._db.Where("username = ? AND password = ?", username, password).First(&user).Error; err != nil {
|
|
return nil, errors.New("Authentication failed!")
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func (db *Database) DB() *gorm.DB {
|
|
return db._db
|
|
}
|
|
|
|
func CreateCategories(db *Database) {
|
|
for _, name := range categories {
|
|
var category Category
|
|
if err := db._db.FirstOrCreate(&category, Category{Name: name}).Error; err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (db *Database) mapHandlers(models []interface{}) error {
|
|
for _, model := range models {
|
|
name := inflection.Plural(strings.ToLower(ModelName(model)))
|
|
for p, action := range map[string]string{
|
|
"": "ReadAll",
|
|
"create/": "Create",
|
|
"{id}": "Read",
|
|
"{id}/update": "Update",
|
|
"{id}/delete": "Delete",
|
|
} {
|
|
method := reflect.ValueOf(model).MethodByName(action)
|
|
if !method.IsValid() {
|
|
return fmt.Errorf("Action %s is not defined for model %s", action, name)
|
|
}
|
|
joinedPath := path.Join("/", name, p)
|
|
if strings.HasSuffix(p, "/") {
|
|
joinedPath += "/"
|
|
}
|
|
db.fns[joinedPath] = method.Interface().(func(*Database, map[string]string, http.ResponseWriter, *http.Request) (interface{}, error))
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (db *Database) GetFunc(path string) (GetFn, error) {
|
|
fn, ok := db.fns[path]
|
|
if !ok {
|
|
return nil, fmt.Errorf("Can't map path %s to any model methods.", path)
|
|
}
|
|
return fn, nil
|
|
}
|
|
|
|
func GetNothing(db *Database, args map[string]string) (interface{}, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func PostNothing(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (IDer, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func ModelName(s interface{}) string {
|
|
t := reflect.TypeOf(s)
|
|
switch t.Kind() {
|
|
case reflect.Ptr:
|
|
elem := t.Elem()
|
|
if strings.Contains(elem.String(), "[]") {
|
|
return strings.Replace(elem.String(), "[]*orm.", "", -1)
|
|
}
|
|
return elem.Name()
|
|
case reflect.Slice:
|
|
return strings.Replace(t.Elem().String(), "*orm.", "", -1)
|
|
default:
|
|
return t.Name()
|
|
}
|
|
}
|