diff --git a/handlers/role.go b/handlers/role.go index 0056f0dd..ab5c4776 100644 --- a/handlers/role.go +++ b/handlers/role.go @@ -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}, }, diff --git a/main.go b/main.go index c7fea16b..9cb4eaa8 100644 --- a/main.go +++ b/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) diff --git a/orm/category.go b/orm/category.go new file mode 100644 index 00000000..3004370a --- /dev/null +++ b/orm/category.go @@ -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 +} diff --git a/orm/orm.go b/orm/orm.go index f4440a21..6e08b0be 100644 --- a/orm/orm.go +++ b/orm/orm.go @@ -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 } diff --git a/orm/participant.go b/orm/participant.go index 4a7682bf..38ea04f7 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -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 diff --git a/renderer/funcmap.go b/renderer/funcmap.go index 5e144897..8cfc6a8f 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -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" } diff --git a/templates/categories.html.tpl b/templates/categories.html.tpl new file mode 100644 index 00000000..0ed9bb8e --- /dev/null +++ b/templates/categories.html.tpl @@ -0,0 +1,33 @@ +{{ define "content" }} + + +