Add errors package and improve style

This commit is contained in:
Andrea Fazzi 2019-12-02 10:48:46 +01:00
parent 43dd305835
commit 2fb6cda55c
8 changed files with 47 additions and 9 deletions

2
dist/main.bundle.js vendored

File diff suppressed because one or more lines are too long

4
dist/styles.css vendored
View file

@ -15443,4 +15443,8 @@ ul.karmen-related-elements {
color: white; color: white;
} }
.uppercase:valid {
text-transform: uppercase;
}
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJzdHlsZXMuY3NzIiwic291cmNlUm9vdCI6IiJ9*/ /*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJzdHlsZXMuY3NzIiwic291cmNlUm9vdCI6IiJ9*/

5
errors/errors.go Normal file
View file

@ -0,0 +1,5 @@
package errors
import "errors"
var RecordExists = errors.New("Record already exists!")

View file

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"git.andreafazzi.eu/andrea/oef/errors"
"git.andreafazzi.eu/andrea/oef/renderer" "git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
@ -38,7 +39,8 @@ func (model *Participant) sanitize(s string) string {
} }
func (model *Participant) username() string { func (model *Participant) username() string {
return strings.Join([]string{model.sanitize(model.Firstname), model.sanitize(model.Lastname)}, ".") // return strings.Join([]string{model.sanitize(model.Firstname), model.sanitize(model.Lastname)}, ".")
return strings.ToUpper(model.FiscalCode)
} }
func (model *Participant) GetID() uint { return model.ID } func (model *Participant) GetID() uint { return model.ID }
@ -48,8 +50,20 @@ func (model *Participant) String() string {
} }
func (model *Participant) BeforeSave(tx *gorm.DB) error { func (model *Participant) BeforeSave(tx *gorm.DB) error {
users := make([]*User, 0)
if err := tx.Where("username = ?", model.FiscalCode).Find(&users).Error; err != nil {
return err
}
if len(users) > 0 {
return errors.RecordExists
}
var user User var user User
if err := tx.FirstOrCreate(&user, &User{Username: model.username()}).Error; err != nil { if err := tx.FirstOrCreate(&user, &User{
Username: model.username(),
Role: "participant",
}).Error; err != nil {
return err return err
} }
model.UserID = user.ID model.UserID = user.ID
@ -92,6 +106,7 @@ func (model *Participant) Create(args map[string]string, r *http.Request) (inter
if err != nil { if err != nil {
return nil, err return nil, err
} }
participant, err = CreateParticipant(participant) participant, err = CreateParticipant(participant)
if err != nil { if err != nil {
return nil, err return nil, err
@ -192,6 +207,8 @@ func CreateParticipant(participant *Participant) (*Participant, error) {
} }
func SaveParticipant(participant interface{}) (interface{}, error) { func SaveParticipant(participant interface{}) (interface{}, error) {
participant.(*Participant).FiscalCode = strings.ToUpper(participant.(*Participant).FiscalCode)
if err := DB(). /*.Omit("Something")*/ Save(participant).Error; err != nil { if err := DB(). /*.Omit("Something")*/ Save(participant).Error; err != nil {
return nil, err return nil, err
} }

View file

@ -54,7 +54,7 @@ var (
) )
func username(claims jwt.MapClaims) string { func username(claims jwt.MapClaims) string {
return claims["name"].(string) return claims["username"].(string)
} }
func isAdmin(claims jwt.MapClaims) bool { func isAdmin(claims jwt.MapClaims) bool {

View file

@ -36,6 +36,7 @@ type htmlTemplateData struct {
Data interface{} Data interface{}
Options url.Values Options url.Values
Claims jwt.MapClaims Claims jwt.MapClaims
Error error
} }
type JsonResponse struct { type JsonResponse struct {
@ -240,12 +241,12 @@ func (rend *HTMLRenderer) writeError(w http.ResponseWriter, r *http.Request, dat
func (rend *HTMLRenderer) Render(w http.ResponseWriter, r *http.Request, data interface{}, options ...url.Values) { func (rend *HTMLRenderer) Render(w http.ResponseWriter, r *http.Request, data interface{}, options ...url.Values) {
if data != nil && isErrorType(data) { if data != nil && isErrorType(data) {
rend.writeError(w, r, &htmlTemplateData{data.(error), nil, nil}) rend.writeError(w, r, &htmlTemplateData{data.(error), nil, nil, data.(error)})
} else { } else {
t, ok := rend.templates[options[0]["tpl_content"][0]] t, ok := rend.templates[options[0]["tpl_content"][0]]
if !ok { if !ok {
err := fmt.Errorf("Template %s not found", options[0]["tpl_content"][0]) err := fmt.Errorf("Template %s not found", options[0]["tpl_content"][0])
rend.writeError(w, r, &htmlTemplateData{err, nil, nil}) rend.writeError(w, r, &htmlTemplateData{err, nil, nil, err})
} }
var claims jwt.MapClaims var claims jwt.MapClaims
@ -255,9 +256,9 @@ func (rend *HTMLRenderer) Render(w http.ResponseWriter, r *http.Request, data in
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := t.ExecuteTemplate(w, options[0]["tpl_layout"][0], &htmlTemplateData{data, options[0], claims}) err := t.ExecuteTemplate(w, options[0]["tpl_layout"][0], &htmlTemplateData{data, options[0], claims, nil})
if err != nil { if err != nil {
rend.writeError(w, r, &htmlTemplateData{err, nil, nil}) rend.writeError(w, r, &htmlTemplateData{err, nil, nil, err})
} }
} }
} }

View file

@ -93,3 +93,7 @@ ul.karmen-related-elements {
background-color: black; background-color: black;
color: white; color: white;
} }
.uppercase:valid {
text-transform: uppercase;
}

View file

@ -30,7 +30,14 @@
</div> </div>
</div> </div>
{{$options := ` { name: "FiscalCode",id: "participant_fiscalcode",label: "Codice fiscale del partecipante",placeholder: "Inserire il codice fiscale",type: "text",required: "true"} `}} {{$options := `
name: "FiscalCode"
id: "participant_fiscalcode"
label: "Codice fiscale del partecipante"
placeholder: "Inserire il codice fiscale"
type: "text"
inputClass: "form-control uppercase"
required: "true" `}}
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "FiscalCode") "update" $update}} {{template "input" dict "options" ($options|yaml) "value" (.Data|field "FiscalCode") "update" $update}}
{{$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"}`}}