api: remote route takes advantage from builds functions

This commit is contained in:
nemunaire 2019-09-07 01:25:42 +02:00
parent ded583008a
commit f2fc142869
6 changed files with 83 additions and 6 deletions

View File

@ -52,6 +52,11 @@ func init() {
router.POST("/api/exercices/:eid/tags", apiHandler(exerciceHandler(addExerciceTag))) router.POST("/api/exercices/:eid/tags", apiHandler(exerciceHandler(addExerciceTag)))
router.PUT("/api/exercices/:eid/tags", apiHandler(exerciceHandler(updateExerciceTags))) router.PUT("/api/exercices/:eid/tags", apiHandler(exerciceHandler(updateExerciceTags)))
// Remote
router.GET("/api/remote/themes/:thid/exercices/:exid", apiHandler(sync.ApiGetRemoteExercice))
router.GET("/api/remote/themes/:thid/exercices/:exid/hints", apiHandler(sync.ApiGetRemoteExerciceHints))
router.GET("/api/remote/themes/:thid/exercices/:exid/flags", apiHandler(sync.ApiGetRemoteExerciceFlags))
// Synchronize // Synchronize
router.POST("/api/sync/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler( router.POST("/api/sync/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler(
func(theme fic.Theme, exercice fic.Exercice, _ []byte) (interface{}, error) { func(theme fic.Theme, exercice fic.Exercice, _ []byte) (interface{}, error) {

View File

@ -41,7 +41,7 @@ func init() {
// Remote // Remote
router.GET("/api/remote/themes", apiHandler(sync.ApiListRemoteThemes)) router.GET("/api/remote/themes", apiHandler(sync.ApiListRemoteThemes))
router.GET("/api/remote/themes/:thid", apiHandler(sync.ApiGetRemoteTheme)) router.GET("/api/remote/themes/:thid", apiHandler(sync.ApiGetRemoteTheme))
router.GET("/api/remote/themes/:thid/exercices", apiHandler(themeHandler(sync.ApiListRemoteExercices))) router.GET("/api/remote/themes/:thid/exercices", apiHandler(sync.ApiListRemoteExercices))
// Synchronize // Synchronize
router.GET("/api/sync/deep", apiHandler( router.GET("/api/sync/deep", apiHandler(

View File

@ -4,15 +4,17 @@ import (
"bufio" "bufio"
"crypto" "crypto"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
"path" "path"
"strings" "strings"
"srs.epita.fr/fic-server/libfic" "github.com/julienschmidt/httprouter"
_ "golang.org/x/crypto/blake2b" _ "golang.org/x/crypto/blake2b"
"srs.epita.fr/fic-server/libfic"
) )
func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []fic.EHint, errs []string) { func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []fic.EHint, errs []string) {
@ -106,3 +108,23 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
} }
return return
} }
// ApiListRemoteExerciceHints is an accessor letting foreign packages to access remote exercice hints.
func ApiGetRemoteExerciceHints(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, *theme, path.Join(theme.Path, ps.ByName("exid")), nil)
if exercice != nil {
hints, errs := CheckExerciceHints(GlobalImporter, *exercice)
if hints != nil {
return hints, nil
} else {
return hints, errors.New(fmt.Sprintf("%q", errs))
}
} else {
return exercice, errors.New(fmt.Sprintf("%q", errs))
}
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
}
}

View File

@ -1,6 +1,7 @@
package sync package sync
import ( import (
"errors"
"fmt" "fmt"
"math/rand" "math/rand"
"path" "path"
@ -8,6 +9,8 @@ import (
"strings" "strings"
"unicode" "unicode"
"github.com/julienschmidt/httprouter"
"srs.epita.fr/fic-server/libfic" "srs.epita.fr/fic-server/libfic"
) )
@ -385,3 +388,23 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) {
return return
} }
// ApiListRemoteExerciceFlags is an accessor letting foreign packages to access remote exercice flags.
func ApiGetRemoteExerciceFlags(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, *theme, path.Join(theme.Path, ps.ByName("exid")), nil)
if exercice != nil {
flags, errs := CheckExerciceFlags(GlobalImporter, *exercice, []fic.EFile{})
if flags != nil {
return flags, nil
} else {
return flags, errors.New(fmt.Sprintf("%q", errs))
}
} else {
return exercice, errors.New(fmt.Sprintf("%q", errs))
}
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
}
}

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/julienschmidt/httprouter"
"gopkg.in/russross/blackfriday.v2" "gopkg.in/russross/blackfriday.v2"
"srs.epita.fr/fic-server/libfic" "srs.epita.fr/fic-server/libfic"
@ -264,6 +265,26 @@ func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
} }
// ApiListRemoteExercices is an accessor letting foreign packages to access remote exercices list. // ApiListRemoteExercices is an accessor letting foreign packages to access remote exercices list.
func ApiListRemoteExercices(theme fic.Theme, _ []byte) (interface{}, error) { func ApiListRemoteExercices(ps httprouter.Params, _ []byte) (interface{}, error) {
return GetExercices(GlobalImporter, theme) theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
return GetExercices(GlobalImporter, *theme)
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
}
}
// ApiListRemoteExercice is an accessor letting foreign packages to access remote exercice attributes.
func ApiGetRemoteExercice(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, *theme, path.Join(theme.Path, ps.ByName("exid")), nil)
if exercice != nil {
return exercice, nil
} else {
return exercice, errors.New(fmt.Sprintf("%q", errs))
}
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
}
} }

View File

@ -1,6 +1,7 @@
package sync package sync
import ( import (
"errors"
"fmt" "fmt"
"math/rand" "math/rand"
"path" "path"
@ -159,5 +160,10 @@ func ApiListRemoteThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
// ApiListRemoteTheme is an accessor letting foreign packages to access remote main theme attributes. // ApiListRemoteTheme is an accessor letting foreign packages to access remote main theme attributes.
func ApiGetRemoteTheme(ps httprouter.Params, _ []byte) (interface{}, error) { func ApiGetRemoteTheme(ps httprouter.Params, _ []byte) (interface{}, error) {
return getAuthors(GlobalImporter, ps.ByName("thid")) r, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if r == nil {
return r, errors.New(fmt.Sprintf("%q", errs))
} else {
return r, nil
}
} }