package orm import ( "net/http" "github.com/jinzhu/gorm" "gogs.carducci-dante.gov.it/karmen/core/renderer" ) type Office struct { gorm.Model Name string Email string Administratives []*Administrative } type OfficeForUpdate struct { Office Office } type OfficeForAdd struct{} func (d *Office) GetID() uint { return d.ID } func GetOffice(args map[string]string) (interface{}, error) { var office Office if err := DB().First(&office, args["id"]).Error; err != nil { return nil, err } return &office, nil } func GetOfficeAll(args map[string]string) (interface{}, error) { var office Office id := args["id"] if err := DB().Preload("Administratives").Where("id = ?", id).Find(&office).Error; err != nil { return nil, err } return &office, nil } func GetOffices(args map[string]string) (interface{}, error) { var offices []*Office if err := DB().Order("name").Find(&offices).Error; err != nil { return nil, err } return offices, nil } func GetOfficesAll(args map[string]string) (interface{}, error) { var offices []*Office if err := DB().Preload("Administratives").Order("name").Find(&offices).Error; err != nil { return nil, err } // for _, office := range offices { // office.GetAdministratives() // } return offices, nil } func SaveOffice(office interface{}) (interface{}, error) { if err := DB().Omit("Administratives").Save(office).Error; err != nil { return nil, err } return office, nil } func UpdateOffice(args map[string]string, r *http.Request) (IDer, error) { office, err := GetOffice(args) if err != nil { return nil, err } err = renderer.Decode(office, r) if err != nil { return nil, err } _, err = SaveOffice(office) if err != nil { return nil, err } office, err = GetOffice(args) if err != nil { return nil, err } return office.(*Office), nil } func AddOffice(args map[string]string, r *http.Request) (IDer, error) { office := new(Office) err := renderer.Decode(office, r) if err != nil { return nil, err } office, err = CreateOffice(office) if err != nil { return nil, err } return office, nil } func CreateOffice(office *Office) (*Office, error) { if err := DB().Create(office).Error; err != nil { return nil, err } return office, nil } func DeleteOffice(args map[string]string, r *http.Request) (IDer, error) { office, err := GetOffice(args) if err != nil { return nil, err } if err := DB().Unscoped().Delete(office.(*Office)).Error; err != nil { return nil, err } return office.(*Office), nil } func GetOfficeForAdd(args map[string]string) (interface{}, error) { var data OfficeForAdd // if err := DB().Find(&data.AllTeachers).Error; err != nil { // return nil, err // } return data, nil } func GetOfficeForUpdate(args map[string]string) (interface{}, error) { var data OfficeForUpdate id := args["id"] if err := DB().First(&data.Office, id).Error; err != nil { return nil, err } return data, nil } // func (t *Office) GetAdministratives() ([]*Administrative, error) { // if err := DB().Raw(selectUniqueOfficeTeachers, t.ID).Scan(&t.Teachers).Error; err != nil { // return nil, err // } // return t.Teachers, nil // }