diff --git a/orm/contest.go b/orm/contest.go index df0c6ce4..cf9f6830 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -20,7 +20,8 @@ type Contest struct { StartTime *time.Time EndTime *time.Time - Questions []*Question + Questions []*Question + Participants []*Participant `gorm:"many2many:subscriptions"` } 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 } else { 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 { return nil, err } @@ -51,7 +73,7 @@ func (c *Contest) Read(args map[string]string, r *http.Request) (interface{}, er 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 } diff --git a/orm/participant.go b/orm/participant.go index bf9ddb44..0b9d4c01 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -1,7 +1,9 @@ package orm import ( + "fmt" "net/http" + "strings" "git.andreafazzi.eu/andrea/oef/renderer" "github.com/jinzhu/gorm" @@ -9,23 +11,32 @@ import ( type Participant struct { gorm.Model - -// SelectedElement map[uint]string `gorm:"-"` -// AllElements []*Element `gorm:"-"` + + Firstname string + 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) 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) { if r.Method == "GET" { participant := new(Participant) - // if err := DB().Find(&participant.AllContests).Error; err != nil { - // return nil, err - // } + if err := DB().Find(&participant.AllContests).Error; err != nil { + return nil, err + } return participant, nil } else { participant := new(Participant) @@ -46,7 +57,7 @@ func (model *Participant) Read(args map[string]string, r *http.Request) (interfa 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 } @@ -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) { 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 participants, nil @@ -70,12 +81,14 @@ func (model *Participant) Update(args map[string]string, r *http.Request) (inter participant := result.(*Participant) - // if err := DB().Find(&participant.AllElements).Error; err != nil { - // return nil, err - // } + if err := DB().Find(&participant.AllContests).Error; err != nil { + return nil, err + } - // participant.SelectedElement = make(map[uint]string) - // participant.SelectedElement[participant.ElementID] = "selected" + participant.SelectedContest = make(map[uint]string) + for _, c := range participant.Contests { + participant.SelectedContest[c.ID] = "selected" + } return participant, nil } else { @@ -87,6 +100,11 @@ func (model *Participant) Update(args map[string]string, r *http.Request) (inter if err != nil { return nil, err } + + if err := DB().Where(participant.(*Participant).ContestIDs).Find(&participant.(*Participant).Contests).Error; err != nil { + return nil, err + } + _, err = SaveParticipant(participant) if err != nil { return nil, err @@ -118,7 +136,7 @@ func CreateParticipant(participant *Participant) (*Participant, 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 participant, nil diff --git a/renderer/funcmap.go b/renderer/funcmap.go index 78f4430e..4aef994b 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -50,11 +50,10 @@ var ( ) func trim(text string) string { - var result string if len(text) > MaxTextLength { - result = text[0:MaxTextLength] + return text[0:MaxTextLength] + "…" } - return result + "…" + return text } func modelName(value interface{}) string { diff --git a/templates/contests.html.tpl b/templates/contests.html.tpl index f90025f1..4023014a 100644 --- a/templates/contests.html.tpl +++ b/templates/contests.html.tpl @@ -16,7 +16,7 @@
{{range $element := .Data}} - + {{$element|string}}
{{$options := `noElements: "nessuna data"`}} diff --git a/templates/contests_show.html.tpl b/templates/contests_show.html.tpl index 949c35a7..7a0a8ee8 100644 --- a/templates/contests_show.html.tpl +++ b/templates/contests_show.html.tpl @@ -16,12 +16,27 @@ 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}}
- + +
+
+ + {{$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}} + +
+
+ {{ end }} diff --git a/templates/participants.html.tpl b/templates/participants.html.tpl index 6f0b7943..60ff5046 100644 --- a/templates/participants.html.tpl +++ b/templates/participants.html.tpl @@ -3,8 +3,8 @@
{{$options := ` - title: "Participants" - buttonTitle: "Crea nuovo Participant" + title: "Participanti" + buttonTitle: "Crea nuovo partecipante" `}} {{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Participant")}} @@ -19,8 +19,8 @@ {{$element|string}}
- {{$options := `noElements: "no subelements"`}} - {{/*template "small" dict "options" ($options | yaml) "data" $element.SubElements*/}} + {{$options := `noElements: "nessuna gara"`}} + {{template "small" dict "options" ($options | yaml) "data" $element.Contests}}
{{end}} diff --git a/templates/participants_add_update.html.tpl b/templates/participants_add_update.html.tpl index ad24151a..11098d1b 100644 --- a/templates/participants_add_update.html.tpl +++ b/templates/participants_add_update.html.tpl @@ -5,12 +5,12 @@ {{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}} -{{template "breadcrumb" toSlice "Participants" (all "Participant") "Aggiungi" "current"}} +{{template "breadcrumb" toSlice "Participanti" (all "Participant") "Aggiungi" "current"}} {{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"}}
+
+
+ {{$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}} +
+
+ {{$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}} +
+
+ + {{$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" } `}} {{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}} diff --git a/templates/participants_show.html.tpl b/templates/participants_show.html.tpl index 1bb95995..8fb217b0 100644 --- a/templates/participants_show.html.tpl +++ b/templates/participants_show.html.tpl @@ -2,22 +2,24 @@
- {{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")}} -

GENERAL SECTION

- +

Informazioni generali

+

+ Questa scheda contiene la informazioni relative al partecipante {{.Data|string}}. +

{{$options := ` - title: "RELATIONS" - model: "MODEL" - icon: "ICON_CLASS" + title: "Gare a cui il partecipante è iscritto" + model: "Contest" + icon: "fa fa-hourglass-start" `}} - {{$noElements := "NO ELEMENTS"}} - {{template "relation_list" dict "options" ($options|yaml) "data" .Data.RELATIONS "noElements" $noElements}} + {{$noElements := "Il partecipante non è iscritto ad alcuna gara."}} + {{template "relation_list" dict "options" ($options|yaml) "data" .Data.Contests "noElements" $noElements}}