From f2fc1428693ad66af86661ec20590e9e4b7f6d83 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sat, 7 Sep 2019 01:25:42 +0200 Subject: [PATCH] api: remote route takes advantage from builds functions --- admin/api/exercice.go | 5 +++++ admin/api/theme.go | 2 +- admin/sync/exercice_hints.go | 26 ++++++++++++++++++++++++-- admin/sync/exercice_keys.go | 23 +++++++++++++++++++++++ admin/sync/exercices.go | 25 +++++++++++++++++++++++-- admin/sync/themes.go | 8 +++++++- 6 files changed, 83 insertions(+), 6 deletions(-) diff --git a/admin/api/exercice.go b/admin/api/exercice.go index 4522a3fc..81d1e63c 100644 --- a/admin/api/exercice.go +++ b/admin/api/exercice.go @@ -52,6 +52,11 @@ func init() { router.POST("/api/exercices/:eid/tags", apiHandler(exerciceHandler(addExerciceTag))) 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 router.POST("/api/sync/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler( func(theme fic.Theme, exercice fic.Exercice, _ []byte) (interface{}, error) { diff --git a/admin/api/theme.go b/admin/api/theme.go index 1fbcdd65..dc4083ca 100644 --- a/admin/api/theme.go +++ b/admin/api/theme.go @@ -41,7 +41,7 @@ func init() { // Remote router.GET("/api/remote/themes", apiHandler(sync.ApiListRemoteThemes)) 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 router.GET("/api/sync/deep", apiHandler( diff --git a/admin/sync/exercice_hints.go b/admin/sync/exercice_hints.go index b6ff8d98..ac1042ee 100644 --- a/admin/sync/exercice_hints.go +++ b/admin/sync/exercice_hints.go @@ -4,15 +4,17 @@ import ( "bufio" "crypto" "encoding/hex" + "errors" "fmt" "io" "os" "path" "strings" - "srs.epita.fr/fic-server/libfic" - + "github.com/julienschmidt/httprouter" _ "golang.org/x/crypto/blake2b" + + "srs.epita.fr/fic-server/libfic" ) 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 } + +// 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)) + } +} diff --git a/admin/sync/exercice_keys.go b/admin/sync/exercice_keys.go index 13635fc1..f270437f 100644 --- a/admin/sync/exercice_keys.go +++ b/admin/sync/exercice_keys.go @@ -1,6 +1,7 @@ package sync import ( + "errors" "fmt" "math/rand" "path" @@ -8,6 +9,8 @@ import ( "strings" "unicode" + "github.com/julienschmidt/httprouter" + "srs.epita.fr/fic-server/libfic" ) @@ -385,3 +388,23 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) { 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)) + } +} diff --git a/admin/sync/exercices.go b/admin/sync/exercices.go index 04f295b1..4b7116eb 100644 --- a/admin/sync/exercices.go +++ b/admin/sync/exercices.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/BurntSushi/toml" + "github.com/julienschmidt/httprouter" "gopkg.in/russross/blackfriday.v2" "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. -func ApiListRemoteExercices(theme fic.Theme, _ []byte) (interface{}, error) { - return GetExercices(GlobalImporter, theme) +func ApiListRemoteExercices(ps httprouter.Params, _ []byte) (interface{}, error) { + 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)) + } } diff --git a/admin/sync/themes.go b/admin/sync/themes.go index 85cb12cc..d3aa485c 100644 --- a/admin/sync/themes.go +++ b/admin/sync/themes.go @@ -1,6 +1,7 @@ package sync import ( + "errors" "fmt" "math/rand" "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. 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 + } }