oef/orm/participant.go

179 lines
4.2 KiB
Go
Raw Normal View History

2019-11-14 15:04:11 +01:00
package orm
import (
2019-11-15 10:41:32 +01:00
"fmt"
2019-11-14 15:04:11 +01:00
"net/http"
2019-11-15 10:41:32 +01:00
"strings"
2019-11-14 15:04:11 +01:00
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
"github.com/sethvargo/go-password/password"
2019-11-14 15:04:11 +01:00
)
type Participant struct {
gorm.Model
2019-11-15 10:41:32 +01:00
Firstname string
Lastname string
Username string
Password string
ContestIDs []uint `schema:"contest_ids" gorm:"-"`
Contests []*Contest `gorm:"many2many:subscriptions"`
SelectedContest map[uint]string `gorm:"-"`
AllContests []*Contest `gorm:"-"`
2019-11-14 15:04:11 +01:00
}
func (model *Participant) sanitize(s string) string {
lower := strings.ToLower(s)
r := strings.NewReplacer("'", "", "-", "", " ", "", "ò", "o", "ì", "i")
return r.Replace(lower)
}
func (model *Participant) genUsername() error {
model.Username = strings.Join([]string{model.sanitize(model.Firstname), model.sanitize(model.Lastname)}, ".")
return nil
}
func (model *Participant) genPassword() error {
password, err := password.Generate(8, 2, 0, false, true)
if err != nil {
return err
}
model.Password = password
return nil
}
2019-11-14 15:04:11 +01:00
func (model *Participant) GetID() uint { return model.ID }
func (model *Participant) String() string {
2019-11-15 10:41:32 +01:00
return fmt.Sprintf("%s %s", strings.ToUpper(model.Lastname), strings.Title(strings.ToLower(model.Firstname)))
2019-11-14 15:04:11 +01:00
}
func (model *Participant) BeforeSave(tx *gorm.DB) error {
if err := model.genUsername(); err != nil {
return err
}
if model.Password == "" {
if err := model.genPassword(); err != nil {
return err
}
}
return nil
}
2019-11-14 15:04:11 +01:00
func (model *Participant) Create(args map[string]string, r *http.Request) (interface{}, error) {
if r.Method == "GET" {
participant := new(Participant)
2019-11-15 10:41:32 +01:00
if err := DB().Find(&participant.AllContests).Error; err != nil {
return nil, err
}
2019-11-14 15:04:11 +01:00
return participant, nil
} else {
participant := new(Participant)
err := renderer.Decode(participant, r)
if err != nil {
return nil, err
}
participant, err = CreateParticipant(participant)
if err != nil {
return nil, err
}
return participant, nil
}
}
func (model *Participant) Read(args map[string]string, r *http.Request) (interface{}, error) {
var participant Participant
id := args["id"]
2019-11-15 10:41:32 +01:00
if err := DB().Preload("Contests").First(&participant, id).Error; err != nil {
2019-11-14 15:04:11 +01:00
return nil, err
}
return &participant, nil
}
func (model *Participant) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
var participants []*Participant
2019-11-15 10:41:32 +01:00
if err := DB().Preload("Contests").Order("created_at").Find(&participants).Error; err != nil {
2019-11-14 15:04:11 +01:00
return nil, err
}
return participants, nil
}
func (model *Participant) 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
}
participant := result.(*Participant)
2019-11-15 10:41:32 +01:00
if err := DB().Find(&participant.AllContests).Error; err != nil {
return nil, err
}
2019-11-14 15:04:11 +01:00
2019-11-15 10:41:32 +01:00
participant.SelectedContest = make(map[uint]string)
for _, c := range participant.Contests {
participant.SelectedContest[c.ID] = "selected"
}
2019-11-14 15:04:11 +01:00
return participant, nil
} else {
participant, err := model.Read(args, nil)
if err != nil {
return nil, err
}
err = renderer.Decode(participant, r)
if err != nil {
return nil, err
}
2019-11-15 10:41:32 +01:00
if err := DB().Where(participant.(*Participant).ContestIDs).Find(&participant.(*Participant).Contests).Error; err != nil {
return nil, err
}
2019-11-14 15:04:11 +01:00
_, err = SaveParticipant(participant)
if err != nil {
return nil, err
}
participant, err = model.Read(args, nil)
if err != nil {
return nil, err
}
return participant.(*Participant), nil
}
}
func (model *Participant) Delete(args map[string]string, r *http.Request) (interface{}, error) {
participant, err := model.Read(args, nil)
if err != nil {
return nil, err
}
if err := DB().Unscoped().Delete(participant.(*Participant)).Error; err != nil {
return nil, err
}
return participant.(*Participant), nil
}
func CreateParticipant(participant *Participant) (*Participant, error) {
if err := DB().Create(participant).Error; err != nil {
return nil, err
}
return participant, nil
}
func SaveParticipant(participant interface{}) (interface{}, error) {
2019-11-15 10:41:32 +01:00
if err := DB(). /*.Omit("Something")*/ Save(participant).Error; err != nil {
2019-11-14 15:04:11 +01:00
return nil, err
}
return participant, nil
}