2019-11-14 15:04:11 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
2019-11-15 10:41:32 +01:00
|
|
|
"fmt"
|
2020-01-10 12:56:25 +01:00
|
|
|
|
2019-11-14 15:04:11 +01:00
|
|
|
"net/http"
|
2019-12-13 08:32:20 +01:00
|
|
|
"strconv"
|
2019-11-15 10:41:32 +01:00
|
|
|
"strings"
|
2019-11-14 15:04:11 +01:00
|
|
|
|
2019-12-07 11:44:19 +01:00
|
|
|
"git.andreafazzi.eu/andrea/oef/errors"
|
2019-11-14 15:04:11 +01:00
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
2020-01-10 12:56:25 +01:00
|
|
|
type ContestIDs []uint
|
|
|
|
|
2019-11-14 15:04:11 +01:00
|
|
|
type Participant struct {
|
|
|
|
gorm.Model
|
2019-11-15 10:41:32 +01:00
|
|
|
|
2020-01-27 08:35:37 +01:00
|
|
|
UserModifier
|
2019-12-06 10:59:00 +01:00
|
|
|
|
2019-11-29 12:44:43 +01:00
|
|
|
UserID uint
|
|
|
|
|
2019-11-15 10:41:32 +01:00
|
|
|
Firstname string
|
|
|
|
Lastname string
|
|
|
|
|
2020-01-11 08:03:56 +01:00
|
|
|
Password string `gorm:"-"`
|
|
|
|
|
2019-11-29 12:44:43 +01:00
|
|
|
FiscalCode string
|
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
CategoryID uint `schema:"category_id"`
|
|
|
|
SchoolID uint `schema:"school_id"`
|
2019-12-03 12:21:28 +01:00
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
User *User
|
|
|
|
School *School
|
|
|
|
Category *Category
|
2019-11-15 10:41:32 +01:00
|
|
|
|
2019-11-22 11:16:27 +01:00
|
|
|
Responses []*Response
|
|
|
|
|
2020-01-10 12:56:25 +01:00
|
|
|
ContestIDs ContestIDs `schema:"contest_ids" gorm:"-"`
|
2019-11-15 10:41:32 +01:00
|
|
|
Contests []*Contest `gorm:"many2many:subscriptions"`
|
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
SelectedCategory map[uint]string `gorm:"-"`
|
|
|
|
AllCategories []*Category `gorm:"-"`
|
|
|
|
|
2019-11-15 10:41:32 +01:00
|
|
|
SelectedContest map[uint]string `gorm:"-"`
|
|
|
|
AllContests []*Contest `gorm:"-"`
|
2019-12-03 12:21:28 +01:00
|
|
|
|
|
|
|
SelectedSchool map[uint]string `gorm:"-"`
|
|
|
|
AllSchools []*School `gorm:"-"`
|
2019-11-14 15:04:11 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:56:25 +01:00
|
|
|
func (ids *ContestIDs) UnmarshalCSV(csv string) error {
|
|
|
|
splits := strings.Split(csv, ",")
|
|
|
|
for _, s := range splits {
|
|
|
|
id, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*ids = append(*ids, uint(id))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-18 12:40:28 +01:00
|
|
|
func (model *Participant) sanitize(s string) string {
|
|
|
|
lower := strings.ToLower(s)
|
|
|
|
r := strings.NewReplacer("'", "", "-", "", " ", "", "ò", "o", "ì", "i")
|
|
|
|
return r.Replace(lower)
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:44:43 +01:00
|
|
|
func (model *Participant) username() string {
|
2019-12-02 10:48:46 +01:00
|
|
|
return strings.ToUpper(model.FiscalCode)
|
2019-11-18 12:40:28 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-27 11:43:56 +01:00
|
|
|
func (model *Participant) SetCreatorID(id uint) {
|
|
|
|
model.CreatorID = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Participant) SetCreatorRole(role string) {
|
|
|
|
model.CreatorRole = role
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Participant) SetCreatorIP(addr string) {
|
|
|
|
model.CreatorIP = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Participant) SetUpdaterID(id uint) {
|
|
|
|
model.UpdaterID = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Participant) SetUpdaterRole(role string) {
|
|
|
|
model.UpdaterRole = role
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Participant) SetUpdaterIP(addr string) {
|
|
|
|
model.UpdaterIP = addr
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:28:27 +01:00
|
|
|
// 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
|
|
|
|
// }
|
2019-12-02 10:48:46 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Participant) exists(db *Database) (*User, error) {
|
2019-12-03 11:14:29 +01:00
|
|
|
var user User
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.First(&user, &User{Username: model.username()}).Error; err != nil && err != gorm.ErrRecordNotFound {
|
2019-12-03 11:14:29 +01:00
|
|
|
return nil, err
|
|
|
|
} else if err == gorm.ErrRecordNotFound {
|
|
|
|
return nil, nil
|
2019-12-02 10:48:46 +01:00
|
|
|
}
|
2019-12-03 11:14:29 +01:00
|
|
|
return &user, nil
|
|
|
|
}
|
2019-12-02 10:48:46 +01:00
|
|
|
|
2019-12-03 11:14:29 +01:00
|
|
|
func (model *Participant) BeforeSave(tx *gorm.DB) error {
|
2019-11-29 12:44:43 +01:00
|
|
|
var user User
|
2019-12-13 12:55:11 +01:00
|
|
|
|
2019-12-02 10:48:46 +01:00
|
|
|
if err := tx.FirstOrCreate(&user, &User{
|
|
|
|
Username: model.username(),
|
2020-01-11 08:03:56 +01:00
|
|
|
Password: model.Password,
|
2019-12-02 10:48:46 +01:00
|
|
|
Role: "participant",
|
|
|
|
}).Error; err != nil {
|
2019-11-18 12:40:28 +01:00
|
|
|
return err
|
|
|
|
}
|
2019-11-29 12:44:43 +01:00
|
|
|
model.UserID = user.ID
|
2019-11-18 12:40:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:16:27 +01:00
|
|
|
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,
|
2019-12-13 08:32:20 +01:00
|
|
|
ParticipantID: model.ID,
|
2019-11-22 11:16:27 +01:00
|
|
|
}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 13:25:33 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
order, err := contest.generateQuestionsOrder(tx)
|
2019-12-16 18:05:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Model(&response).Update("QuestionsOrder", order).Error; err != nil {
|
2019-12-13 13:25:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:16:27 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:44:43 +01:00
|
|
|
func (model *Participant) AfterDelete(tx *gorm.DB) error {
|
|
|
|
if err := tx.Unscoped().Delete(model.User).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Participant) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-14 15:04:11 +01:00
|
|
|
if r.Method == "GET" {
|
|
|
|
participant := new(Participant)
|
2019-12-07 11:44:19 +01:00
|
|
|
if isSchool(r) {
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&participant.AllCategories).Error; err != nil {
|
2019-12-07 11:44:19 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-09 08:27:46 +01:00
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&participant.AllCategories).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&participant.AllContests).Error; err != nil {
|
2019-12-07 11:44:19 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&participant.AllSchools).Error; err != nil {
|
2019-12-07 11:44:19 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-03 15:24:01 +01:00
|
|
|
}
|
2019-11-14 15:04:11 +01:00
|
|
|
return participant, nil
|
|
|
|
} else {
|
|
|
|
participant := new(Participant)
|
2020-01-23 09:15:27 +01:00
|
|
|
|
2019-11-14 15:04:11 +01:00
|
|
|
err := renderer.Decode(participant, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-02 10:48:46 +01:00
|
|
|
|
2019-12-09 14:18:31 +01:00
|
|
|
// Check if participant exists
|
2020-01-15 11:27:00 +01:00
|
|
|
if user, err := participant.exists(db); err == nil && user != nil {
|
|
|
|
if err := db._db.Where("user_id = ?", user.ID).Find(&participant).Error; err != nil {
|
2019-12-03 11:14:29 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-27 13:15:51 +01:00
|
|
|
return nil, errors.ParticipantExists
|
2019-12-03 11:14:29 +01:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:18:31 +01:00
|
|
|
// If user has "school" role get school id from token
|
2019-12-09 08:27:46 +01:00
|
|
|
if isSchool(r) {
|
|
|
|
schoolID, err := strconv.Atoi(getUserIDFromToken(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
participant.SchoolID = uint(schoolID)
|
|
|
|
}
|
2019-12-09 14:18:31 +01:00
|
|
|
|
|
|
|
// Check if a participant of the same category exists
|
|
|
|
var school School
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.First(&school, participant.SchoolID).Error; err != nil {
|
2019-12-09 14:18:31 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
hasCategory, err := school.HasCategory(db, participant)
|
2019-12-09 14:18:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if hasCategory {
|
|
|
|
return nil, errors.CategoryExists
|
|
|
|
}
|
|
|
|
|
2020-01-27 11:43:56 +01:00
|
|
|
WriteCreator(r, participant)
|
2019-12-09 14:18:31 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
participant, err = CreateParticipant(db, participant)
|
2019-11-14 15:04:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2020-01-21 17:25:06 +01:00
|
|
|
// FIXME: Should be managed by API
|
2020-01-23 10:23:06 +01:00
|
|
|
if isAdministrator(r) {
|
|
|
|
var response Response
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2020-01-23 10:23:06 +01:00
|
|
|
if err := db._db.First(&response, &Response{ParticipantID: participant.ID}).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2020-01-27 08:35:37 +01:00
|
|
|
response.CreatorID = getUserIDFromTokenAsUint(r)
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2020-01-23 10:23:06 +01:00
|
|
|
if err := db._db.Save(&response).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-23 09:15:27 +01:00
|
|
|
}
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2019-11-14 15:04:11 +01:00
|
|
|
return participant, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Participant) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-14 15:04:11 +01:00
|
|
|
var participant Participant
|
|
|
|
|
|
|
|
id := args["id"]
|
|
|
|
|
2019-12-07 11:44:19 +01:00
|
|
|
// School user can access to its participants only!
|
|
|
|
if isSchool(r) {
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Preload("School").First(&participant, id).Error; err != nil {
|
2019-12-07 11:44:19 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strconv.Itoa(int(participant.SchoolID)) != getUserIDFromToken(r) {
|
2020-01-14 16:28:27 +01:00
|
|
|
// setFlashMessage(w, r, "notAuthorized")
|
2019-12-07 11:44:19 +01:00
|
|
|
return nil, errors.NotAuthorized
|
|
|
|
}
|
|
|
|
|
2020-01-27 14:21:08 +01:00
|
|
|
if err := db._db.Preload("User").Preload("School").Preload("School.Participants").Preload("Category").First(&participant, id).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2020-01-27 14:21:08 +01:00
|
|
|
if err := db._db.Preload("User").Preload("School").Preload("School.Participants").Preload("Responses").Preload("Contests").Preload("Category").First(&participant, id).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-24 10:55:01 +01:00
|
|
|
|
2019-11-14 15:04:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &participant, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Participant) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-14 15:04:11 +01:00
|
|
|
var participants []*Participant
|
2019-12-09 08:27:46 +01:00
|
|
|
|
|
|
|
// School user can access to its participants only!
|
|
|
|
if isSchool(r) {
|
|
|
|
schoolId, err := strconv.Atoi(getUserIDFromToken(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Preload("Category").Preload("School").Preload("Contests").Order("lastname").Find(&participants, &Participant{SchoolID: uint(schoolId)}).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Preload("School").Preload("Contests").Preload("Responses").Order("created_at").Find(&participants).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-14 15:04:11 +01:00
|
|
|
}
|
|
|
|
return participants, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Participant) Update(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-14 15:04:11 +01:00
|
|
|
if r.Method == "GET" {
|
2020-01-15 11:27:00 +01:00
|
|
|
result, err := model.Read(db, args, w, r)
|
2019-11-14 15:04:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
participant := result.(*Participant)
|
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
if isSchool(r) {
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&participant.AllCategories).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-14 15:04:11 +01:00
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
participant.SelectedCategory = make(map[uint]string)
|
|
|
|
participant.SelectedCategory[participant.CategoryID] = "selected"
|
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&participant.AllCategories).Error; err != nil {
|
2019-12-09 15:23:44 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
participant.SelectedCategory = make(map[uint]string)
|
|
|
|
participant.SelectedCategory[participant.CategoryID] = "selected"
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&participant.AllContests).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-14 15:04:11 +01:00
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
participant.SelectedContest = make(map[uint]string)
|
|
|
|
for _, c := range participant.Contests {
|
|
|
|
participant.SelectedContest[c.ID] = "selected"
|
|
|
|
}
|
2019-12-03 12:21:28 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&participant.AllSchools).Error; err != nil {
|
2019-12-09 08:27:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-03 12:21:28 +01:00
|
|
|
|
2019-12-09 08:27:46 +01:00
|
|
|
participant.SelectedSchool = make(map[uint]string)
|
|
|
|
participant.SelectedSchool[participant.SchoolID] = "selected"
|
|
|
|
}
|
2019-11-14 15:04:11 +01:00
|
|
|
return participant, nil
|
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
participant, err := model.Read(db, args, w, r)
|
2019-11-14 15:04:11 +01:00
|
|
|
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
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if user, err := participant.(*Participant).exists(db); err == nil && user != nil {
|
2019-12-03 11:14:29 +01:00
|
|
|
if user.ID != participant.(*Participant).UserID {
|
2020-01-14 16:28:27 +01:00
|
|
|
// err := setFlashMessage(w, r, "participantExists")
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2019-12-03 11:14:29 +01:00
|
|
|
return participant, nil
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:18:31 +01:00
|
|
|
// Check if a participant of the same category exists
|
|
|
|
var school School
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.First(&school, participant.(*Participant).SchoolID).Error; err != nil {
|
2019-12-09 14:18:31 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
hasCategory, err := school.HasCategory(db, participant.(*Participant))
|
2019-12-09 14:18:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if hasCategory {
|
|
|
|
return nil, errors.CategoryExists
|
|
|
|
}
|
|
|
|
|
2020-01-27 11:43:56 +01:00
|
|
|
if err := db._db.Where([]uint(participant.(*Participant).ContestIDs)).Find(&participant.(*Participant).Contests).Error; err != nil {
|
2019-11-15 10:41:32 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-27 11:43:56 +01:00
|
|
|
WriteUpdater(r, participant.(*Participant))
|
2019-12-09 15:54:24 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
_, err = SaveParticipant(db, participant)
|
2019-11-14 15:04:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-18 17:04:07 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Model(participant).Association("Contests").Replace(participant.(*Participant).Contests).Error; err != nil {
|
2019-11-18 17:04:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
participant, err = model.Read(db, args, w, r)
|
2019-11-14 15:04:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2020-01-21 17:25:06 +01:00
|
|
|
// FIXME: Should be managed by API
|
2020-01-23 10:23:06 +01:00
|
|
|
if isAdministrator(r) {
|
|
|
|
var response Response
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2020-01-23 10:23:06 +01:00
|
|
|
if err := db._db.First(&response, &Response{ParticipantID: participant.(*Participant).ID}).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2020-01-27 08:35:37 +01:00
|
|
|
response.UpdaterID = getUserIDFromTokenAsUint(r)
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2020-01-23 10:23:06 +01:00
|
|
|
if err := db._db.Save(&response).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 07:50:18 +01:00
|
|
|
|
2019-11-14 15:04:11 +01:00
|
|
|
return participant.(*Participant), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Participant) Delete(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
|
|
participant, err := model.Read(db, args, w, r)
|
2019-11-14 15:04:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Unscoped().Delete(participant.(*Participant)).Error; err != nil {
|
2019-11-14 15:04:11 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return participant.(*Participant), nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func CreateParticipant(db *Database, participant *Participant) (*Participant, error) {
|
|
|
|
if err := db._db.Where([]uint(participant.ContestIDs)).Find(&participant.Contests).Error; err != nil {
|
2019-11-29 12:44:43 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Create(participant).Error; err != nil {
|
2019-11-14 15:04:11 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return participant, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func SaveParticipant(db *Database, participant interface{}) (interface{}, error) {
|
2019-12-02 10:48:46 +01:00
|
|
|
participant.(*Participant).FiscalCode = strings.ToUpper(participant.(*Participant).FiscalCode)
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Omit("Category", "School").Save(participant).Error; err != nil {
|
2019-11-14 15:04:11 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return participant, nil
|
|
|
|
}
|