125 lines
2.9 KiB
Go
125 lines
2.9 KiB
Go
|
package orm
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
||
|
"github.com/jinzhu/gorm"
|
||
|
)
|
||
|
|
||
|
type Category struct {
|
||
|
gorm.Model
|
||
|
|
||
|
Name string
|
||
|
}
|
||
|
|
||
|
func (model *Category) GetID() uint { return model.ID }
|
||
|
|
||
|
func (model *Category) String() string {
|
||
|
return model.Name
|
||
|
}
|
||
|
|
||
|
func (model *Category) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||
|
if r.Method == "GET" {
|
||
|
category := new(Category)
|
||
|
// if err := DB().Find(&category.AllContests).Error; err != nil {
|
||
|
// return nil, err
|
||
|
// }
|
||
|
return category, nil
|
||
|
} else {
|
||
|
category := new(Category)
|
||
|
err := renderer.Decode(category, r)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
category, err = CreateCategory(category)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return category, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (model *Category) Read(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||
|
var category Category
|
||
|
|
||
|
id := args["id"]
|
||
|
|
||
|
if err := DB(). /*.Preload("Something")*/ First(&category, id).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &category, nil
|
||
|
}
|
||
|
|
||
|
func (model *Category) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||
|
var categories []*Category
|
||
|
if err := DB(). /*.Preload("Something")*/ Order("created_at").Find(&categories).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return categories, nil
|
||
|
}
|
||
|
|
||
|
func (model *Category) 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
|
||
|
}
|
||
|
|
||
|
category := result.(*Category)
|
||
|
|
||
|
// if err := DB().Find(&category.AllElements).Error; err != nil {
|
||
|
// return nil, err
|
||
|
// }
|
||
|
|
||
|
// category.SelectedElement = make(map[uint]string)
|
||
|
// category.SelectedElement[category.ElementID] = "selected"
|
||
|
|
||
|
return category, nil
|
||
|
} else {
|
||
|
category, err := model.Read(args, w, r)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
err = renderer.Decode(category, r)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
_, err = SaveCategory(category)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
category, err = model.Read(args, w, r)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return category.(*Category), nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (model *Category) Delete(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||
|
category, err := model.Read(args, w, r)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if err := DB().Unscoped().Delete(category.(*Category)).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return category.(*Category), nil
|
||
|
}
|
||
|
|
||
|
func CreateCategory(category *Category) (*Category, error) {
|
||
|
if err := DB().Create(category).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return category, nil
|
||
|
}
|
||
|
|
||
|
func SaveCategory(category interface{}) (interface{}, error) {
|
||
|
if err := DB(). /*.Omit("Something")*/ Save(category).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return category, nil
|
||
|
}
|