Add categories
This commit is contained in:
parent
8f02b2eb5f
commit
06b9cfa0db
13 changed files with 353 additions and 43 deletions
|
@ -21,7 +21,7 @@ var (
|
|||
},
|
||||
|
||||
"school": map[string][]int{
|
||||
"Participant": []int{PermissionCreate, PermissionRead, PermissionUpdate, PermissionDelete},
|
||||
"Participant": []int{PermissionCreate, PermissionRead, PermissionReadAll, PermissionUpdate, PermissionDelete},
|
||||
"School": []int{PermissionRead, PermissionUpdate},
|
||||
},
|
||||
|
||||
|
|
4
main.go
4
main.go
|
@ -33,6 +33,7 @@ var (
|
|||
&orm.School{},
|
||||
&orm.Response{},
|
||||
&orm.User{},
|
||||
&orm.Category{},
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -70,6 +71,9 @@ func main() {
|
|||
orm.AutoMigrate(models...)
|
||||
}
|
||||
|
||||
log.Println("Eventually write categories on DB...")
|
||||
orm.CreateCategories()
|
||||
|
||||
log.Println("Map models <-> handlers")
|
||||
if err := orm.MapHandlers(models); err != nil {
|
||||
panic(err)
|
||||
|
|
124
orm/category.go
Normal file
124
orm/category.go
Normal file
|
@ -0,0 +1,124 @@
|
|||
package orm
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.andreafazzi.eu/andrea/oef/renderer"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
gorm.Model
|
||||
|
||||
Name string
|
||||
}
|
||||
|
||||
func (model *Category) GetID() uint { return model.ID }
|
||||
|
||||
func (model *Category) String() string {
|
||||
return model.Name
|
||||
}
|
||||
|
||||
func (model *Category) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
category := new(Category)
|
||||
// if err := DB().Find(&category.AllContests).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
return category, nil
|
||||
} else {
|
||||
category := new(Category)
|
||||
err := renderer.Decode(category, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
category, err = CreateCategory(category)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return category, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (model *Category) Read(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
var category Category
|
||||
|
||||
id := args["id"]
|
||||
|
||||
if err := DB(). /*.Preload("Something")*/ First(&category, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &category, nil
|
||||
}
|
||||
|
||||
func (model *Category) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
var categories []*Category
|
||||
if err := DB(). /*.Preload("Something")*/ Order("created_at").Find(&categories).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
func (model *Category) Update(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
if r.Method == "GET" {
|
||||
result, err := model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
category := result.(*Category)
|
||||
|
||||
// if err := DB().Find(&category.AllElements).Error; err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// category.SelectedElement = make(map[uint]string)
|
||||
// category.SelectedElement[category.ElementID] = "selected"
|
||||
|
||||
return category, nil
|
||||
} else {
|
||||
category, err := model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = renderer.Decode(category, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = SaveCategory(category)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
category, err = model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return category.(*Category), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (model *Category) Delete(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
category, err := model.Read(args, w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Unscoped().Delete(category.(*Category)).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return category.(*Category), nil
|
||||
}
|
||||
|
||||
func CreateCategory(category *Category) (*Category, error) {
|
||||
if err := DB().Create(category).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return category, nil
|
||||
}
|
||||
|
||||
func SaveCategory(category interface{}) (interface{}, error) {
|
||||
if err := DB(). /*.Omit("Something")*/ Save(category).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return category, nil
|
||||
}
|
|
@ -46,6 +46,15 @@ func AutoMigrate(models ...interface{}) {
|
|||
|
||||
}
|
||||
|
||||
func CreateCategories() {
|
||||
for _, name := range []string{"Junior", "Senior"} {
|
||||
var category Category
|
||||
if err := currDB.FirstOrCreate(&category, Category{Name: name}).Error; err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Use(db *gorm.DB) {
|
||||
currDB = db
|
||||
}
|
||||
|
|
|
@ -27,16 +27,21 @@ type Participant struct {
|
|||
|
||||
FiscalCode string
|
||||
|
||||
SchoolID uint `schema:"school_id"`
|
||||
CategoryID uint `schema:"category_id"`
|
||||
SchoolID uint `schema:"school_id"`
|
||||
|
||||
User *User
|
||||
School *School
|
||||
User *User
|
||||
School *School
|
||||
Category *Category
|
||||
|
||||
Responses []*Response
|
||||
|
||||
ContestIDs []uint `schema:"contest_ids" gorm:"-"`
|
||||
Contests []*Contest `gorm:"many2many:subscriptions"`
|
||||
|
||||
SelectedCategory map[uint]string `gorm:"-"`
|
||||
AllCategories []*Category `gorm:"-"`
|
||||
|
||||
SelectedContest map[uint]string `gorm:"-"`
|
||||
AllContests []*Contest `gorm:"-"`
|
||||
|
||||
|
@ -121,19 +126,17 @@ func (model *Participant) AfterDelete(tx *gorm.DB) error {
|
|||
func (model *Participant) Create(args map[string]string, w http.ResponseWriter, 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 isSchool(r) {
|
||||
schoolID, err := strconv.Atoi(getUserIDFromToken(r))
|
||||
if err != nil {
|
||||
if err := DB().Find(&participant.AllCategories).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Find(&participant.AllSchools, schoolID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
} else {
|
||||
if err := DB().Find(&participant.AllCategories).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Find(&participant.AllContests).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Find(&participant.AllSchools).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -161,6 +164,13 @@ func (model *Participant) Create(args map[string]string, w http.ResponseWriter,
|
|||
|
||||
participant.UserModifierCreate = NewUserModifierCreate(r)
|
||||
|
||||
if isSchool(r) {
|
||||
schoolID, err := strconv.Atoi(getUserIDFromToken(r))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
participant.SchoolID = uint(schoolID)
|
||||
}
|
||||
participant, err = CreateParticipant(participant)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -184,10 +194,15 @@ func (model *Participant) Read(args map[string]string, w http.ResponseWriter, r
|
|||
setFlashMessage(w, r, "notAuthorized")
|
||||
return nil, errors.NotAuthorized
|
||||
}
|
||||
}
|
||||
|
||||
if err := DB().Preload("User").Preload("School").Preload("Responses").Preload("Contests").First(&participant, id).Error; err != nil {
|
||||
return nil, err
|
||||
if err := DB().Preload("User").Preload("School").Preload("Category").First(&participant, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
} else {
|
||||
if err := DB().Preload("User").Preload("School").Preload("Responses").Preload("Contests").Preload("Category").First(&participant, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &participant, nil
|
||||
|
@ -195,8 +210,20 @@ func (model *Participant) Read(args map[string]string, w http.ResponseWriter, r
|
|||
|
||||
func (model *Participant) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
var participants []*Participant
|
||||
if err := DB().Preload("School").Preload("Contests").Preload("Responses").Order("created_at").Find(&participants).Error; err != nil {
|
||||
return nil, err
|
||||
|
||||
// School user can access to its participants only!
|
||||
if isSchool(r) {
|
||||
schoolId, err := strconv.Atoi(getUserIDFromToken(r))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := DB().Preload("Category").Preload("School").Preload("Contests").Order("lastname").Find(&participants, &Participant{SchoolID: uint(schoolId)}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := DB().Preload("School").Preload("Contests").Preload("Responses").Order("created_at").Find(&participants).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return participants, nil
|
||||
}
|
||||
|
@ -210,22 +237,30 @@ func (model *Participant) Update(args map[string]string, w http.ResponseWriter,
|
|||
|
||||
participant := result.(*Participant)
|
||||
|
||||
if err := DB().Find(&participant.AllContests).Error; err != nil {
|
||||
return nil, err
|
||||
if isSchool(r) {
|
||||
if err := DB().Find(&participant.AllCategories).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
participant.SelectedCategory = make(map[uint]string)
|
||||
participant.SelectedCategory[participant.CategoryID] = "selected"
|
||||
} else {
|
||||
if err := DB().Find(&participant.AllContests).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
participant.SelectedContest = make(map[uint]string)
|
||||
for _, c := range participant.Contests {
|
||||
participant.SelectedContest[c.ID] = "selected"
|
||||
}
|
||||
|
||||
if err := DB().Find(&participant.AllSchools).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
participant.SelectedSchool = make(map[uint]string)
|
||||
participant.SelectedSchool[participant.SchoolID] = "selected"
|
||||
}
|
||||
|
||||
participant.SelectedContest = make(map[uint]string)
|
||||
for _, c := range participant.Contests {
|
||||
participant.SelectedContest[c.ID] = "selected"
|
||||
}
|
||||
|
||||
if err := DB().Find(&participant.AllSchools).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
participant.SelectedSchool = make(map[uint]string)
|
||||
participant.SelectedSchool[participant.SchoolID] = "selected"
|
||||
|
||||
return participant, nil
|
||||
} else {
|
||||
participant, err := model.Read(args, w, r)
|
||||
|
@ -296,7 +331,7 @@ func CreateParticipant(participant *Participant) (*Participant, error) {
|
|||
func SaveParticipant(participant interface{}) (interface{}, error) {
|
||||
participant.(*Participant).FiscalCode = strings.ToUpper(participant.(*Participant).FiscalCode)
|
||||
|
||||
if err := DB().Omit("School").Save(participant).Error; err != nil {
|
||||
if err := DB().Omit("Category", "School").Save(participant).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return participant, nil
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"html/template"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -56,6 +57,7 @@ var (
|
|||
"isSubscriber": isSubscriber,
|
||||
"isSchool": isSchool,
|
||||
"attr": attr,
|
||||
"userId": userId,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -63,6 +65,14 @@ func username(claims jwt.MapClaims) string {
|
|||
return claims["username"].(string)
|
||||
}
|
||||
|
||||
func userId(claims jwt.MapClaims) (uint, error) {
|
||||
id, err := strconv.Atoi(claims["user_id"].(string))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint(id), nil
|
||||
}
|
||||
|
||||
func isAdmin(claims jwt.MapClaims) bool {
|
||||
return claims["role"].(string) == "administrator"
|
||||
}
|
||||
|
|
33
templates/categories.html.tpl
Normal file
33
templates/categories.html.tpl
Normal file
|
@ -0,0 +1,33 @@
|
|||
{{ define "content" }}
|
||||
|
||||
|
||||
<div class="container">
|
||||
{{$options := `
|
||||
title: "Categories"
|
||||
buttonTitle: "Crea nuovo Category"
|
||||
`}}
|
||||
|
||||
{{template "read_all_header" dict "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Category")}}
|
||||
{{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 "Category"}}>
|
||||
<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 }}
|
28
templates/categories_add_update.html.tpl
Normal file
28
templates/categories_add_update.html.tpl
Normal file
|
@ -0,0 +1,28 @@
|
|||
{{ define "content" }}
|
||||
<div class="container">
|
||||
|
||||
{{$update := .Options.Get "update"}}
|
||||
|
||||
{{if $update}}
|
||||
|
||||
{{template "breadcrumb" toSlice "Categories" (all "Category") (.Data|string) (.Data.ID|show "Category") "Aggiorna" "current"}}
|
||||
{{else}}
|
||||
{{template "breadcrumb" toSlice "Categories" (all "Category") "Aggiungi" "current"}}
|
||||
{{end}}
|
||||
|
||||
{{template "add_update_header" dict "update" $update "addTitle" "Crea nuovo ELEMENTO" "updateTitle" (printf "Aggiorna ELEMENTO %s" (.Data|string))}}
|
||||
{{$form := "form_add_update"}}
|
||||
<form
|
||||
class="needs-validation"
|
||||
action="{{if $update}}{{.Data.ID|update "Category"}}{{else}}{{create "Category"}}{{end}}"
|
||||
method="POST"
|
||||
role="form"
|
||||
id={{$form}}>
|
||||
|
||||
{{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Category" } `}}
|
||||
{{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
{{ end }}
|
27
templates/categories_show.html.tpl
Normal file
27
templates/categories_show.html.tpl
Normal file
|
@ -0,0 +1,27 @@
|
|||
{{ define "content" }}
|
||||
|
||||
<div class="container">
|
||||
|
||||
{{template "breadcrumb" toSlice "ELEMENTS" (all "Category") (.Data|string) "current"}}
|
||||
{{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Category") "deletePath" (.Data.ID|delete "Category")}}
|
||||
|
||||
<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 }}
|
|
@ -42,6 +42,11 @@
|
|||
<a class="nav-item nav-link {{.Options|active "School"}}" href="{{all "School"}}">Scuole</a>
|
||||
<a class="nav-item nav-link {{.Options|active "Participant"}}" href="{{all "Participant"}}">Partecipanti</a>
|
||||
{{end}}
|
||||
{{if $isSchool}}
|
||||
<a class="nav-item nav-link {{.Options|active "School"}}" href="{{.Claims|userId|show "School"}}">Scuola</a>
|
||||
<a class="nav-item nav-link {{.Options|active "Participant"}}" href="{{all "Participant"}}">Partecipanti</a>
|
||||
{{end}}
|
||||
|
||||
</ul>
|
||||
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
data-live-search="true"
|
||||
form="{{.form}}"
|
||||
title="{{.options.title}}"
|
||||
data-dropup-auto="false" {{if .options.multiple}}multiple{{end}}>
|
||||
data-dropup-auto="false" {{if .options.multiple}}multiple{{end}} {{if .options.required}}required{{end}}>
|
||||
<option value="0"></option>
|
||||
{{range $el := .data}}
|
||||
{{if $.update}}
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
{{ define "content" }}
|
||||
|
||||
{{$isAdmin := .Claims|isAdmin}}
|
||||
{{$isSchool := .Claims|isSchool}}
|
||||
|
||||
<div class="container">
|
||||
|
||||
{{$update := .Options.Get "update"}}
|
||||
|
@ -40,12 +44,31 @@
|
|||
required: "true" `}}
|
||||
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "FiscalCode") "update" $update}}
|
||||
|
||||
{{$options := `
|
||||
name: "category_id"
|
||||
id: "category_id"
|
||||
label: "Categoria del partecipante"
|
||||
title: "Seleziona la categoria"
|
||||
required: "true"
|
||||
`}}
|
||||
{{template "select" dict "options" ($options|yaml) "data" (.Data|field "AllCategories") "selected" (.Data|field "SelectedCategory") "update" $update "form" $form}}
|
||||
|
||||
{{if $isAdmin}}
|
||||
{{$options := ` { name: "school_id", id: "school_id", label: "Scuola del partecipante", title: "Seleziona la scuola"}`}}
|
||||
{{template "select" dict "options" ($options|yaml) "data" (.Data|field "AllSchools") "selected" (.Data|field "SelectedSchool") "update" $update "form" $form}}
|
||||
|
||||
{{$options := ` { name: "contest_ids", id: "contest_ids", label: "Gare a cui il partecipante è iscritto", title: "Seleziona le gare", multiple: "true"}`}}
|
||||
{{$options := `
|
||||
name: "contest_ids"
|
||||
id: "contest_ids"
|
||||
label: "Gare a cui il partecipante è iscritto"
|
||||
title: "Seleziona le gare"
|
||||
multiple: "true"
|
||||
required: "true"
|
||||
`}}
|
||||
{{template "select" dict "options" ($options|yaml) "data" (.Data|field "AllContests") "selected" (.Data|field "SelectedContest") "update" $update "form" $form}}
|
||||
|
||||
{{end}}
|
||||
|
||||
{{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Participant" } `}}
|
||||
{{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}
|
||||
|
||||
|
|
|
@ -5,14 +5,8 @@
|
|||
|
||||
<div class="container">
|
||||
|
||||
{{if $isAdmin}}
|
||||
|
||||
{{template "breadcrumb" toSlice "Partecipanti" (all "Participant") (.Data|string) "current"}}
|
||||
{{end}}
|
||||
|
||||
{{if $isSchool}}
|
||||
{{template "breadcrumb" toSlice (.Data.School|string) (.Data.SchoolID|show "School") (.Data|string) "current"}}
|
||||
{{end}}
|
||||
|
||||
{{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Participant") "deletePath" (.Data.ID|delete "Participant")}}
|
||||
|
||||
<h2 class="karmen-relation-header">Informazioni generali</h2>
|
||||
|
@ -51,6 +45,22 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
|
||||
{{$options := `
|
||||
title: "Categoria del partecipante"
|
||||
model: "Category"
|
||||
icon: "fa fa-object-group"
|
||||
`}}
|
||||
|
||||
{{$noElements := "Il partecipante non appartiene ad alcuna categoria."}}
|
||||
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.Category "noElements" $noElements}}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{if $isAdmin}}
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
|
||||
|
@ -81,6 +91,8 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{{end}}
|
||||
|
||||
</div>
|
||||
|
||||
{{ end }}
|
||||
|
|
Loading…
Reference in a new issue