Implement MVC for Participant

This commit is contained in:
Andrea Fazzi 2019-11-15 10:41:32 +01:00
parent 46ce9b704c
commit da478d71c8
8 changed files with 109 additions and 39 deletions

View file

@ -20,7 +20,8 @@ type Contest struct {
StartTime *time.Time StartTime *time.Time
EndTime *time.Time EndTime *time.Time
Questions []*Question Questions []*Question
Participants []*Participant `gorm:"many2many:subscriptions"`
} }
func (c *Contest) GetID() uint { return c.ID } func (c *Contest) GetID() uint { return c.ID }
@ -34,7 +35,28 @@ func (c *Contest) Create(args map[string]string, r *http.Request) (interface{},
return nil, nil return nil, nil
} else { } else {
contest := new(Contest) contest := new(Contest)
err := renderer.Decode(contest, r)
err := r.ParseForm()
if err != nil {
return nil, err
}
date := r.FormValue("Date")
startTime := r.FormValue("StartTime")
endTime := r.FormValue("EndTime")
r.PostForm.Set("Date", fmt.Sprintf("%sT%s:00+00:00", date, startTime))
r.PostForm.Set("StartTime", fmt.Sprintf("%sT%s:00+00:00", date, startTime))
r.PostForm.Set("EndTime", fmt.Sprintf("%sT%s:00+00:00", date, endTime))
// err = r.ParseForm()
// if err != nil {
// return nil, err
// }
// r.Form["Date"][0] = fmt.Sprintf("%sT%s+00:00", date, startTime)
err = renderer.Decode(contest, r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -51,7 +73,7 @@ func (c *Contest) Read(args map[string]string, r *http.Request) (interface{}, er
id := args["id"] id := args["id"]
if err := DB().Preload("Questions").First(&contest, id).Error; err != nil { if err := DB().Preload("Participants").Preload("Questions").First(&contest, id).Error; err != nil {
return nil, err return nil, err
} }

View file

@ -1,7 +1,9 @@
package orm package orm
import ( import (
"fmt"
"net/http" "net/http"
"strings"
"git.andreafazzi.eu/andrea/oef/renderer" "git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
@ -9,23 +11,32 @@ import (
type Participant struct { type Participant struct {
gorm.Model gorm.Model
// SelectedElement map[uint]string `gorm:"-"` Firstname string
// AllElements []*Element `gorm:"-"` Lastname string
Username string
Password string
ContestIDs []uint `schema:"contest_ids" gorm:"-"`
Contests []*Contest `gorm:"many2many:subscriptions"`
SelectedContest map[uint]string `gorm:"-"`
AllContests []*Contest `gorm:"-"`
} }
func (model *Participant) GetID() uint { return model.ID } func (model *Participant) GetID() uint { return model.ID }
func (model *Participant) String() string { func (model *Participant) String() string {
return "" // Please implement this. return fmt.Sprintf("%s %s", strings.ToUpper(model.Lastname), strings.Title(strings.ToLower(model.Firstname)))
} }
func (model *Participant) Create(args map[string]string, r *http.Request) (interface{}, error) { func (model *Participant) Create(args map[string]string, r *http.Request) (interface{}, error) {
if r.Method == "GET" { if r.Method == "GET" {
participant := new(Participant) participant := new(Participant)
// if err := DB().Find(&participant.AllContests).Error; err != nil { if err := DB().Find(&participant.AllContests).Error; err != nil {
// return nil, err return nil, err
// } }
return participant, nil return participant, nil
} else { } else {
participant := new(Participant) participant := new(Participant)
@ -46,7 +57,7 @@ func (model *Participant) Read(args map[string]string, r *http.Request) (interfa
id := args["id"] id := args["id"]
if err := DB()/*.Preload("Something")*/.First(&participant, id).Error; err != nil { if err := DB().Preload("Contests").First(&participant, id).Error; err != nil {
return nil, err return nil, err
} }
@ -55,7 +66,7 @@ func (model *Participant) Read(args map[string]string, r *http.Request) (interfa
func (model *Participant) ReadAll(args map[string]string, r *http.Request) (interface{}, error) { func (model *Participant) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
var participants []*Participant var participants []*Participant
if err := DB()/*.Preload("Something")*/.Order("created_at").Find(&participants).Error; err != nil { if err := DB().Preload("Contests").Order("created_at").Find(&participants).Error; err != nil {
return nil, err return nil, err
} }
return participants, nil return participants, nil
@ -70,12 +81,14 @@ func (model *Participant) Update(args map[string]string, r *http.Request) (inter
participant := result.(*Participant) participant := result.(*Participant)
// if err := DB().Find(&participant.AllElements).Error; err != nil { if err := DB().Find(&participant.AllContests).Error; err != nil {
// return nil, err return nil, err
// } }
// participant.SelectedElement = make(map[uint]string) participant.SelectedContest = make(map[uint]string)
// participant.SelectedElement[participant.ElementID] = "selected" for _, c := range participant.Contests {
participant.SelectedContest[c.ID] = "selected"
}
return participant, nil return participant, nil
} else { } else {
@ -87,6 +100,11 @@ func (model *Participant) Update(args map[string]string, r *http.Request) (inter
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := DB().Where(participant.(*Participant).ContestIDs).Find(&participant.(*Participant).Contests).Error; err != nil {
return nil, err
}
_, err = SaveParticipant(participant) _, err = SaveParticipant(participant)
if err != nil { if err != nil {
return nil, err return nil, err
@ -118,7 +136,7 @@ func CreateParticipant(participant *Participant) (*Participant, error) {
} }
func SaveParticipant(participant interface{}) (interface{}, error) { func SaveParticipant(participant interface{}) (interface{}, error) {
if err := DB()/*.Omit("Something")*/.Save(participant).Error; err != nil { if err := DB(). /*.Omit("Something")*/ Save(participant).Error; err != nil {
return nil, err return nil, err
} }
return participant, nil return participant, nil

View file

@ -50,11 +50,10 @@ var (
) )
func trim(text string) string { func trim(text string) string {
var result string
if len(text) > MaxTextLength { if len(text) > MaxTextLength {
result = text[0:MaxTextLength] return text[0:MaxTextLength] + "…"
} }
return result + "…" return text
} }
func modelName(value interface{}) string { func modelName(value interface{}) string {

View file

@ -16,7 +16,7 @@
<div class="list-group" id="myUL"> <div class="list-group" id="myUL">
{{range $element := .Data}} {{range $element := .Data}}
<a class="list-group-item list-group-item-action" href={{$element.ID|show "Contest"}}> <a class="list-group-item list-group-item-action" href={{$element.ID|show "Contest"}}>
<span class="fa fa-user"></span> <span class="fa fa-hourglass-start"></span>
{{$element|string}} {{$element|string}}
<div class="text-right"> <div class="text-right">
{{$options := `noElements: "nessuna data"`}} {{$options := `noElements: "nessuna data"`}}

View file

@ -16,12 +16,27 @@
icon: "fa fa-question-circle" icon: "fa fa-question-circle"
`}} `}}
{{$noElements := "nessuna domanda associata alla gara"}} {{$noElements := "Nessuna domanda è stata ancora associata alla gara."}}
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.Questions "noElements" $noElements}} {{template "relation_list" dict "options" ($options|yaml) "data" .Data.Questions "noElements" $noElements}}
</div> </div>
</div> </div>
<div class="row">
<div class="col-md-12">
{{$options := `
title: "Partecipanti"
model: "Participant"
icon: "fa fa-user"
`}}
{{$noElements := "Nessun partecipante è iscritto a questa gara."}}
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.Participants "noElements" $noElements}}
</div>
</div>
</div> </div>
{{ end }} {{ end }}

View file

@ -3,8 +3,8 @@
<div class="container"> <div class="container">
{{$options := ` {{$options := `
title: "Participants" title: "Participanti"
buttonTitle: "Crea nuovo Participant" buttonTitle: "Crea nuovo partecipante"
`}} `}}
{{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Participant")}} {{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Participant")}}
@ -19,8 +19,8 @@
<span class="fa fa-user"></span> <span class="fa fa-user"></span>
{{$element|string}} {{$element|string}}
<div class="text-right"> <div class="text-right">
{{$options := `noElements: "no subelements"`}} {{$options := `noElements: "nessuna gara"`}}
{{/*template "small" dict "options" ($options | yaml) "data" $element.SubElements*/}} {{template "small" dict "options" ($options | yaml) "data" $element.Contests}}
</div> </div>
</a> </a>
{{end}} {{end}}

View file

@ -5,12 +5,12 @@
{{if $update}} {{if $update}}
{{template "breadcrumb" toSlice "Participants" (all "Participant") (.Data|string) (.Data.ID|show "Participant") "Aggiorna" "current"}} {{template "breadcrumb" toSlice "Participanti" (all "Participant") (.Data|string) (.Data.ID|show "Participant") "Aggiorna" "current"}}
{{else}} {{else}}
{{template "breadcrumb" toSlice "Participants" (all "Participant") "Aggiungi" "current"}} {{template "breadcrumb" toSlice "Participanti" (all "Participant") "Aggiungi" "current"}}
{{end}} {{end}}
{{template "add_update_header" dict "update" $update "addTitle" "Crea nuovo ELEMENTO" "updateTitle" (printf "Aggiorna ELEMENTO %s" (.Data|string))}} {{template "add_update_header" dict "update" $update "addTitle" "Crea nuovo partecipante" "updateTitle" (printf "Aggiorna partecipante %s" (.Data|string))}}
{{$form := "form_add_update"}} {{$form := "form_add_update"}}
<form <form
class="needs-validation" class="needs-validation"
@ -19,6 +19,20 @@
role="form" role="form"
id={{$form}}> id={{$form}}>
<div class="form-row">
<div class="col">
{{$options := ` { name: "Firstname",id: "participant_firstname",label: "Nome del partecipante",placeholder: "Inserire il nome del partecipante",type: "text",required: "true"} `}}
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "Firstname") "update" $update}}
</div>
<div class="col">
{{$options := ` { name: "Lastname",id: "participant_lastname",label: "Cognome del partecipante",placeholder: "Inserire il cognome del partecipante",type: "text",required: "true"} `}}
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "Lastname") "update" $update}}
</div>
</div>
{{$options := ` { name: "contest_ids", id: "contest_ids", label: "Gare a cui il partecipante è iscritto", title: "Seleziona le gare", multiple: "true"}`}}
{{template "select" dict "options" ($options|yaml) "data" (.Data|field "AllContests") "selected" (.Data|field "SelectedContest") "update" $update "form" $form}}
{{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Participant" } `}} {{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Participant" } `}}
{{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}} {{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}

View file

@ -2,22 +2,24 @@
<div class="container"> <div class="container">
{{template "breadcrumb" toSlice "ELEMENTS" (all "Participant") (.Data|string) "current"}} {{template "breadcrumb" toSlice "Partecipanti" (all "Participant") (.Data|string) "current"}}
{{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Participant") "deletePath" (.Data.ID|delete "Participant")}} {{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> <h2 class="karmen-relation-header">Informazioni generali</h2>
<p>
Questa scheda contiene la informazioni relative al partecipante <strong>{{.Data|string}}</strong>.
</p>
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
{{$options := ` {{$options := `
title: "RELATIONS" title: "Gare a cui il partecipante è iscritto"
model: "MODEL" model: "Contest"
icon: "ICON_CLASS" icon: "fa fa-hourglass-start"
`}} `}}
{{$noElements := "NO ELEMENTS"}} {{$noElements := "Il partecipante non è iscritto ad alcuna gara."}}
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.RELATIONS "noElements" $noElements}} {{template "relation_list" dict "options" ($options|yaml) "data" .Data.Contests "noElements" $noElements}}
</div> </div>
</div> </div>