Add Response prototype
This commit is contained in:
parent
a33cd9ac2d
commit
d19d1b04b9
10 changed files with 291 additions and 20 deletions
1
main.go
1
main.go
|
@ -30,6 +30,7 @@ var (
|
|||
&orm.Answer{},
|
||||
&orm.Contest{},
|
||||
&orm.Participant{},
|
||||
&orm.Response{},
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
129
orm/response.go
Normal file
129
orm/response.go
Normal file
|
@ -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
|
||||
}
|
|
@ -2,10 +2,23 @@
|
|||
|
||||
|
||||
<div class="container">
|
||||
{{$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}}
|
||||
<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-hourglass-start"></span>
|
||||
{{$element|string}}
|
||||
<div class="text-right">
|
||||
{{$options := `noElements: "nessuna data"`}}
|
||||
{{template "small" dict "options" ($options | yaml) "data" ($element.Date|prettyDate)}}
|
||||
</div>
|
||||
</a>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
{{range $element := .Data}}
|
||||
{{if $admin}}
|
||||
<a class="list-group-item list-group-item-action" href={{$element.ID|show "Contest"}}>
|
||||
{{else}}
|
||||
<a class="list-group-item list-group-item-action" href={{create "Response"}}{{"&contest_id="}}{{$element.ID}}>
|
||||
{{end}}
|
||||
<span class="fa fa-hourglass-start"></span>
|
||||
{{$element|string}}
|
||||
<div class="text-right">
|
||||
{{$options := `noElements: "nessuna data"`}}
|
||||
{{template "small" dict "options" ($options | yaml) "data" ($element.Date|prettyDate)}}
|
||||
</div>
|
||||
</a>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{{define "read_all_header"}}
|
||||
<div class="karmen-info-header">
|
||||
<div class="row">
|
||||
{{if .options.buttonTitle}}
|
||||
<div class="col-md-8">
|
||||
<h1>{{.options.title}} ({{.lengthData}})</h1>
|
||||
</div>
|
||||
|
@ -9,6 +10,11 @@
|
|||
{{template "create_button" dict "buttonTitle" .options.buttonTitle "modelPath" .modelPath}}
|
||||
</div>
|
||||
</div>
|
||||
{{else}}
|
||||
<div class="col-md-12">
|
||||
<h1>{{.options.title}}</h1>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
{{if .options.update}}
|
||||
<a href="{{.id|show .options.model}}" class="btn btn-default">Annulla</a>
|
||||
{{else}}
|
||||
{{if .options.cancelTitle}}
|
||||
<a href="{{all .options.model}}" class="btn btn-default">Annulla</a>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
|
|
33
templates/responses.html.tpl
Normal file
33
templates/responses.html.tpl
Normal file
|
@ -0,0 +1,33 @@
|
|||
{{ define "content" }}
|
||||
|
||||
|
||||
<div class="container">
|
||||
{{$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}}
|
||||
<div class="list-group" id="myUL">
|
||||
{{range $element := .Data}}
|
||||
<a class="list-group-item list-group-item-action" href={{$element.ID|show "Response"}}>
|
||||
<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*/}}
|
||||
</div>
|
||||
</a>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
{{ end }}
|
55
templates/responses_add_update.html.tpl
Normal file
55
templates/responses_add_update.html.tpl
Normal file
|
@ -0,0 +1,55 @@
|
|||
{{ define "content" }}
|
||||
<div class="container">
|
||||
{{$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"}}
|
||||
<form
|
||||
class="needs-validation"
|
||||
action="{{if $update}}{{.Data.ID|update "Response"}}{{else}}{{create "Response"}}{{end}}"
|
||||
method="POST"
|
||||
role="form"
|
||||
id={{$form}}>
|
||||
|
||||
{{range $question := .Data.Questions}}
|
||||
<div class="form-group p-3">
|
||||
<p class="lead">{{$question.Text}}</p>
|
||||
{{range $answer := $question.Answers}}
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="question_{{$question.ID}}" id="answer_{{$answer.ID}}" value="{{$answer.ID}}">
|
||||
<label class="form-check-label" for="answer_{{$answer.ID}}">
|
||||
{{$answer}}
|
||||
</label>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{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}}
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
{{ end }}
|
27
templates/responses_show.html.tpl
Normal file
27
templates/responses_show.html.tpl
Normal file
|
@ -0,0 +1,27 @@
|
|||
{{ define "content" }}
|
||||
|
||||
<div class="container">
|
||||
|
||||
{{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")}}
|
||||
|
||||
<h2 class="karmen-relation-header">GENERAL SECTION</h2>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
|
||||
{{$options := `
|
||||
title: "RELATIONS"
|
||||
model: "MODEL"
|
||||
icon: "ICON_CLASS"
|
||||
`}}
|
||||
|
||||
{{$noElements := "NO ELEMENTS"}}
|
||||
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.RELATIONS "noElements" $noElements}}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
{{ end }}
|
Loading…
Reference in a new issue