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
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
}

View file

@ -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

View file

@ -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 {

View file

@ -16,7 +16,7 @@
<div class="list-group" id="myUL">
{{range $element := .Data}}
<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}}
<div class="text-right">
{{$options := `noElements: "nessuna data"`}}

View file

@ -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}}
</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>
{{ end }}

View file

@ -3,8 +3,8 @@
<div class="container">
{{$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 @@
<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*/}}
{{$options := `noElements: "nessuna gara"`}}
{{template "small" dict "options" ($options | yaml) "data" $element.Contests}}
</div>
</a>
{{end}}

View file

@ -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"}}
<form
class="needs-validation"
@ -19,6 +19,20 @@
role="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" } `}}
{{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}

View file

@ -2,22 +2,24 @@
<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")}}
<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="col-md-12">
{{$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}}
</div>
</div>