Add School model
This commit is contained in:
parent
65f0aeadd0
commit
6cb74a5ebe
9 changed files with 331 additions and 11 deletions
|
@ -5,5 +5,8 @@ var (
|
|||
"participantExists": map[string]string{
|
||||
"it": "Un partecipante con questo codice fiscale è già presente nella base dati!",
|
||||
},
|
||||
"schoolExists": map[string]string{
|
||||
"it": "Una scuola con questo codice meccanografico è già presente nella base dati!",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
|
1
main.go
1
main.go
|
@ -30,6 +30,7 @@ var (
|
|||
&orm.Answer{},
|
||||
&orm.Contest{},
|
||||
&orm.Participant{},
|
||||
&orm.School{},
|
||||
&orm.Response{},
|
||||
&orm.User{},
|
||||
}
|
||||
|
|
|
@ -22,7 +22,10 @@ type Participant struct {
|
|||
|
||||
FiscalCode string
|
||||
|
||||
SchoolID uint
|
||||
|
||||
User *User
|
||||
School *School
|
||||
|
||||
Responses []*Response
|
||||
|
||||
|
@ -31,6 +34,9 @@ type Participant struct {
|
|||
|
||||
SelectedContest map[uint]string `gorm:"-"`
|
||||
AllContests []*Contest `gorm:"-"`
|
||||
|
||||
SelectedSchool map[uint]string `gorm:"-"`
|
||||
AllSchools []*School `gorm:"-"`
|
||||
}
|
||||
|
||||
func (model *Participant) sanitize(s string) string {
|
||||
|
@ -40,7 +46,6 @@ func (model *Participant) sanitize(s string) string {
|
|||
}
|
||||
|
||||
func (model *Participant) username() string {
|
||||
// return strings.Join([]string{model.sanitize(model.Firstname), model.sanitize(model.Lastname)}, ".")
|
||||
return strings.ToUpper(model.FiscalCode)
|
||||
}
|
||||
|
||||
|
@ -181,6 +186,13 @@ func (model *Participant) Update(args map[string]string, w http.ResponseWriter,
|
|||
participant.SelectedContest[c.ID] = "selected"
|
||||
}
|
||||
|
||||
if err := DB().Find(&participant.AllSchools).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
participant.SelectedSchool = make(map[uint]string)
|
||||
participant.SelectedSchool[participant.SchoolID] = "selected"
|
||||
|
||||
return participant, nil
|
||||
} else {
|
||||
participant, err := model.Read(args, w, r)
|
||||
|
|
197
orm/school.go
Normal file
197
orm/school.go
Normal file
|
@ -0,0 +1,197 @@
|
|||
package orm
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.andreafazzi.eu/andrea/oef/renderer"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type School struct {
|
||||
gorm.Model
|
||||
|
||||
Name string
|
||||
Email string
|
||||
Code string
|
||||
|
||||
UserID uint
|
||||
|
||||
User *User
|
||||
Participants []*Participant
|
||||
|
||||
// SelectedElement map[uint]string `gorm:"-"`
|
||||
// AllElements []*Element `gorm:"-"`
|
||||
}
|
||||
|
||||
func (model *School) GetID() uint { return model.ID }
|
||||
|
||||
func (model *School) String() string {
|
||||
return model.Name
|
||||
}
|
||||
|
||||
func (model *School) username() string {
|
||||
return strings.ToUpper(model.Code)
|
||||
}
|
||||
|
||||
func (model *School) exists() (*User, error) {
|
||||
var user User
|
||||
if err := DB().First(&user, &User{Username: model.username()}).Error; err != nil && err != gorm.ErrRecordNotFound {
|
||||
return nil, err
|
||||
} else if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (model *School) BeforeSave(tx *gorm.DB) error {
|
||||
var user User
|
||||
if err := tx.FirstOrCreate(&user, &User{
|
||||
Username: model.username(),
|
||||
Role: "school",
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
model.UserID = user.ID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (model *School) AfterDelete(tx *gorm.DB) error {
|
||||
if err := tx.Unscoped().Delete(model.User).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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.AllContests).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
return school, nil
|
||||
} else {
|
||||
school := new(School)
|
||||
err := renderer.Decode(school, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if this user already exists in the users table.
|
||||
if user, err := school.exists(); err == nil && user != nil {
|
||||
if err := DB().Where("user_id = ?", user.ID).Find(&school).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err := setFlashMessage(w, r, "schoolExists")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return school, nil
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
school, err = CreateSchool(school)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return school, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (model *School) Read(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
var school School
|
||||
|
||||
id := args["id"]
|
||||
|
||||
if err := DB(). /*.Preload("Something")*/ First(&school, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &school, nil
|
||||
}
|
||||
|
||||
func (model *School) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
var schools []*School
|
||||
if err := DB(). /*.Preload("Something")*/ Order("created_at").Find(&schools).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return schools, nil
|
||||
}
|
||||
|
||||
func (model *School) 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
|
||||
}
|
||||
|
||||
school := result.(*School)
|
||||
|
||||
// if err := DB().Find(&school.AllElements).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// school.SelectedElement = make(map[uint]string)
|
||||
// school.SelectedElement[school.ElementID] = "selected"
|
||||
|
||||
return school, nil
|
||||
} else {
|
||||
school, err := model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = renderer.Decode(school, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if the modified school code belong to an existing school.
|
||||
if user, err := school.(*School).exists(); err == nil && user != nil {
|
||||
if user.ID != school.(*School).UserID {
|
||||
err := setFlashMessage(w, r, "schoolExists")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return school, nil
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = SaveSchool(school)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
school, err = model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return school.(*School), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (model *School) Delete(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
school, err := model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Unscoped().Delete(school.(*School)).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return school.(*School), nil
|
||||
}
|
||||
|
||||
func CreateSchool(school *School) (*School, error) {
|
||||
if err := DB().Create(school).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return school, nil
|
||||
}
|
||||
|
||||
func SaveSchool(school interface{}) (interface{}, error) {
|
||||
if err := DB(). /*.Omit("Something")*/ Save(school).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return school, nil
|
||||
}
|
|
@ -20,7 +20,7 @@ func (model *{{.Model}}) String() string {
|
|||
return "" // Please implement this.
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) Create(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
func (model *{{.Model}}) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
{{.Model|toLower}} := new({{.Model}})
|
||||
// if err := DB().Find(&{{.Model|toLower}}.AllContests).Error; err != nil {
|
||||
|
@ -41,7 +41,7 @@ func (model *{{.Model}}) Create(args map[string]string, r *http.Request) (interf
|
|||
}
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) Read(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
func (model *{{.Model}}) Read(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
var {{.Model|toLower}} {{.Model}}
|
||||
|
||||
id := args["id"]
|
||||
|
@ -53,7 +53,7 @@ func (model *{{.Model}}) Read(args map[string]string, r *http.Request) (interfac
|
|||
return &{{.Model|toLower}}, nil
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
func (model *{{.Model}}) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
var {{.Models|toLower}} []*{{.Model}}
|
||||
if err := DB()/*.Preload("Something")*/.Order("created_at").Find(&{{.Models|toLower}}).Error; err != nil {
|
||||
return nil, err
|
||||
|
@ -61,9 +61,9 @@ func (model *{{.Model}}) ReadAll(args map[string]string, r *http.Request) (inter
|
|||
return {{.Models|toLower}}, nil
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) Update(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
func (model *{{.Model}}) Update(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
result, err := model.Read(args, r)
|
||||
result, err := model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ func (model *{{.Model}}) Update(args map[string]string, r *http.Request) (interf
|
|||
|
||||
return {{.Model|toLower}}, nil
|
||||
} else {
|
||||
{{.Model|toLower}}, err := model.Read(args, nil)
|
||||
{{.Model|toLower}}, err := model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ func (model *{{.Model}}) Update(args map[string]string, r *http.Request) (interf
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
{{.Model|toLower}}, err = model.Read(args, nil)
|
||||
{{.Model|toLower}}, err = model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -99,8 +99,8 @@ func (model *{{.Model}}) Update(args map[string]string, r *http.Request) (interf
|
|||
}
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) Delete(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
{{.Model|toLower}}, err := model.Read(args, nil)
|
||||
func (model *{{.Model}}) Delete(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
{{.Model|toLower}}, err := model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
{{if (.Claims|isAdmin)}}
|
||||
<a class="nav-item nav-link {{.Options|active "Question"}}" href="{{all "Question"}}">Domande</a>
|
||||
<a class="nav-item nav-link {{.Options|active "Answer"}}" href="{{all "Answer"}}">Risposte</a>
|
||||
<a class="nav-item nav-link {{.Options|active "School"}}" href="{{all "School"}}">Scuole</a>
|
||||
<a class="nav-item nav-link {{.Options|active "Participant"}}" href="{{all "Participant"}}">Partecipanti</a>
|
||||
{{end}}
|
||||
</ul>
|
||||
|
|
33
templates/schools.html.tpl
Normal file
33
templates/schools.html.tpl
Normal file
|
@ -0,0 +1,33 @@
|
|||
{{ define "content" }}
|
||||
|
||||
|
||||
<div class="container">
|
||||
{{$options := `
|
||||
title: "Scuole"
|
||||
buttonTitle: "Crea nuova scuola"
|
||||
`}}
|
||||
|
||||
{{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "School")}}
|
||||
{{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 "School"}}>
|
||||
<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 }}
|
45
templates/schools_add_update.html.tpl
Normal file
45
templates/schools_add_update.html.tpl
Normal file
|
@ -0,0 +1,45 @@
|
|||
{{ define "content" }}
|
||||
<div class="container">
|
||||
|
||||
{{$update := .Options.Get "update"}}
|
||||
|
||||
{{if $update}}
|
||||
|
||||
{{template "breadcrumb" toSlice "Schools" (all "School") (.Data|string) (.Data.ID|show "School") "Aggiorna" "current"}}
|
||||
{{else}}
|
||||
{{template "breadcrumb" toSlice "Schools" (all "School") "Aggiungi" "current"}}
|
||||
{{end}}
|
||||
|
||||
{{template "add_update_header" dict "update" $update "addTitle" "Crea nuova scuola" "updateTitle" (printf "Aggiorna scuola %s" (.Data|string))}}
|
||||
{{$form := "form_add_update"}}
|
||||
<form
|
||||
class="needs-validation"
|
||||
action="{{if $update}}{{.Data.ID|update "School"}}{{else}}{{create "School"}}{{end}}"
|
||||
method="POST"
|
||||
role="form"
|
||||
id={{$form}}>
|
||||
|
||||
{{$options := ` { name: "Name",id: "school_name",label: "Denominazione dell'istituto",placeholder: "Inserire la denominazione",type: "text",required: "true"} `}}
|
||||
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "Name") "update" $update}}
|
||||
|
||||
{{$options := `
|
||||
name: "Code"
|
||||
id: "school_code"
|
||||
label: "Codice meccanografico"
|
||||
placeholder: "Inserire il codice meccanografico"
|
||||
type: "text"
|
||||
inputClass: "form-control uppercase"
|
||||
required: "true"
|
||||
`}}
|
||||
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "Code") "update" $update}}
|
||||
|
||||
{{$options := ` { name: "Email",id: "school_email",label: "Cognome del partecipante",placeholder: "Inserire la mail istituzionale",type: "email",required: "true"} `}}
|
||||
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "Email") "update" $update}}
|
||||
|
||||
{{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "School" } `}}
|
||||
{{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
{{ end }}
|
28
templates/schools_show.html.tpl
Normal file
28
templates/schools_show.html.tpl
Normal file
|
@ -0,0 +1,28 @@
|
|||
{{ define "content" }}
|
||||
|
||||
<div class="container">
|
||||
|
||||
{{template "breadcrumb" toSlice "Scuole" (all "School") (.Data|string) "current"}}
|
||||
{{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "School") "deletePath" (.Data.ID|delete "School")}}
|
||||
|
||||
<h2 class="karmen-relation-header">Informazioni sulla scuola</h2>
|
||||
<p>Il codice meccanografico della scuola è <strong>{{.Data.Code}}</strong>.</p>
|
||||
<p>La mail istituzionale della scuola è <strong>{{.Data.Email}}</strong>.</p>
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
|
||||
{{$options := `
|
||||
title: "Partecipanti"
|
||||
model: "Participant"
|
||||
icon: "fa fa-user"
|
||||
`}}
|
||||
|
||||
{{$noElements := "La scuola non ha iscritto alcun partecipante."}}
|
||||
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.Participants "noElements" $noElements}}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
{{ end }}
|
Loading…
Reference in a new issue