oef/orm/school.go
2019-12-03 12:21:28 +01:00

197 lines
4.4 KiB
Go

package orm
import (
"net/http"
"strings"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
)
type School struct {
gorm.Model
Name string
Email string
Code string
UserID uint
User *User
Participants []*Participant
// SelectedElement map[uint]string `gorm:"-"`
// AllElements []*Element `gorm:"-"`
}
func (model *School) GetID() uint { return model.ID }
func (model *School) String() string {
return model.Name
}
func (model *School) username() string {
return strings.ToUpper(model.Code)
}
func (model *School) 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 *School) BeforeSave(tx *gorm.DB) error {
var user User
if err := tx.FirstOrCreate(&user, &User{
Username: model.username(),
Role: "school",
}).Error; err != nil {
return err
}
model.UserID = user.ID
return nil
}
func (model *School) AfterDelete(tx *gorm.DB) error {
if err := tx.Unscoped().Delete(model.User).Error; err != nil {
return err
}
return nil
}
func (model *School) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
if r.Method == "GET" {
school := new(School)
// if err := DB().Find(&school.AllContests).Error; err != nil {
// return nil, err
// }
return school, nil
} else {
school := new(School)
err := renderer.Decode(school, r)
if err != nil {
return nil, err
}
// Check if this user already exists in the users table.
if user, err := school.exists(); err == nil && user != nil {
if err := DB().Where("user_id = ?", user.ID).Find(&school).Error; err != nil {
return nil, err
}
err := setFlashMessage(w, r, "schoolExists")
if err != nil {
return nil, err
}
return school, nil
} else if err != nil {
return nil, err
}
school, err = CreateSchool(school)
if err != nil {
return nil, err
}
return school, nil
}
}
func (model *School) Read(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
var school School
id := args["id"]
if err := DB(). /*.Preload("Something")*/ First(&school, id).Error; err != nil {
return nil, err
}
return &school, nil
}
func (model *School) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
var schools []*School
if err := DB(). /*.Preload("Something")*/ Order("created_at").Find(&schools).Error; err != nil {
return nil, err
}
return schools, nil
}
func (model *School) 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
}
school := result.(*School)
// if err := DB().Find(&school.AllElements).Error; err != nil {
// return nil, err
// }
// school.SelectedElement = make(map[uint]string)
// school.SelectedElement[school.ElementID] = "selected"
return school, nil
} else {
school, err := model.Read(args, w, r)
if err != nil {
return nil, err
}
err = renderer.Decode(school, r)
if err != nil {
return nil, err
}
// Check if the modified school code belong to an existing school.
if user, err := school.(*School).exists(); err == nil && user != nil {
if user.ID != school.(*School).UserID {
err := setFlashMessage(w, r, "schoolExists")
if err != nil {
return nil, err
}
return school, nil
}
} else if err != nil {
return nil, err
}
_, err = SaveSchool(school)
if err != nil {
return nil, err
}
school, err = model.Read(args, w, r)
if err != nil {
return nil, err
}
return school.(*School), nil
}
}
func (model *School) Delete(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
school, err := model.Read(args, w, r)
if err != nil {
return nil, err
}
if err := DB().Unscoped().Delete(school.(*School)).Error; err != nil {
return nil, err
}
return school.(*School), nil
}
func CreateSchool(school *School) (*School, error) {
if err := DB().Create(school).Error; err != nil {
return nil, err
}
return school, nil
}
func SaveSchool(school interface{}) (interface{}, error) {
if err := DB(). /*.Omit("Something")*/ Save(school).Error; err != nil {
return nil, err
}
return school, nil
}