Merge exercices API routes

This commit is contained in:
nemunaire 2016-12-26 01:14:46 +01:00
parent 14f0e6cf8e
commit 793ae81244
4 changed files with 60 additions and 105 deletions

View File

@ -3,7 +3,6 @@ package api
import (
"encoding/json"
"errors"
"strconv"
"srs.epita.fr/fic-server/libfic"
@ -14,8 +13,17 @@ func init() {
router.GET("/api/exercices/", apiHandler(listExercices))
router.GET("/api/exercices/:eid", apiHandler(exerciceHandler(showExercice)))
router.PUT("/api/exercices/:eid", apiHandler(updateExercice))
router.DELETE("/api/exercices/:eid", apiHandler(deleteExercice))
router.PUT("/api/exercices/:eid", apiHandler(exerciceHandler(updateExercice)))
router.DELETE("/api/exercices/:eid", apiHandler(exerciceHandler(deleteExercice)))
router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles)))
router.POST("/api/exercices/:eid/files", apiHandler(exerciceHandler(createExerciceFile)))
router.GET("/api/exercices/:eid/hints", apiHandler(exerciceHandler(listExerciceHints)))
router.POST("/api/exercices/:eid/hints", apiHandler(exerciceHandler(createExerciceHint)))
router.GET("/api/exercices/:eid/keys", apiHandler(exerciceHandler(listExerciceKeys)))
router.POST("/api/exercices/:eid/keys", apiHandler(exerciceHandler(createExerciceKey)))
}
func listExercices(_ httprouter.Params, body []byte) (interface{}, error) {
@ -23,63 +31,48 @@ func listExercices(_ httprouter.Params, body []byte) (interface{}, error) {
return fic.GetExercices()
}
func listExerciceFiles(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetFiles()
}
func listExerciceHints(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetHints()
}
func listExerciceKeys(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetKeys()
}
func showExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice, nil
}
func deleteExercice(ps httprouter.Params, body []byte) (interface{}, error) {
if eid, err := strconv.Atoi(ps.ByName("eid")); err != nil {
return nil, err
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
return nil, err
} else {
return exercice.Delete()
}
func deleteExercice(exercice fic.Exercice, _ []byte) (interface{}, error) {
return exercice.Delete()
}
type uploadedExercice struct {
Title string
Statement string
Depend *int64
Gain int
VideoURI string
}
func updateExercice(ps httprouter.Params, body []byte) (interface{}, error) {
if eid, err := strconv.Atoi(ps.ByName("eid")); err != nil {
func updateExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
var ue fic.Exercice
if err := json.Unmarshal(body, &ue); err != nil {
return nil, err
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
return nil, err
} else {
// Update an exercice
var ue uploadedExercice
if err := json.Unmarshal(body, &ue); err != nil {
return nil, err
}
if len(ue.Title) == 0 {
return nil, errors.New("Exercice's title not filled")
}
if ue.Depend != nil {
if _, err := fic.GetExercice(*ue.Depend); err != nil {
return nil, err
}
}
exercice.Title = ue.Title
exercice.Statement = ue.Statement
exercice.Depend = ue.Depend
exercice.Gain = int64(ue.Gain)
exercice.VideoURI = ue.VideoURI
return exercice.Update()
}
ue.Id = exercice.Id
if len(ue.Title) == 0 {
return nil, errors.New("Exercice's title not filled")
}
if _, err := ue.Update(); err != nil {
return nil, err
}
return ue, nil
}
func createExercice(theme fic.Theme, body []byte) (interface{}, error) {
// Create a new exercice
var ue uploadedExercice
var ue fic.Exercice
if err := json.Unmarshal(body, &ue); err != nil {
return nil, err
}
@ -105,7 +98,7 @@ type uploadedKey struct {
Key string
}
func createExerciceKey(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
func createExerciceKey(exercice fic.Exercice, body []byte) (interface{}, error) {
var uk uploadedKey
if err := json.Unmarshal(body, &uk); err != nil {
return nil, err
@ -118,14 +111,8 @@ func createExerciceKey(theme fic.Theme, exercice fic.Exercice, body []byte) (int
return exercice.AddRawKey(uk.Name, uk.Key)
}
type uploadedHint struct {
Title string
Content string
Cost int64
}
func createExerciceHint(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
var uh uploadedHint
func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error) {
var uh fic.EHint
if err := json.Unmarshal(body, &uh); err != nil {
return nil, err
}

View File

@ -27,7 +27,7 @@ type uploadedFile struct {
Parts []string
}
func createExerciceFile(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
func createExerciceFile(exercice fic.Exercice, body []byte) (interface{}, error) {
var uf uploadedFile
if err := json.Unmarshal(body, &uf); err != nil {
return nil, err

View File

@ -24,19 +24,19 @@ func init() {
router.GET("/api/themes/:thid/exercices", apiHandler(themeHandler(listThemedExercices)))
router.POST("/api/themes/:thid/exercices", apiHandler(themeHandler(createExercice)))
router.GET("/api/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler(showThemedExercice)))
router.PUT("/api/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler(updateThemedExercice)))
router.DELETE("/api/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler(deleteThemedExercice)))
router.GET("/api/themes/:thid/exercices/:eid", apiHandler(exerciceHandler(showExercice)))
router.PUT("/api/themes/:thid/exercices/:eid", apiHandler(exerciceHandler(updateExercice)))
router.DELETE("/api/themes/:thid/exercices/:eid", apiHandler(exerciceHandler(deleteExercice)))
router.GET("/api/themes/:thid/exercices/:eid/files", apiHandler(themedExerciceHandler(listThemedExerciceFiles)))
router.POST("/api/themes/:thid/exercices/:eid/files", apiHandler(themedExerciceHandler(createExerciceFile)))
router.GET("/api/themes/:thid/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles)))
router.POST("/api/themes/:thid/exercices/:eid/files", apiHandler(exerciceHandler(createExerciceFile)))
router.GET("/api/themes/:thid/exercices/:eid/hints", apiHandler(themedExerciceHandler(listThemedExerciceHints)))
router.POST("/api/themes/:thid/exercices/:eid/hints", apiHandler(themedExerciceHandler(createExerciceHint)))
router.GET("/api/themes/:thid/exercices/:eid/hints", apiHandler(exerciceHandler(listExerciceHints)))
router.POST("/api/themes/:thid/exercices/:eid/hints", apiHandler(exerciceHandler(createExerciceHint)))
router.GET("/api/themes/:thid/exercices/:eid/keys", apiHandler(themedExerciceHandler(listThemedExerciceKeys)))
router.POST("/api/themes/:thid/exercices/:eid/keys", apiHandler(themedExerciceHandler(createExerciceKey)))
router.GET("/api/themes/:thid/exercices/:eid/keys", apiHandler(exerciceHandler(listExerciceKeys)))
router.POST("/api/themes/:thid/exercices/:eid/keys", apiHandler(exerciceHandler(createExerciceKey)))
}
func bindingFiles(_ httprouter.Params, body []byte) (interface{}, error) {
@ -83,18 +83,6 @@ func showThemedExercice(theme fic.Theme, exercice fic.Exercice, body []byte) (in
return exercice, nil
}
func listThemedExerciceFiles(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetFiles()
}
func listThemedExerciceHints(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetHints()
}
func listThemedExerciceKeys(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetKeys()
}
type uploadedTheme struct {
Name string
@ -126,33 +114,13 @@ func updateTheme(theme fic.Theme, body []byte) (interface{}, error) {
return nil, errors.New("Theme's name not filled")
}
return ut.Update()
}
func updateThemedExercice(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
// Update an exercice
var ue fic.Exercice
if err := json.Unmarshal(body, &ue); err != nil {
if _, err := ut.Update(); err != nil {
return nil, err
} else {
return ut, nil
}
ue.Id = exercice.Id
if len(ue.Title) == 0 {
return nil, errors.New("Exercice's title not filled")
}
if _, err := ue.Update(); err != nil {
return nil, err
}
return ue, nil
}
func deleteTheme(theme fic.Theme, _ []byte) (interface{}, error) {
return theme.Delete()
}
func deleteThemedExercice(_ fic.Theme, exercice fic.Exercice, _ []byte) (interface{}, error) {
return exercice.Delete()
}

View File

@ -78,7 +78,7 @@ func (t Theme) GetExercices() ([]Exercice, error) {
}
}
func (t Theme) AddExercice(title string, statement string, depend *Exercice, gain int, videoURI string) (Exercice, error) {
func (t Theme) AddExercice(title string, statement string, depend *Exercice, gain int64, videoURI string) (Exercice, error) {
var dpd interface{}
if depend == nil {
dpd = nil
@ -91,9 +91,9 @@ func (t Theme) AddExercice(title string, statement string, depend *Exercice, gai
return Exercice{}, err
} else {
if depend == nil {
return Exercice{eid, title, statement, nil, int64(gain), videoURI}, nil
return Exercice{eid, title, statement, nil, gain, videoURI}, nil
} else {
return Exercice{eid, title, statement, &depend.Id, int64(gain), videoURI}, nil
return Exercice{eid, title, statement, &depend.Id, gain, videoURI}, nil
}
}
}