147 lines
3.1 KiB
Go
147 lines
3.1 KiB
Go
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
|
|
Password string
|
|
Role string
|
|
}
|
|
|
|
func (model *User) GetID() uint { return model.ID }
|
|
|
|
func (model *User) String() string {
|
|
return "" // Please implement this.
|
|
}
|
|
|
|
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(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
if r.Method == "GET" {
|
|
user := new(User)
|
|
// if err := DB().Find(&user.AllContests).Error; err != nil {
|
|
// return nil, err
|
|
// }
|
|
return user, nil
|
|
} else {
|
|
user := new(User)
|
|
err := renderer.Decode(user, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
user, err = CreateUser(user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return user, nil
|
|
}
|
|
}
|
|
|
|
func (model *User) Read(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
var user User
|
|
|
|
id := args["id"]
|
|
|
|
if err := DB(). /*.Preload("Something")*/ First(&user, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
func (model *User) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
var users []*User
|
|
if err := DB(). /*.Preload("Something")*/ Order("created_at").Find(&users).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
func (model *User) Update(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
if r.Method == "GET" {
|
|
result, err := model.Read(args, w, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
user := result.(*User)
|
|
|
|
// if err := 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(args, w, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = renderer.Decode(user, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = SaveUser(user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
user, err = model.Read(args, w, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return user.(*User), nil
|
|
}
|
|
}
|
|
|
|
func (model *User) Delete(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
user, err := model.Read(args, w, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := DB().Unscoped().Delete(user.(*User)).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return user.(*User), nil
|
|
}
|
|
|
|
func CreateUser(user *User) (*User, error) {
|
|
if err := DB().Create(user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func SaveUser(user interface{}) (interface{}, error) {
|
|
if err := DB(). /*.Omit("Something")*/ Save(user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return user, nil
|
|
}
|