oef/orm/region.go

180 lines
4 KiB
Go

package orm
import (
"net/http"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
)
var (
regions = []string{
"Abruzzo",
"Basilicata",
"Calabria",
"Campania",
"Emilia-Romagna",
"Friuli-Venezia Giulia",
"Lazio",
"Liguria",
"Lombardia",
"Marche",
"Molise",
"Piemonte",
"Puglia",
"Sardegna",
"Sicilia",
"Toscana",
"Trentino-Alto Adige",
"Umbria",
"Valle d'Aosta",
"Veneto",
}
regionsMap = map[string]uint{
"Abruzzo": 1,
"Basilicata": 2,
"Calabria": 3,
"Campania": 4,
"Emilia-Romagna": 5,
"Friuli-Venezia Giulia": 6,
"Lazio": 7,
"Liguria": 8,
"Lombardia": 9,
"Marche": 10,
"Molise": 11,
"Piemonte": 12,
"Puglia": 13,
"Sardegna": 14,
"Sicilia": 15,
"Toscana": 16,
"Trentino-Alto Adige": 17,
"Umbria": 18,
"Valle d'Aosta": 19,
"Veneto": 20,
}
)
type Region struct {
gorm.Model
Name string
}
func CreateRegions(db *Database) {
for _, name := range regions {
var region Region
if err := db._db.FirstOrCreate(&region, Region{Name: name}).Error; err != nil {
panic(err)
}
}
}
func (model *Region) GetID() uint { return model.ID }
func (model *Region) String() string {
return model.Name
}
func (model *Region) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
if r.Method == "GET" {
region := new(Region)
// if err := db._db.Find(&region.AllContests).Error; err != nil {
// return nil, err
// }
return region, nil
} else {
region := new(Region)
err := renderer.Decode(region, r)
if err != nil {
return nil, err
}
region, err = CreateRegion(db, region)
if err != nil {
return nil, err
}
return region, nil
}
}
func (model *Region) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
var region Region
id := args["id"]
if err := db._db. /*.Preload("Something")*/ First(&region, id).Error; err != nil {
return nil, err
}
return &region, nil
}
func (model *Region) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
var regions []*Region
if err := db._db. /*.Preload("Something")*/ Order("created_at").Find(&regions).Error; err != nil {
return nil, err
}
return regions, nil
}
func (model *Region) 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
}
region := result.(*Region)
// if err := db._db.Find(&region.AllElements).Error; err != nil {
// return nil, err
// }
// region.SelectedElement = make(map[uint]string)
// region.SelectedElement[region.ElementID] = "selected"
return region, nil
} else {
region, err := model.Read(db, args, w, r)
if err != nil {
return nil, err
}
err = renderer.Decode(region, r)
if err != nil {
return nil, err
}
_, err = SaveRegion(db, region)
if err != nil {
return nil, err
}
region, err = model.Read(db, args, w, r)
if err != nil {
return nil, err
}
return region.(*Region), nil
}
}
func (model *Region) Delete(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
region, err := model.Read(db, args, w, r)
if err != nil {
return nil, err
}
if err := db._db.Unscoped().Delete(region.(*Region)).Error; err != nil {
return nil, err
}
return region.(*Region), nil
}
func CreateRegion(db *Database, region *Region) (*Region, error) {
if err := db._db.Create(region).Error; err != nil {
return nil, err
}
return region, nil
}
func SaveRegion(db *Database, region interface{}) (interface{}, error) {
if err := db._db. /*.Omit("Something")*/ Save(region).Error; err != nil {
return nil, err
}
return region, nil
}