2017-01-15 01:37:59 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-01-29 12:38:39 +00:00
|
|
|
"errors"
|
2017-01-15 01:37:59 +00:00
|
|
|
"path"
|
|
|
|
|
2017-12-11 01:56:26 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/sync"
|
2017-01-16 12:10:15 +00:00
|
|
|
"srs.epita.fr/fic-server/libfic"
|
2017-01-15 01:37:59 +00:00
|
|
|
"srs.epita.fr/fic-server/settings"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-12-11 01:56:26 +00:00
|
|
|
router.GET("/api/settings-ro.json", apiHandler(getROSettings))
|
2017-01-15 01:37:59 +00:00
|
|
|
router.GET("/api/settings.json", apiHandler(getSettings))
|
|
|
|
router.PUT("/api/settings.json", apiHandler(saveSettings))
|
2017-01-29 12:38:39 +00:00
|
|
|
|
|
|
|
router.POST("/api/reset", apiHandler(reset))
|
2017-01-15 01:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 01:56:26 +00:00
|
|
|
func getROSettings(_ httprouter.Params, body []byte) (interface{}, error) {
|
|
|
|
syncMtd := "Disabled"
|
|
|
|
if sync.GlobalImporter != nil {
|
|
|
|
syncMtd = sync.GlobalImporter.Kind()
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
|
"sync": syncMtd,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:37:59 +00:00
|
|
|
func getSettings(_ httprouter.Params, body []byte) (interface{}, error) {
|
2018-08-19 19:00:32 +00:00
|
|
|
return settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile))
|
2017-01-15 01:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveSettings(_ httprouter.Params, body []byte) (interface{}, error) {
|
|
|
|
var config settings.FICSettings
|
|
|
|
if err := json.Unmarshal(body, &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-25 15:05:03 +00:00
|
|
|
if err := settings.SaveSettings(path.Join(settings.SettingsDir, settings.SettingsFile), config); err != nil {
|
2017-01-15 01:37:59 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
2018-12-06 20:25:09 +00:00
|
|
|
ApplySettings(config)
|
2017-01-15 01:37:59 +00:00
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 12:38:39 +00:00
|
|
|
|
2018-12-06 20:25:09 +00:00
|
|
|
func ApplySettings(config settings.FICSettings) {
|
|
|
|
fic.PartialValidation = config.PartialValidation
|
|
|
|
fic.UnlockedChallenges = !config.EnableExerciceDepend
|
|
|
|
fic.FirstBlood = config.FirstBlood
|
|
|
|
fic.SubmissionCostBase = config.SubmissionCostBase
|
2018-12-06 21:18:08 +00:00
|
|
|
fic.SubmissionUniqueness = config.SubmissionUniqueness
|
2018-12-06 20:25:09 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 12:38:39 +00:00
|
|
|
func reset(_ httprouter.Params, body []byte) (interface{}, error) {
|
|
|
|
var m map[string]string
|
|
|
|
if err := json.Unmarshal(body, &m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t, ok := m["type"]; !ok {
|
|
|
|
return nil, errors.New("Field type not found")
|
|
|
|
} else if t == "teams" {
|
|
|
|
return true, fic.ResetTeams()
|
|
|
|
} else if t == "challenges" {
|
|
|
|
return true, fic.ResetExercices()
|
|
|
|
} else if t == "game" {
|
|
|
|
return true, fic.ResetGame()
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Unknown reset type")
|
|
|
|
}
|
|
|
|
}
|