Add regions
This commit is contained in:
parent
45aa873a7e
commit
3c19bbd4fb
6 changed files with 266 additions and 11 deletions
35
orm/orm.go
35
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
|
||||
}
|
||||
|
|
124
orm/region.go
Normal file
124
orm/region.go
Normal file
|
@ -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
|
||||
}
|
|
@ -19,14 +19,20 @@ type School struct {
|
|||
*UserModifierUpdate
|
||||
|
||||
Name string
|
||||
Address string
|
||||
Email string
|
||||
Code string
|
||||
EmailSentDate time.Time
|
||||
|
||||
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
|
||||
|
|
33
templates/regions.html.tpl
Normal file
33
templates/regions.html.tpl
Normal file
|
@ -0,0 +1,33 @@
|
|||
{{ define "content" }}
|
||||
|
||||
|
||||
<div class="container">
|
||||
{{$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}}
|
||||
<div class="list-group" id="myUL">
|
||||
{{range $element := .Data}}
|
||||
<a class="list-group-item list-group-item-action" href={{$element.ID|show "Region"}}>
|
||||
<span class="fa fa-user"></span>
|
||||
{{$element|string}}
|
||||
<div class="text-right">
|
||||
{{$options := `noElements: "no subelements"`}}
|
||||
{{/*template "small" dict "options" ($options | yaml) "data" $element.SubElements*/}}
|
||||
</div>
|
||||
</a>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
{{ end }}
|
28
templates/regions_add_update.html.tpl
Normal file
28
templates/regions_add_update.html.tpl
Normal file
|
@ -0,0 +1,28 @@
|
|||
{{ define "content" }}
|
||||
<div class="container">
|
||||
|
||||
{{$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"}}
|
||||
<form
|
||||
class="needs-validation"
|
||||
action="{{if $update}}{{.Data.ID|update "Region"}}{{else}}{{create "Region"}}{{end}}"
|
||||
method="POST"
|
||||
role="form"
|
||||
id={{$form}}>
|
||||
|
||||
{{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Region" } `}}
|
||||
{{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
{{ end }}
|
27
templates/regions_show.html.tpl
Normal file
27
templates/regions_show.html.tpl
Normal file
|
@ -0,0 +1,27 @@
|
|||
{{ define "content" }}
|
||||
|
||||
<div class="container">
|
||||
|
||||
{{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")}}
|
||||
|
||||
<h2 class="karmen-relation-header">GENERAL SECTION</h2>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
|
||||
{{$options := `
|
||||
title: "RELATIONS"
|
||||
model: "MODEL"
|
||||
icon: "ICON_CLASS"
|
||||
`}}
|
||||
|
||||
{{$noElements := "NO ELEMENTS"}}
|
||||
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.RELATIONS "noElements" $noElements}}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
{{ end }}
|
Loading…
Reference in a new issue