admin/api: refactor file API
This commit is contained in:
parent
3e5b4ebad2
commit
973363b3da
3 changed files with 41 additions and 44 deletions
|
@ -1,7 +1,6 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -26,11 +25,6 @@ func init() {
|
||||||
router.PATCH("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(updateExerciceHistory)))
|
router.PATCH("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(updateExerciceHistory)))
|
||||||
router.DELETE("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(delExerciceHistory)))
|
router.DELETE("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(delExerciceHistory)))
|
||||||
|
|
||||||
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(exerciceFileHandler(showExerciceFile)))
|
|
||||||
router.DELETE("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(deleteExerciceFile)))
|
|
||||||
|
|
||||||
router.GET("/api/exercices/:eid/hints", apiHandler(exerciceHandler(listExerciceHints)))
|
router.GET("/api/exercices/:eid/hints", apiHandler(exerciceHandler(listExerciceHints)))
|
||||||
router.POST("/api/exercices/:eid/hints", apiHandler(exerciceHandler(createExerciceHint)))
|
router.POST("/api/exercices/:eid/hints", apiHandler(exerciceHandler(createExerciceHint)))
|
||||||
router.GET("/api/exercices/:eid/hints/:hid", apiHandler(hintHandler(showExerciceHint)))
|
router.GET("/api/exercices/:eid/hints/:hid", apiHandler(hintHandler(showExerciceHint)))
|
||||||
|
@ -64,10 +58,6 @@ func init() {
|
||||||
_, _, errs := sync.SyncExercice(sync.GlobalImporter, theme, exercice.Path, nil)
|
_, _, errs := sync.SyncExercice(sync.GlobalImporter, theme, exercice.Path, nil)
|
||||||
return errs, nil
|
return errs, nil
|
||||||
})))
|
})))
|
||||||
router.POST("/api/sync/exercices/:eid/files", apiHandler(exerciceHandler(
|
|
||||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
|
||||||
return sync.SyncExerciceFiles(sync.GlobalImporter, exercice), nil
|
|
||||||
})))
|
|
||||||
router.POST("/api/sync/exercices/:eid/hints", apiHandler(exerciceHandler(
|
router.POST("/api/sync/exercices/:eid/hints", apiHandler(exerciceHandler(
|
||||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||||
return sync.SyncExerciceHints(sync.GlobalImporter, exercice), nil
|
return sync.SyncExerciceHints(sync.GlobalImporter, exercice), nil
|
||||||
|
@ -113,10 +103,6 @@ func exportResolutionMovies(_ httprouter.Params, body []byte) (interface{}, erro
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func listExerciceFiles(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
||||||
return exercice.GetFiles()
|
|
||||||
}
|
|
||||||
|
|
||||||
func listExerciceHints(exercice fic.Exercice, body []byte) (interface{}, error) {
|
func listExerciceHints(exercice fic.Exercice, body []byte) (interface{}, error) {
|
||||||
return exercice.GetHints()
|
return exercice.GetHints()
|
||||||
}
|
}
|
||||||
|
@ -530,35 +516,6 @@ func deleteExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, _ []byte) (interface{}, er
|
||||||
return quiz.Delete()
|
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 {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return sync.ImportFile(sync.GlobalImporter, uf.URI,
|
|
||||||
func(filePath string, origin string) (interface{}, error) {
|
|
||||||
if digest, err := hex.DecodeString(uf.Digest); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
return exercice.ImportFile(filePath, origin, digest)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func showExerciceFile(file fic.EFile, body []byte) (interface{}, error) {
|
|
||||||
return file, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func deleteExerciceFile(file fic.EFile, _ []byte) (interface{}, error) {
|
|
||||||
return file.Delete()
|
|
||||||
}
|
|
||||||
|
|
||||||
func listExerciceTags(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
func listExerciceTags(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||||
return exercice.GetTags()
|
return exercice.GetTags()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"srs.epita.fr/fic-server/admin/sync"
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
@ -16,7 +18,21 @@ func init() {
|
||||||
router.PUT("/api/files/:fileid", apiHandler(fileHandler(updateFile)))
|
router.PUT("/api/files/:fileid", apiHandler(fileHandler(updateFile)))
|
||||||
router.DELETE("/api/files/:fileid", apiHandler(fileHandler(deleteFile)))
|
router.DELETE("/api/files/:fileid", apiHandler(fileHandler(deleteFile)))
|
||||||
|
|
||||||
|
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(exerciceFileHandler(showFile)))
|
||||||
|
router.PUT("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(updateFile)))
|
||||||
|
router.DELETE("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(deleteFile)))
|
||||||
|
|
||||||
|
// Check
|
||||||
router.POST("/api/files/:fileid/check", apiHandler(fileHandler(checkFile)))
|
router.POST("/api/files/:fileid/check", apiHandler(fileHandler(checkFile)))
|
||||||
|
|
||||||
|
// Synchronize
|
||||||
|
router.POST("/api/sync/exercices/:eid/files", apiHandler(exerciceHandler(
|
||||||
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||||
|
return sync.SyncExerciceFiles(sync.GlobalImporter, exercice), nil
|
||||||
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
func listFiles(_ httprouter.Params, body []byte) (interface{}, error) {
|
func listFiles(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||||
|
@ -24,6 +40,10 @@ func listFiles(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||||
return fic.GetFiles()
|
return fic.GetFiles()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listExerciceFiles(exercice fic.Exercice, body []byte) (interface{}, error) {
|
||||||
|
return exercice.GetFiles()
|
||||||
|
}
|
||||||
|
|
||||||
func clearFiles(_ httprouter.Params, _ []byte) (interface{}, error) {
|
func clearFiles(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||||
return fic.ClearFiles()
|
return fic.ClearFiles()
|
||||||
}
|
}
|
||||||
|
@ -32,6 +52,27 @@ func showFile(file fic.EFile, _ []byte) (interface{}, error) {
|
||||||
return file, nil
|
return file, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return sync.ImportFile(sync.GlobalImporter, uf.URI,
|
||||||
|
func(filePath string, origin string) (interface{}, error) {
|
||||||
|
if digest, err := hex.DecodeString(uf.Digest); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return exercice.ImportFile(filePath, origin, digest)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func updateFile(file fic.EFile, body []byte) (interface{}, error) {
|
func updateFile(file fic.EFile, body []byte) (interface{}, error) {
|
||||||
var uf fic.EFile
|
var uf fic.EFile
|
||||||
if err := json.Unmarshal(body, &uf); err != nil {
|
if err := json.Unmarshal(body, &uf); err != nil {
|
||||||
|
|
|
@ -44,7 +44,6 @@
|
||||||
<div class="btn-toolbar bg-secondary justify-content-end" role="toolbar">
|
<div class="btn-toolbar bg-secondary justify-content-end" role="toolbar">
|
||||||
<div class="btn-group" role="group">
|
<div class="btn-group" role="group">
|
||||||
<button type="button" ng-click="syncFiles()" class="btn btn-sm btn-light"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> Synchroniser</button>
|
<button type="button" ng-click="syncFiles()" class="btn btn-sm btn-light"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> Synchroniser</button>
|
||||||
<button type="button" ng-click="addFile()" class="btn btn-sm btn-info"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Ajouter</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<form ng-submit="saveFile()" class="list-group-item bg-light text-dark" ng-repeat="file in files">
|
<form ng-submit="saveFile()" class="list-group-item bg-light text-dark" ng-repeat="file in files">
|
||||||
|
|
Reference in a new issue