package orm import ( "net/http" "git.andreafazzi.eu/andrea/oef/renderer" "github.com/jinzhu/gorm" ) var ( categories = []string{"Junior", "Senior"} ) 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(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { if r.Method == "GET" { category := new(Category) // if err := db._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(db, category) if err != nil { return nil, err } return category, nil } } func (model *Category) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { var category Category id := args["id"] if err := db._db. /*.Preload("Something")*/ First(&category, id).Error; err != nil { return nil, err } return &category, nil } func (model *Category) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { var categories []*Category if err := db._db. /*.Preload("Something")*/ Order("created_at").Find(&categories).Error; err != nil { return nil, err } return categories, nil } func (model *Category) 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 } category := result.(*Category) // if err := db._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(db, args, w, r) if err != nil { return nil, err } err = renderer.Decode(category, r) if err != nil { return nil, err } _, err = SaveCategory(db, category) if err != nil { return nil, err } category, err = model.Read(db, args, w, r) if err != nil { return nil, err } return category.(*Category), nil } } 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) if err != nil { return nil, err } if err := db._db.Unscoped().Delete(category.(*Category)).Error; err != nil { return nil, err } return category.(*Category), nil } func CreateCategory(db *Database, category *Category) (*Category, error) { if err := db._db.Create(category).Error; err != nil { return nil, err } return category, nil } func SaveCategory(db *Database, category interface{}) (interface{}, error) { if err := db._db. /*.Omit("Something")*/ Save(category).Error; err != nil { return nil, err } return category, nil }