admin: complet API and interface with files checking page
This commit is contained in:
parent
184714aeeb
commit
9a1a64c41c
9 changed files with 166 additions and 19 deletions
|
@ -21,8 +21,8 @@ func init() {
|
|||
|
||||
router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles)))
|
||||
router.POST("/api/exercices/:eid/files", apiHandler(exerciceHandler(createExerciceFile)))
|
||||
router.GET("/api/exercices/:eid/files/:fid", apiHandler(fileHandler(showExerciceFile)))
|
||||
router.DELETE("/api/exercices/:eid/files/:fid", apiHandler(fileHandler(deleteExerciceFile)))
|
||||
router.GET("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(showExerciceFile)))
|
||||
router.DELETE("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(deleteExerciceFile)))
|
||||
|
||||
router.GET("/api/exercices/:eid/hints", apiHandler(exerciceHandler(listExerciceHints)))
|
||||
router.POST("/api/exercices/:eid/hints", apiHandler(exerciceHandler(createExerciceHint)))
|
||||
|
@ -233,6 +233,11 @@ func deleteExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, _ []byte) (interface{}, er
|
|||
return quiz.Delete()
|
||||
}
|
||||
|
||||
type uploadedFile struct {
|
||||
URI string
|
||||
Digest string
|
||||
}
|
||||
|
||||
func createExerciceFile(exercice fic.Exercice, body []byte) (interface{}, error) {
|
||||
var uf uploadedFile
|
||||
if err := json.Unmarshal(body, &uf); err != nil {
|
||||
|
|
|
@ -1,8 +1,50 @@
|
|||
package api
|
||||
|
||||
import ()
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
type uploadedFile struct {
|
||||
URI string
|
||||
Digest string
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/files/", apiHandler(listFiles))
|
||||
router.DELETE("/api/files/", apiHandler(clearFiles))
|
||||
|
||||
router.GET("/api/files/:fileid", apiHandler(fileHandler(showFile)))
|
||||
router.PUT("/api/files/:fileid", apiHandler(fileHandler(updateFile)))
|
||||
router.DELETE("/api/files/:fileid", apiHandler(fileHandler(deleteFile)))
|
||||
}
|
||||
|
||||
func listFiles(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
// List all files
|
||||
return fic.GetFiles()
|
||||
}
|
||||
|
||||
func clearFiles(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return fic.ClearFiles()
|
||||
}
|
||||
|
||||
func showFile(file fic.EFile, _ []byte) (interface{}, error) {
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func updateFile(file fic.EFile, body []byte) (interface{}, error) {
|
||||
var uf fic.EFile
|
||||
if err := json.Unmarshal(body, &uf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
uf.Id = file.Id
|
||||
|
||||
if _, err := uf.Update(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return uf, nil
|
||||
}
|
||||
}
|
||||
|
||||
func deleteFile(file fic.EFile, _ []byte) (interface{}, error) {
|
||||
return file.Delete()
|
||||
}
|
||||
|
|
|
@ -213,7 +213,7 @@ func quizHandler(f func(fic.MCQ,fic.Exercice,[]byte) (interface{}, error)) func
|
|||
}
|
||||
}
|
||||
|
||||
func fileHandler(f func(fic.EFile,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) {
|
||||
func exerciceFileHandler(f func(fic.EFile,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) {
|
||||
return func (ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
var exercice fic.Exercice
|
||||
exerciceHandler(func (ex fic.Exercice, _[]byte) (interface{}, error) {
|
||||
|
@ -248,6 +248,18 @@ func eventHandler(f func(fic.Event,[]byte) (interface{}, error)) func (httproute
|
|||
}
|
||||
}
|
||||
|
||||
func fileHandler(f func(fic.EFile,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) {
|
||||
return func (ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if fileid, err := strconv.Atoi(string(ps.ByName("fileid"))); err != nil {
|
||||
return nil, err
|
||||
} else if file, err := fic.GetFile(fileid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(file, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func notFound(ps httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
Reference in a new issue