oef/orm/participant.go

275 lines
6.8 KiB
Go

package orm
import (
"fmt"
"net/http"
"strings"
"git.andreafazzi.eu/andrea/oef/config"
"git.andreafazzi.eu/andrea/oef/i18n"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
)
type Participant struct {
gorm.Model
*Creator
UserID uint
Firstname string
Lastname string
FiscalCode string
SchoolID uint `schema:"school_id"`
User *User
School *School
Responses []*Response
ContestIDs []uint `schema:"contest_ids" gorm:"-"`
Contests []*Contest `gorm:"many2many:subscriptions"`
SelectedContest map[uint]string `gorm:"-"`
AllContests []*Contest `gorm:"-"`
SelectedSchool map[uint]string `gorm:"-"`
AllSchools []*School `gorm:"-"`
}
func (model *Participant) sanitize(s string) string {
lower := strings.ToLower(s)
r := strings.NewReplacer("'", "", "-", "", " ", "", "ò", "o", "ì", "i")
return r.Replace(lower)
}
func (model *Participant) username() string {
return strings.ToUpper(model.FiscalCode)
}
func (model *Participant) GetID() uint { return model.ID }
func (model *Participant) String() string {
return fmt.Sprintf("%s %s", strings.ToUpper(model.Lastname), strings.Title(strings.ToLower(model.Firstname)))
}
func setFlashMessage(w http.ResponseWriter, r *http.Request, key string) error {
session, err := store.Get(r, "flash-session")
if err != nil {
return err
}
session.AddFlash(i18n.FlashMessages[key][config.Config.Language])
err = session.Save(r, w)
if err != nil {
return err
}
return nil
}
func (model *Participant) exists() (*User, error) {
var user User
if err := DB().First(&user, &User{Username: model.username()}).Error; err != nil && err != gorm.ErrRecordNotFound {
return nil, err
} else if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &user, nil
}
func (model *Participant) BeforeSave(tx *gorm.DB) error {
var user User
if err := tx.FirstOrCreate(&user, &User{
Username: model.username(),
Role: "participant",
}).Error; err != nil {
return err
}
model.UserID = user.ID
return nil
}
func (model *Participant) AfterSave(tx *gorm.DB) error {
for _, contest := range model.Contests {
var response Response
if err := tx.FirstOrCreate(
&response,
&Response{
Name: fmt.Sprintf("%s (%s)", contest.Name, model.String()),
ContestID: contest.ID,
ParticipantID: model.UserID,
}).Error; err != nil {
return err
}
}
return nil
}
func (model *Participant) AfterDelete(tx *gorm.DB) error {
if err := tx.Unscoped().Delete(model.User).Error; err != nil {
return err
}
return nil
}
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 err := DB().Find(&participant.AllSchools).Error; err != nil {
return nil, err
}
return participant, nil
} else {
participant := new(Participant)
err := renderer.Decode(participant, r)
if err != nil {
return nil, err
}
if user, err := participant.exists(); err == nil && user != nil {
if err := DB().Where("user_id = ?", user.ID).Find(&participant).Error; err != nil {
return nil, err
}
err := setFlashMessage(w, r, "participantExists")
if err != nil {
return nil, err
}
return participant, nil
} else if err != nil {
return nil, err
}
participant.Creator = NewCreator(r)
participant, err = CreateParticipant(participant)
if err != nil {
return nil, err
}
return participant, nil
}
}
func (model *Participant) Read(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
var participant Participant
id := args["id"]
if err := DB().Preload("User").Preload("School").Preload("Responses").Preload("Contests").First(&participant, id).Error; err != nil {
return nil, err
}
return &participant, nil
}
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
}
return participants, nil
}
func (model *Participant) 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
}
participant := result.(*Participant)
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"
return participant, nil
} else {
participant, err := model.Read(args, w, r)
if err != nil {
return nil, err
}
err = renderer.Decode(participant, r)
if err != nil {
return nil, err
}
if user, err := participant.(*Participant).exists(); err == nil && user != nil {
if user.ID != participant.(*Participant).UserID {
err := setFlashMessage(w, r, "participantExists")
if err != nil {
return nil, err
}
return participant, nil
}
} else if err != nil {
return nil, err
}
if err := DB().Where(participant.(*Participant).ContestIDs).Find(&participant.(*Participant).Contests).Error; err != nil {
return nil, err
}
_, err = SaveParticipant(participant)
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, w, r)
if err != nil {
return nil, err
}
return participant.(*Participant), nil
}
}
func (model *Participant) Delete(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
participant, err := model.Read(args, w, r)
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().Where(participant.ContestIDs).Find(&participant.Contests).Error; err != nil {
return nil, err
}
if err := DB().Create(participant).Error; err != nil {
return nil, err
}
return participant, nil
}
func SaveParticipant(participant interface{}) (interface{}, error) {
participant.(*Participant).FiscalCode = strings.ToUpper(participant.(*Participant).FiscalCode)
if err := DB(). /*.Omit("Something")*/ Save(participant).Error; err != nil {
return nil, err
}
return participant, nil
}