2019-11-04 15:00:46 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-03 11:14:29 +01:00
|
|
|
"git.andreafazzi.eu/andrea/oef/config"
|
|
|
|
"github.com/gorilla/sessions"
|
2019-11-04 15:00:46 +01:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/jinzhu/inflection"
|
|
|
|
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IDer interface {
|
|
|
|
GetID() uint
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:14:29 +01:00
|
|
|
type GetFn func(map[string]string, http.ResponseWriter, *http.Request) (interface{}, error)
|
2019-11-04 15:00:46 +01:00
|
|
|
|
|
|
|
var (
|
2019-12-03 11:14:29 +01:00
|
|
|
fns map[string]func(map[string]string, http.ResponseWriter, *http.Request) (interface{}, error)
|
2019-11-04 15:00:46 +01:00
|
|
|
currDB *gorm.DB
|
2019-12-03 11:14:29 +01:00
|
|
|
store = sessions.NewCookieStore([]byte(config.Config.Keys.CookieStoreKey))
|
2019-11-04 15:00:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-12-03 11:14:29 +01:00
|
|
|
fns = make(map[string]func(map[string]string, http.ResponseWriter, *http.Request) (interface{}, error), 0)
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(connection string) (*gorm.DB, error) {
|
|
|
|
db, err := gorm.Open("mysql", connection)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func AutoMigrate(models ...interface{}) {
|
|
|
|
if err := currDB.AutoMigrate(models...).Error; err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
func CreateCategories() {
|
2019-12-09 11:15:42 +01:00
|
|
|
for _, name := range categories {
|
2019-12-09 08:27:46 +01:00
|
|
|
var category Category
|
|
|
|
if err := currDB.FirstOrCreate(&category, Category{Name: name}).Error; err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 15:00:46 +01:00
|
|
|
func Use(db *gorm.DB) {
|
|
|
|
currDB = db
|
|
|
|
}
|
|
|
|
|
|
|
|
func DB() *gorm.DB {
|
|
|
|
return currDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func MapHandlers(models []interface{}) error {
|
|
|
|
for _, model := range models {
|
2020-01-02 13:01:21 +01:00
|
|
|
name := inflection.Plural(strings.ToLower(ModelName(model)))
|
2019-11-04 15:00:46 +01:00
|
|
|
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 += "/"
|
|
|
|
}
|
2019-12-03 11:14:29 +01:00
|
|
|
fns[joinedPath] = method.Interface().(func(map[string]string, http.ResponseWriter, *http.Request) (interface{}, error))
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFunc(path string) (GetFn, error) {
|
|
|
|
fn, ok := fns[path]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Can't map path %s to any model methods.", path)
|
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetNothing(args map[string]string) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:14:29 +01:00
|
|
|
func PostNothing(args map[string]string, w http.ResponseWriter, r *http.Request) (IDer, error) {
|
2019-11-04 15:00:46 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:01:21 +01:00
|
|
|
func ModelName(s interface{}) string {
|
2020-01-07 18:33:11 +01:00
|
|
|
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:
|
2019-11-04 15:00:46 +01:00
|
|
|
return t.Name()
|
|
|
|
}
|
|
|
|
}
|