oef/orm/category.go

129 lines
3 KiB
Go
Raw Normal View History

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
}
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)
// 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
}
category, err = CreateCategory(db, category)
2019-12-09 08:27:46 +01:00
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) {
2019-12-09 08:27:46 +01:00
var category Category
id := args["id"]
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
}
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
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
}
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" {
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)
// 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 {
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
}
_, err = SaveCategory(db, category)
2019-12-09 08:27:46 +01:00
if err != nil {
return nil, err
}
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
}
}
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
}
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
}
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
}
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
}