173 lines
3.7 KiB
Go
173 lines
3.7 KiB
Go
package orm
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/jinzhu/gorm"
|
|
)
|
|
|
|
type Contest struct {
|
|
gorm.Model
|
|
|
|
Name string
|
|
Category string
|
|
|
|
Date *time.Time
|
|
StartTime *time.Time
|
|
EndTime *time.Time
|
|
|
|
Questions []*Question
|
|
Participants []*Participant `gorm:"many2many:subscriptions"`
|
|
}
|
|
|
|
func (c *Contest) GetID() uint { return c.ID }
|
|
|
|
func (c *Contest) String() string {
|
|
return c.Name
|
|
}
|
|
|
|
func (c *Contest) Create(args map[string]string, r *http.Request) (interface{}, error) {
|
|
if r.Method == "GET" {
|
|
return nil, nil
|
|
} else {
|
|
contest := new(Contest)
|
|
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
date := r.FormValue("Date")
|
|
startTime := r.FormValue("StartTime")
|
|
endTime := r.FormValue("EndTime")
|
|
|
|
r.PostForm.Set("Date", fmt.Sprintf("%sT%s:00+00:00", date, startTime))
|
|
r.PostForm.Set("StartTime", fmt.Sprintf("%sT%s:00+00:00", date, startTime))
|
|
r.PostForm.Set("EndTime", fmt.Sprintf("%sT%s:00+00:00", date, endTime))
|
|
|
|
// err = r.ParseForm()
|
|
// if err != nil {
|
|
// return nil, err
|
|
// }
|
|
|
|
// r.Form["Date"][0] = fmt.Sprintf("%sT%s+00:00", date, startTime)
|
|
|
|
err = renderer.Decode(contest, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
contest, err = CreateContest(contest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return contest, nil
|
|
}
|
|
}
|
|
|
|
func (c *Contest) Read(args map[string]string, r *http.Request) (interface{}, error) {
|
|
var contest Contest
|
|
|
|
id := args["id"]
|
|
|
|
if err := DB().Preload("Participants").Preload("Questions").First(&contest, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &contest, nil
|
|
}
|
|
|
|
func (c *Contest) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
|
|
var contests []*Contest
|
|
|
|
claims := r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
|
|
|
|
if claims["admin"].(bool) {
|
|
if err := DB().Order("created_at").Find(&contests).Error; err != nil {
|
|
return nil, err
|
|
} else {
|
|
return contests, nil
|
|
}
|
|
}
|
|
|
|
participant := &Participant{}
|
|
|
|
if err := DB().Preload("Contests").Where("username = ?", claims["name"].(string)).First(&participant).Error; err != nil {
|
|
return nil, err
|
|
} else {
|
|
return participant.Contests, nil
|
|
}
|
|
}
|
|
|
|
func (c *Contest) Update(args map[string]string, r *http.Request) (interface{}, error) {
|
|
if r.Method == "GET" {
|
|
result, err := c.Read(args, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
contest := result.(*Contest)
|
|
|
|
return contest, nil
|
|
} else {
|
|
contest, err := c.Read(args, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = r.ParseForm()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
date := r.FormValue("Date")
|
|
startTime := r.FormValue("StartTime")
|
|
endTime := r.FormValue("EndTime")
|
|
|
|
r.PostForm.Set("Date", fmt.Sprintf("%sT%s:00+00:00", date, startTime))
|
|
r.PostForm.Set("StartTime", fmt.Sprintf("%sT%s:00+00:00", date, startTime))
|
|
r.PostForm.Set("EndTime", fmt.Sprintf("%sT%s:00+00:00", date, endTime))
|
|
|
|
err = r.ParseForm()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
r.Form["Date"][0] = fmt.Sprintf("%sT%s+00:00", date, startTime)
|
|
|
|
err = renderer.Decode(contest, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = SaveContest(contest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
contest, err = c.Read(args, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return contest.(*Contest), nil
|
|
}
|
|
}
|
|
|
|
func (c *Contest) Delete(args map[string]string, r *http.Request) (interface{}, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func CreateContest(contest *Contest) (*Contest, error) {
|
|
if err := DB().Create(contest).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return contest, nil
|
|
}
|
|
|
|
func SaveContest(contest interface{}) (interface{}, error) {
|
|
if err := DB().Omit("Contests").Save(contest).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return contest, nil
|
|
}
|