From d19d1b04b979ebbb99afc43e8dbb3e4275e98954 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Mon, 18 Nov 2019 17:04:07 +0100 Subject: [PATCH] Add Response prototype --- main.go | 1 + orm/contest.go | 6 +- orm/participant.go | 5 + orm/response.go | 129 ++++++++++++++++++ templates/contests.html.tpl | 47 +++++-- templates/layout/read_all_header.html.tpl | 6 + .../layout/submit_cancel_button.html.tpl | 2 + templates/responses.html.tpl | 33 +++++ templates/responses_add_update.html.tpl | 55 ++++++++ templates/responses_show.html.tpl | 27 ++++ 10 files changed, 291 insertions(+), 20 deletions(-) create mode 100644 orm/response.go create mode 100644 templates/responses.html.tpl create mode 100644 templates/responses_add_update.html.tpl create mode 100644 templates/responses_show.html.tpl diff --git a/main.go b/main.go index 44ecb0d0..37aa8b96 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ var ( &orm.Answer{}, &orm.Contest{}, &orm.Participant{}, + &orm.Response{}, } ) diff --git a/orm/contest.go b/orm/contest.go index a122fd3a..76aee65e 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -96,11 +96,7 @@ func (c *Contest) ReadAll(args map[string]string, r *http.Request) (interface{}, participant := &Participant{} - if err := DB().Where("username = ?", claims["name"].(string)).First(&participant).Error; err != nil { - return nil, err - } - - if err := DB().Debug().Order("created_at").Find(&participant.Contests).Error; err != nil { + if err := DB().Preload("Contests").Where("username = ?", claims["name"].(string)).First(&participant).Error; err != nil { return nil, err } else { return participant.Contests, nil diff --git a/orm/participant.go b/orm/participant.go index 8f7151d6..d44fe93c 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -144,6 +144,11 @@ func (model *Participant) Update(args map[string]string, r *http.Request) (inter if err != nil { return nil, err } + + if err := DB().Model(participant).Association("Contests").Replace(participant.(*Participant).Contests).Error; err != nil { + return nil, err + } + participant, err = model.Read(args, nil) if err != nil { return nil, err diff --git a/orm/response.go b/orm/response.go new file mode 100644 index 00000000..3488406a --- /dev/null +++ b/orm/response.go @@ -0,0 +1,129 @@ +package orm + +import ( + "net/http" + + "git.andreafazzi.eu/andrea/oef/renderer" + "github.com/jinzhu/gorm" +) + +type Response struct { + gorm.Model + + Questions []*Question + + // SelectedElement map[uint]string `gorm:"-"` + // AllElements []*Element `gorm:"-"` +} + +func (model *Response) GetID() uint { return model.ID } + +func (model *Response) String() string { + return "" // Please implement this. +} + +func (model *Response) Create(args map[string]string, r *http.Request) (interface{}, error) { + if r.Method == "GET" { + response := new(Response) + contestID := r.URL.Query().Get("contest_id") + + if err := DB().Preload("Answers").Where("contest_id = ?", contestID).Find(&response.Questions).Error; err != nil { + return nil, err + } + return response, nil + } else { + response := new(Response) + err := renderer.Decode(response, r) + if err != nil { + return nil, err + } + response, err = CreateResponse(response) + if err != nil { + return nil, err + } + return response, nil + } +} + +func (model *Response) Read(args map[string]string, r *http.Request) (interface{}, error) { + var response Response + + id := args["id"] + + if err := DB(). /*.Preload("Something")*/ First(&response, id).Error; err != nil { + return nil, err + } + + return &response, nil +} + +func (model *Response) ReadAll(args map[string]string, r *http.Request) (interface{}, error) { + var responses []*Response + if err := DB(). /*.Preload("Something")*/ Order("created_at").Find(&responses).Error; err != nil { + return nil, err + } + return responses, nil +} + +func (model *Response) 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 + } + + response := result.(*Response) + + // if err := DB().Find(&response.AllElements).Error; err != nil { + // return nil, err + // } + + // response.SelectedElement = make(map[uint]string) + // response.SelectedElement[response.ElementID] = "selected" + + return response, nil + } else { + response, err := model.Read(args, nil) + if err != nil { + return nil, err + } + err = renderer.Decode(response, r) + if err != nil { + return nil, err + } + _, err = SaveResponse(response) + if err != nil { + return nil, err + } + response, err = model.Read(args, nil) + if err != nil { + return nil, err + } + return response.(*Response), nil + } +} + +func (model *Response) Delete(args map[string]string, r *http.Request) (interface{}, error) { + response, err := model.Read(args, nil) + if err != nil { + return nil, err + } + if err := DB().Unscoped().Delete(response.(*Response)).Error; err != nil { + return nil, err + } + return response.(*Response), nil +} + +func CreateResponse(response *Response) (*Response, error) { + if err := DB().Create(response).Error; err != nil { + return nil, err + } + return response, nil +} + +func SaveResponse(response interface{}) (interface{}, error) { + if err := DB(). /*.Omit("Something")*/ Save(response).Error; err != nil { + return nil, err + } + return response, nil +} diff --git a/templates/contests.html.tpl b/templates/contests.html.tpl index 4023014a..a1cc0ee5 100644 --- a/templates/contests.html.tpl +++ b/templates/contests.html.tpl @@ -2,10 +2,23 @@
-{{$options := ` + {{$admin := (.Claims|isAdmin)}} + {{$options := ""}} + + {{if $admin}} + + {{$options = ` title: "Gare" buttonTitle: "Crea nuova gara" - `}} + `}} + + {{else}} + + {{$options = ` + title: "Gare a cui sei iscritto" + `}} + + {{end}} {{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Contest")}} {{template "search_input"}} @@ -14,19 +27,23 @@ {{template "display_no_elements"}} {{else}} - + {{range $element := .Data}} + {{if $admin}} + + {{else}} + + {{$element|string}} +
+ {{$options := `noElements: "nessuna data"`}} + {{template "small" dict "options" ($options | yaml) "data" ($element.Date|prettyDate)}} +
+
+ {{end}} + {{end}} +
+ diff --git a/templates/layout/read_all_header.html.tpl b/templates/layout/read_all_header.html.tpl index 1b61d4b9..ccd80702 100644 --- a/templates/layout/read_all_header.html.tpl +++ b/templates/layout/read_all_header.html.tpl @@ -1,6 +1,7 @@ {{define "read_all_header"}}
+ {{if .options.buttonTitle}}

{{.options.title}} ({{.lengthData}})

@@ -9,6 +10,11 @@ {{template "create_button" dict "buttonTitle" .options.buttonTitle "modelPath" .modelPath}}
+ {{else}} +
+

{{.options.title}}

+
+ {{end}} {{end}} diff --git a/templates/layout/submit_cancel_button.html.tpl b/templates/layout/submit_cancel_button.html.tpl index d861f8be..f8295609 100644 --- a/templates/layout/submit_cancel_button.html.tpl +++ b/templates/layout/submit_cancel_button.html.tpl @@ -4,7 +4,9 @@ {{if .options.update}} Annulla {{else}} + {{if .options.cancelTitle}} Annulla {{end}} + {{end}} {{end}} diff --git a/templates/responses.html.tpl b/templates/responses.html.tpl new file mode 100644 index 00000000..2af97301 --- /dev/null +++ b/templates/responses.html.tpl @@ -0,0 +1,33 @@ +{{ define "content" }} + + +
+{{$options := ` + title: "Responses" + buttonTitle: "Crea nuovo Response" + `}} + + {{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Response")}} + {{template "search_input"}} + + {{if not .}} + {{template "display_no_elements"}} + {{else}} + + +
+ + +{{ end }} diff --git a/templates/responses_add_update.html.tpl b/templates/responses_add_update.html.tpl new file mode 100644 index 00000000..b4c54edd --- /dev/null +++ b/templates/responses_add_update.html.tpl @@ -0,0 +1,55 @@ +{{ define "content" }} +
+ {{$admin := (.Claims|isAdmin)}} + {{$update := .Options.Get "update"}} + + {{if $admin}} + + {{if $update}} + + {{template "breadcrumb" toSlice "Responses" (all "Response") (.Data|string) (.Data.ID|show "Response") "Aggiorna" "current"}} + {{else}} + {{template "breadcrumb" toSlice "Responses" (all "Response") "Aggiungi" "current"}} + {{end}} + + {{end}} + + {{template "add_update_header" dict "update" $update "addTitle" "Rispondi al questionario" "updateTitle" (printf "Aggiorna le tue risposte %s" (.Data|string))}} + {{$form := "form_add_update"}} +
+ + {{range $question := .Data.Questions}} +
+

{{$question.Text}}

+ {{range $answer := $question.Answers}} +
+ + +
+ {{end}} +
+ {{end}} + + {{if $admin}} + + {{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Response" } `}} + {{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}} + + {{else}} + + {{$options := ` { saveTitle: "Invia le risposte", model: "Response" } `}} + {{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}} + + {{end}} + +
+ +
+{{ end }} diff --git a/templates/responses_show.html.tpl b/templates/responses_show.html.tpl new file mode 100644 index 00000000..4cccf38d --- /dev/null +++ b/templates/responses_show.html.tpl @@ -0,0 +1,27 @@ +{{ define "content" }} + +
+ + {{template "breadcrumb" toSlice "ELEMENTS" (all "Response") (.Data|string) "current"}} + {{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Response") "deletePath" (.Data.ID|delete "Response")}} + +

GENERAL SECTION

+ +
+
+ + {{$options := ` + title: "RELATIONS" + model: "MODEL" + icon: "ICON_CLASS" + `}} + + {{$noElements := "NO ELEMENTS"}} + {{template "relation_list" dict "options" ($options|yaml) "data" .Data.RELATIONS "noElements" $noElements}} + +
+
+ +
+ +{{ end }}