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