2019-12-09 08:27:46 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
2020-01-10 12:56:25 +01:00
|
|
|
var (
|
|
|
|
categories = []string{"Junior", "Senior"}
|
|
|
|
)
|
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
type Category struct {
|
|
|
|
gorm.Model
|
|
|
|
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Category) GetID() uint { return model.ID }
|
|
|
|
|
|
|
|
func (model *Category) String() string {
|
|
|
|
return model.Name
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Category) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-12-09 08:27:46 +01:00
|
|
|
if r.Method == "GET" {
|
|
|
|
category := new(Category)
|
2020-01-15 11:27:00 +01:00
|
|
|
// if err := db._db.Find(&category.AllContests).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
return category, nil
|
|
|
|
} else {
|
|
|
|
category := new(Category)
|
|
|
|
err := renderer.Decode(category, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
category, err = CreateCategory(db, category)
|
2019-12-09 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return category, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Category) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-12-09 08:27:46 +01:00
|
|
|
var category Category
|
|
|
|
|
|
|
|
id := args["id"]
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db. /*.Preload("Something")*/ First(&category, id).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &category, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Category) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-12-09 08:27:46 +01:00
|
|
|
var categories []*Category
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db. /*.Preload("Something")*/ Order("created_at").Find(&categories).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return categories, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Category) Update(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-12-09 08:27:46 +01:00
|
|
|
if r.Method == "GET" {
|
2020-01-15 11:27:00 +01:00
|
|
|
result, err := model.Read(db, args, w, r)
|
2019-12-09 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
category := result.(*Category)
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
// if err := db._db.Find(&category.AllElements).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// category.SelectedElement = make(map[uint]string)
|
|
|
|
// category.SelectedElement[category.ElementID] = "selected"
|
|
|
|
|
|
|
|
return category, nil
|
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
category, err := model.Read(db, args, w, r)
|
2019-12-09 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = renderer.Decode(category, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
_, err = SaveCategory(db, category)
|
2019-12-09 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
category, err = model.Read(db, args, w, r)
|
2019-12-09 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return category.(*Category), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Category) Delete(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
|
|
category, err := model.Read(db, args, w, r)
|
2019-12-09 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Unscoped().Delete(category.(*Category)).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return category.(*Category), nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func CreateCategory(db *Database, category *Category) (*Category, error) {
|
|
|
|
if err := db._db.Create(category).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return category, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func SaveCategory(db *Database, category interface{}) (interface{}, error) {
|
|
|
|
if err := db._db. /*.Omit("Something")*/ Save(category).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return category, nil
|
|
|
|
}
|