126 lines
3 KiB
Go
126 lines
3 KiB
Go
|
package orm
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
||
|
"github.com/jinzhu/gorm"
|
||
|
)
|
||
|
|
||
|
type Participant struct {
|
||
|
gorm.Model
|
||
|
|
||
|
// SelectedElement map[uint]string `gorm:"-"`
|
||
|
// AllElements []*Element `gorm:"-"`
|
||
|
}
|
||
|
|
||
|
func (model *Participant) GetID() uint { return model.ID }
|
||
|
|
||
|
func (model *Participant) String() string {
|
||
|
return "" // Please implement this.
|
||
|
}
|
||
|
|
||
|
func (model *Participant) Create(args map[string]string, r *http.Request) (interface{}, error) {
|
||
|
if r.Method == "GET" {
|
||
|
participant := new(Participant)
|
||
|
// if err := DB().Find(&participant.AllContests).Error; err != nil {
|
||
|
// return nil, err
|
||
|
// }
|
||
|
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"]
|
||
|
|
||
|
if err := DB()/*.Preload("Something")*/.First(&participant, id).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &participant, nil
|
||
|
}
|
||
|
|
||
|
func (model *Participant) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
|
||
|
var participants []*Participant
|
||
|
if err := DB()/*.Preload("Something")*/.Order("created_at").Find(&participants).Error; err != nil {
|
||
|
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)
|
||
|
|
||
|
// if err := DB().Find(&participant.AllElements).Error; err != nil {
|
||
|
// return nil, err
|
||
|
// }
|
||
|
|
||
|
// participant.SelectedElement = make(map[uint]string)
|
||
|
// participant.SelectedElement[participant.ElementID] = "selected"
|
||
|
|
||
|
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
|
||
|
}
|
||
|
_, 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) {
|
||
|
if err := DB()/*.Omit("Something")*/.Save(participant).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return participant, nil
|
||
|
}
|