diff --git a/orm/orm.go b/orm/orm.go index 6e08b0be..17ba5d06 100644 --- a/orm/orm.go +++ b/orm/orm.go @@ -25,6 +25,30 @@ var ( fns map[string]func(map[string]string, http.ResponseWriter, *http.Request) (interface{}, error) currDB *gorm.DB store = sessions.NewCookieStore([]byte(config.Config.Keys.CookieStoreKey)) + + categories = []string{"Junior", "Senior"} + 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", + } ) func init() { @@ -47,7 +71,7 @@ func AutoMigrate(models ...interface{}) { } func CreateCategories() { - for _, name := range []string{"Junior", "Senior"} { + for _, name := range categories { var category Category if err := currDB.FirstOrCreate(&category, Category{Name: name}).Error; err != nil { panic(err) @@ -55,6 +79,15 @@ func CreateCategories() { } } +func CreateRegions() { + for _, name := range regions { + var region Region + if err := currDB.FirstOrCreate(®ion, Region{Name: name}).Error; err != nil { + panic(err) + } + } +} + func Use(db *gorm.DB) { currDB = db } diff --git a/orm/region.go b/orm/region.go new file mode 100644 index 00000000..b4ec83e2 --- /dev/null +++ b/orm/region.go @@ -0,0 +1,124 @@ +package orm + +import ( + "net/http" + + "git.andreafazzi.eu/andrea/oef/renderer" + "github.com/jinzhu/gorm" +) + +type Region struct { + gorm.Model + + Name string +} + +func (model *Region) GetID() uint { return model.ID } + +func (model *Region) String() string { + return model.Name +} + +func (model *Region) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { + if r.Method == "GET" { + region := new(Region) + // if err := DB().Find(®ion.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(region) + if err != nil { + return nil, err + } + return region, nil + } +} + +func (model *Region) Read(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { + var region Region + + id := args["id"] + + if err := DB(). /*.Preload("Something")*/ First(®ion, id).Error; err != nil { + return nil, err + } + + return ®ion, nil +} + +func (model *Region) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { + var regions []*Region + if err := DB(). /*.Preload("Something")*/ Order("created_at").Find(®ions).Error; err != nil { + return nil, err + } + return regions, nil +} + +func (model *Region) 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 + } + + region := result.(*Region) + + // if err := DB().Find(®ion.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(args, w, r) + if err != nil { + return nil, err + } + err = renderer.Decode(region, r) + if err != nil { + return nil, err + } + _, err = SaveRegion(region) + if err != nil { + return nil, err + } + region, err = model.Read(args, w, r) + if err != nil { + return nil, err + } + return region.(*Region), nil + } +} + +func (model *Region) Delete(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { + region, err := model.Read(args, w, r) + if err != nil { + return nil, err + } + if err := DB().Unscoped().Delete(region.(*Region)).Error; err != nil { + return nil, err + } + return region.(*Region), nil +} + +func CreateRegion(region *Region) (*Region, error) { + if err := DB().Create(region).Error; err != nil { + return nil, err + } + return region, nil +} + +func SaveRegion(region interface{}) (interface{}, error) { + if err := DB(). /*.Omit("Something")*/ Save(region).Error; err != nil { + return nil, err + } + return region, nil +} diff --git a/orm/school.go b/orm/school.go index 1c33af83..30f0ffe9 100644 --- a/orm/school.go +++ b/orm/school.go @@ -19,14 +19,20 @@ type School struct { *UserModifierUpdate Name string + Address string Email string Code string EmailSentDate time.Time - UserID uint + UserID uint + RegionID uint `schema:"region_id"` + Region *Region User *User Participants []*Participant + + SelectedRegion map[uint]string `gorm:"-"` + AllRegions []*Region `gorm:"-"` } func (model *School) GetID() uint { return model.ID } @@ -102,6 +108,11 @@ func (model *School) AfterCreate(tx *gorm.DB) error { func (model *School) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { if r.Method == "GET" { school := new(School) + + if err := DB().Find(&school.AllRegions).Error; err != nil { + return nil, err + } + return school, nil } else { school := new(School) @@ -144,7 +155,7 @@ func (model *School) Read(args map[string]string, w http.ResponseWriter, r *http return nil, errors.NotAuthorized } - if err := DB().Preload("User").Preload("Participants.Category").Preload("Participants").First(&school, id).Error; err != nil { + if err := DB().Preload("User").Preload("Region").Preload("Participants.Category").Preload("Participants").First(&school, id).Error; err != nil { return nil, err } @@ -153,7 +164,7 @@ func (model *School) Read(args map[string]string, w http.ResponseWriter, r *http func (model *School) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { var schools []*School - if err := DB().Preload("Participants").Order("code").Find(&schools).Error; err != nil { + if err := DB().Preload("Region").Preload("Participants.Category").Preload("Participants").Order("code").Find(&schools).Error; err != nil { return nil, err } return schools, nil @@ -168,12 +179,12 @@ func (model *School) Update(args map[string]string, w http.ResponseWriter, r *ht school := result.(*School) - // if err := DB().Find(&school.AllElements).Error; err != nil { - // return nil, err - // } + if err := DB().Find(&school.AllRegions).Error; err != nil { + return nil, err + } - // school.SelectedElement = make(map[uint]string) - // school.SelectedElement[school.ElementID] = "selected" + school.SelectedRegion = make(map[uint]string) + school.SelectedRegion[school.RegionID] = "selected" return school, nil } else { @@ -232,8 +243,7 @@ func CreateSchool(school *School) (*School, error) { } func SaveSchool(school interface{}) (interface{}, error) { - // school.(*School).Code = strings.ToUpper(school.(*School).Code) - if err := DB(). /*.Omit("Something")*/ Save(school).Error; err != nil { + if err := DB().Omit("Region").Save(school).Error; err != nil { return nil, err } return school, nil diff --git a/templates/regions.html.tpl b/templates/regions.html.tpl new file mode 100644 index 00000000..22cee4b7 --- /dev/null +++ b/templates/regions.html.tpl @@ -0,0 +1,33 @@ +{{ define "content" }} + + +
+{{$options := ` + title: "Regions" + buttonTitle: "Crea nuovo Region" + `}} + + {{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Region")}} + {{template "search_input"}} + + {{if not .}} + {{template "display_no_elements"}} + {{else}} +
+ {{range $element := .Data}} + + + {{$element|string}} +
+ {{$options := `noElements: "no subelements"`}} + {{/*template "small" dict "options" ($options | yaml) "data" $element.SubElements*/}} +
+
+ {{end}} + {{end}} +
+ +
+ + +{{ end }} diff --git a/templates/regions_add_update.html.tpl b/templates/regions_add_update.html.tpl new file mode 100644 index 00000000..51d3146e --- /dev/null +++ b/templates/regions_add_update.html.tpl @@ -0,0 +1,28 @@ +{{ define "content" }} +
+ +{{$update := .Options.Get "update"}} + +{{if $update}} + +{{template "breadcrumb" toSlice "Regions" (all "Region") (.Data|string) (.Data.ID|show "Region") "Aggiorna" "current"}} +{{else}} +{{template "breadcrumb" toSlice "Regions" (all "Region") "Aggiungi" "current"}} +{{end}} + +{{template "add_update_header" dict "update" $update "addTitle" "Crea nuovo ELEMENTO" "updateTitle" (printf "Aggiorna ELEMENTO %s" (.Data|string))}} +{{$form := "form_add_update"}} +
+ + {{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Region" } `}} + {{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}} + +
+ +
+{{ end }} diff --git a/templates/regions_show.html.tpl b/templates/regions_show.html.tpl new file mode 100644 index 00000000..62a14cf3 --- /dev/null +++ b/templates/regions_show.html.tpl @@ -0,0 +1,27 @@ +{{ define "content" }} + +
+ + {{template "breadcrumb" toSlice "ELEMENTS" (all "Region") (.Data|string) "current"}} + {{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Region") "deletePath" (.Data.ID|delete "Region")}} + +

GENERAL SECTION

+ +
+
+ + {{$options := ` + title: "RELATIONS" + model: "MODEL" + icon: "ICON_CLASS" + `}} + + {{$noElements := "NO ELEMENTS"}} + {{template "relation_list" dict "options" ($options|yaml) "data" .Data.RELATIONS "noElements" $noElements}} + +
+
+ +
+ +{{ end }}