oef/orm/user.go

150 lines
3.3 KiB
Go
Raw Normal View History

package orm
import (
"net/http"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
"github.com/sethvargo/go-password/password"
)
type User struct {
gorm.Model
Username string
2020-01-11 08:03:56 +01:00
Password string `csv:"-"`
Role string
}
2019-12-07 08:58:30 +01:00
var (
SubscriberUser = User{Username: "subscriber", Role: "subscriber"}
AdministratorUser = User{Username: "admin", Role: "administrator"}
)
func (model *User) GetID() uint { return model.ID }
func (model *User) String() string {
2020-12-29 19:18:18 +01:00
return model.Username
}
func genPassword() (string, error) {
password, err := password.Generate(8, 2, 0, false, true)
if err != nil {
return "", err
}
return password, nil
}
func (model *User) BeforeSave(tx *gorm.DB) error {
if model.Password == "" {
password, err := genPassword()
if err != nil {
return err
}
model.Password = password
}
return nil
}
func (model *User) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
if r.Method == "GET" {
user := new(User)
return user, nil
} else {
user := new(User)
err := renderer.Decode(user, r)
if err != nil {
return nil, err
}
user, err = CreateUser(db, user)
if err != nil {
return nil, err
}
return user, nil
}
}
func (model *User) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
var user User
id := args["id"]
if err := db._db. /*.Preload("Something")*/ First(&user, id).Error; err != nil {
return nil, err
}
return &user, nil
}
func (model *User) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
var users []*User
if err := db._db. /*.Preload("Something")*/ Order("created_at").Find(&users).Error; err != nil {
return nil, err
}
return users, nil
}
func (model *User) Update(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
if r.Method == "GET" {
result, err := model.Read(db, args, w, r)
if err != nil {
return nil, err
}
user := result.(*User)
// if err := db._db.Find(&user.AllElements).Error; err != nil {
// return nil, err
// }
// user.SelectedElement = make(map[uint]string)
// user.SelectedElement[user.ElementID] = "selected"
return user, nil
} else {
user, err := model.Read(db, args, w, r)
if err != nil {
return nil, err
}
err = renderer.Decode(user, r)
if err != nil {
return nil, err
}
_, err = SaveUser(db, user)
if err != nil {
return nil, err
}
user, err = model.Read(db, args, w, r)
if err != nil {
return nil, err
}
return user.(*User), nil
}
}
func (model *User) Delete(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
user, err := model.Read(db, args, w, r)
if err != nil {
return nil, err
}
if err := db._db.Unscoped().Delete(user.(*User)).Error; err != nil {
return nil, err
}
return user.(*User), nil
}
func CreateUser(db *Database, user *User) (*User, error) {
if err := db._db.Create(user).Error; err != nil {
return nil, err
}
return user, nil
}
func SaveUser(db *Database, user interface{}) (interface{}, error) {
if err := db._db. /*.Omit("Something")*/ Save(user).Error; err != nil {
return nil, err
}
return user, nil
}