Generate Participant MVC
This commit is contained in:
parent
9a62740eed
commit
46ce9b704c
8 changed files with 271 additions and 12 deletions
1
main.go
1
main.go
|
@ -29,6 +29,7 @@ var (
|
|||
&orm.Question{},
|
||||
&orm.Answer{},
|
||||
&orm.Contest{},
|
||||
&orm.Participant{},
|
||||
}
|
||||
)
|
||||
|
||||
|
|
125
orm/participant.go
Normal file
125
orm/participant.go
Normal file
|
@ -0,0 +1,125 @@
|
|||
package orm
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.andreafazzi.eu/andrea/oef/renderer"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type Participant struct {
|
||||
gorm.Model
|
||||
|
||||
// SelectedElement map[uint]string `gorm:"-"`
|
||||
// AllElements []*Element `gorm:"-"`
|
||||
}
|
||||
|
||||
func (model *Participant) GetID() uint { return model.ID }
|
||||
|
||||
func (model *Participant) String() string {
|
||||
return "" // Please implement this.
|
||||
}
|
||||
|
||||
func (model *Participant) Create(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
participant := new(Participant)
|
||||
// if err := DB().Find(&participant.AllContests).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
return participant, nil
|
||||
} else {
|
||||
participant := new(Participant)
|
||||
err := renderer.Decode(participant, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
participant, err = CreateParticipant(participant)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return participant, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (model *Participant) Read(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
var participant Participant
|
||||
|
||||
id := args["id"]
|
||||
|
||||
if err := DB()/*.Preload("Something")*/.First(&participant, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &participant, nil
|
||||
}
|
||||
|
||||
func (model *Participant) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
var participants []*Participant
|
||||
if err := DB()/*.Preload("Something")*/.Order("created_at").Find(&participants).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return participants, nil
|
||||
}
|
||||
|
||||
func (model *Participant) Update(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
result, err := model.Read(args, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
participant := result.(*Participant)
|
||||
|
||||
// if err := DB().Find(&participant.AllElements).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// participant.SelectedElement = make(map[uint]string)
|
||||
// participant.SelectedElement[participant.ElementID] = "selected"
|
||||
|
||||
return participant, nil
|
||||
} else {
|
||||
participant, err := model.Read(args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = renderer.Decode(participant, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = SaveParticipant(participant)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
participant, err = model.Read(args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return participant.(*Participant), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (model *Participant) Delete(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
participant, err := model.Read(args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Unscoped().Delete(participant.(*Participant)).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return participant.(*Participant), nil
|
||||
}
|
||||
|
||||
func CreateParticipant(participant *Participant) (*Participant, error) {
|
||||
if err := DB().Create(participant).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return participant, nil
|
||||
}
|
||||
|
||||
func SaveParticipant(participant interface{}) (interface{}, error) {
|
||||
if err := DB()/*.Omit("Something")*/.Save(participant).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return participant, nil
|
||||
}
|
|
@ -19,8 +19,8 @@ var (
|
|||
"show": "%s_show",
|
||||
"add_update": "%s_add_update",
|
||||
}
|
||||
fnOrmPatterns = map[string]string{
|
||||
"orm": "%s",
|
||||
fnModelPatterns = map[string]string{
|
||||
"model": "%s",
|
||||
}
|
||||
|
||||
TemplateInputDir string
|
||||
|
@ -76,6 +76,45 @@ func generateTemplates(patterns map[string]string, model string, funcMap ...temp
|
|||
|
||||
}
|
||||
|
||||
func generateModels(patterns map[string]string, model string, funcMap ...template.FuncMap) error {
|
||||
for tplName, pattern := range patterns {
|
||||
var data struct {
|
||||
Model string
|
||||
Models string
|
||||
}
|
||||
|
||||
data.Model = inflection.Singular(model)
|
||||
data.Models = inflection.Plural(model)
|
||||
|
||||
name := fmt.Sprintf(pattern, strings.ToLower(data.Model))
|
||||
|
||||
tpl, err := tpl_util.LoadTextTemplate(filepath.Join(ModelInputDir, tplName+".tpl"), funcMap...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
filename := filepath.Join(ModelOutputDir, name+".go")
|
||||
if _, err := os.Stat(filename); err != nil {
|
||||
oFn, err := os.Create(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer oFn.Close()
|
||||
|
||||
log.Printf("Generating model go file %s for model %s...", filename, name)
|
||||
|
||||
err = tpl.Execute(oFn, data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
log.Printf("Model file %s already exists. Skipping.", filename)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||
|
@ -84,7 +123,7 @@ func main() {
|
|||
|
||||
TemplateOutputDir = *flag.String("tpl-output-dir", "./templates", "Template output directory")
|
||||
TemplateInputDir = *flag.String("tpl-input-dir", "./template_generator/templates", "Generator template directory")
|
||||
ModelOutputDir = *flag.String("model-input-dir", "./orm", "ORM output directory")
|
||||
ModelOutputDir = *flag.String("model-output-dir", "./orm", "ORM output directory")
|
||||
ModelInputDir = *flag.String("model-input-dir", "./template_generator/orm", "Generator ORM template directory")
|
||||
|
||||
flag.Parse()
|
||||
|
@ -104,4 +143,9 @@ func main() {
|
|||
if err := generateTemplates(fnTemplatePatterns, model, funcMap); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := generateModels(fnModelPatterns, model, funcMap); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ type {{.Model}} struct {
|
|||
// AllElements []*Element `gorm:"-"`
|
||||
}
|
||||
|
||||
func (model *{{.Model}}) GetID() uint { return q.ID }
|
||||
func (model *{{.Model}}) GetID() uint { return model.ID }
|
||||
|
||||
func (model *{{.Model}}) String() string {
|
||||
return "" // Please implement this.
|
||||
|
@ -46,7 +46,7 @@ func (model *{{.Model}}) Read(args map[string]string, r *http.Request) (interfac
|
|||
|
||||
id := args["id"]
|
||||
|
||||
if err := DB()/*.Preload("Something").*/First(&{{.Model|toLower}}, id).Error; err != nil {
|
||||
if err := DB()/*.Preload("Something")*/.First(&{{.Model|toLower}}, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ func (model *{{.Model}}) ReadAll(args map[string]string, r *http.Request) (inter
|
|||
|
||||
func (model *{{.Model}}) Update(args map[string]string, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
result, err := q.Read(args, r)
|
||||
result, err := model.Read(args, 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 := q.Read(args, nil)
|
||||
{{.Model|toLower}}, err := model.Read(args, nil)
|
||||
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 = q.Read(args, nil)
|
||||
{{.Model|toLower}}, err = model.Read(args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -100,14 +100,14 @@ 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 := t.Read(args, nil)
|
||||
{{.Model|toLower}}, err := model.Read(args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Unscoped().Delete({{.Model|toLower}}.(*{{.Model|ToLower}})).Error; err != nil {
|
||||
if err := DB().Unscoped().Delete({{.Model|toLower}}.(*{{.Model}})).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return {{.Model|toLower}}.(*{{.Model|ToLower}}), nil
|
||||
return {{.Model|toLower}}.(*{{.Model}}), nil
|
||||
}
|
||||
|
||||
func Create{{.Model}}({{.Model|toLower}} *{{.Model}}) (*{{.Model}}, error) {
|
||||
|
|
|
@ -24,7 +24,8 @@
|
|||
<ul class="navbar-nav mr-auto">
|
||||
<a class="nav-item nav-link {{.Options|active "Contest"}}" href="{{all "Contest"}}">Gare</a>
|
||||
<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 "Answers"}}">Risposte</a>
|
||||
<a class="nav-item nav-link {{.Options|active "Answer"}}" href="{{all "Answer"}}">Risposte</a>
|
||||
<a class="nav-item nav-link {{.Options|active "Participant"}}" href="{{all "Participant"}}">Partecipanti</a>
|
||||
</ul>
|
||||
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
|
|
33
templates/participants.html.tpl
Normal file
33
templates/participants.html.tpl
Normal file
|
@ -0,0 +1,33 @@
|
|||
{{ define "content" }}
|
||||
|
||||
|
||||
<div class="container">
|
||||
{{$options := `
|
||||
title: "Participants"
|
||||
buttonTitle: "Crea nuovo Participant"
|
||||
`}}
|
||||
|
||||
{{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Participant")}}
|
||||
{{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 "Participant"}}>
|
||||
<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/participants_add_update.html.tpl
Normal file
28
templates/participants_add_update.html.tpl
Normal file
|
@ -0,0 +1,28 @@
|
|||
{{ define "content" }}
|
||||
<div class="container">
|
||||
|
||||
{{$update := .Options.Get "update"}}
|
||||
|
||||
{{if $update}}
|
||||
|
||||
{{template "breadcrumb" toSlice "Participants" (all "Participant") (.Data|string) (.Data.ID|show "Participant") "Aggiorna" "current"}}
|
||||
{{else}}
|
||||
{{template "breadcrumb" toSlice "Participants" (all "Participant") "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 "Participant"}}{{else}}{{create "Participant"}}{{end}}"
|
||||
method="POST"
|
||||
role="form"
|
||||
id={{$form}}>
|
||||
|
||||
{{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Participant" } `}}
|
||||
{{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
{{ end }}
|
27
templates/participants_show.html.tpl
Normal file
27
templates/participants_show.html.tpl
Normal file
|
@ -0,0 +1,27 @@
|
|||
{{ define "content" }}
|
||||
|
||||
<div class="container">
|
||||
|
||||
{{template "breadcrumb" toSlice "ELEMENTS" (all "Participant") (.Data|string) "current"}}
|
||||
{{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Participant") "deletePath" (.Data.ID|delete "Participant")}}
|
||||
|
||||
<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