2019-12-09 11:15:42 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
2020-01-10 12:07:11 +01:00
|
|
|
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",
|
2021-03-01 17:53:37 +01:00
|
|
|
"ESTERO",
|
2020-01-10 12:07:11 +01:00
|
|
|
}
|
|
|
|
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,
|
2021-03-01 17:53:37 +01:00
|
|
|
"ESTERO": 21,
|
2020-01-10 12:07:11 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-12-09 11:15:42 +01:00
|
|
|
type Region struct {
|
|
|
|
gorm.Model
|
|
|
|
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func CreateRegions(db *Database) {
|
2020-01-10 12:07:11 +01:00
|
|
|
for _, name := range regions {
|
|
|
|
var region Region
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.FirstOrCreate(®ion, Region{Name: name}).Error; err != nil {
|
2020-01-10 12:07:11 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:15:42 +01:00
|
|
|
func (model *Region) GetID() uint { return model.ID }
|
|
|
|
|
|
|
|
func (model *Region) String() string {
|
|
|
|
return model.Name
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Region) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-12-09 11:15:42 +01:00
|
|
|
if r.Method == "GET" {
|
|
|
|
region := new(Region)
|
2020-01-15 11:27:00 +01:00
|
|
|
// if err := db._db.Find(®ion.AllContests).Error; err != nil {
|
2019-12-09 11:15:42 +01:00
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
return region, nil
|
|
|
|
} else {
|
|
|
|
region := new(Region)
|
|
|
|
err := renderer.Decode(region, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
region, err = CreateRegion(db, region)
|
2019-12-09 11:15:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return region, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Region) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-12-09 11:15:42 +01:00
|
|
|
var region Region
|
|
|
|
|
|
|
|
id := args["id"]
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db. /*.Preload("Something")*/ First(®ion, id).Error; err != nil {
|
2019-12-09 11:15:42 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®ion, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Region) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-12-09 11:15:42 +01:00
|
|
|
var regions []*Region
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db. /*.Preload("Something")*/ Order("created_at").Find(®ions).Error; err != nil {
|
2019-12-09 11:15:42 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return regions, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Region) Update(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-12-09 11:15:42 +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 11:15:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
region := result.(*Region)
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
// if err := db._db.Find(®ion.AllElements).Error; err != nil {
|
2019-12-09 11:15:42 +01:00
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// region.SelectedElement = make(map[uint]string)
|
|
|
|
// region.SelectedElement[region.ElementID] = "selected"
|
|
|
|
|
|
|
|
return region, nil
|
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
region, err := model.Read(db, args, w, r)
|
2019-12-09 11:15:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = renderer.Decode(region, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
_, err = SaveRegion(db, region)
|
2019-12-09 11:15:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
region, err = model.Read(db, args, w, r)
|
2019-12-09 11:15:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return region.(*Region), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
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)
|
2019-12-09 11:15:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Unscoped().Delete(region.(*Region)).Error; err != nil {
|
2019-12-09 11:15:42 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return region.(*Region), nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func CreateRegion(db *Database, region *Region) (*Region, error) {
|
|
|
|
if err := db._db.Create(region).Error; err != nil {
|
2019-12-09 11:15:42 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return region, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func SaveRegion(db *Database, region interface{}) (interface{}, error) {
|
|
|
|
if err := db._db. /*.Omit("Something")*/ Save(region).Error; err != nil {
|
2019-12-09 11:15:42 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return region, nil
|
|
|
|
}
|