From 6728595683008792e0b5431c2806751a471bdfbe Mon Sep 17 00:00:00 2001 From: rucsi Date: Fri, 12 Jan 2024 16:56:29 -0600 Subject: [PATCH] save new, getLocales --- internal/api/cms.go | 21 +++++++++++------- internal/api/helpers.go | 49 +++++++++++++++++++++++++++++++---------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/internal/api/cms.go b/internal/api/cms.go index 9c448ba..5906faf 100644 --- a/internal/api/cms.go +++ b/internal/api/cms.go @@ -356,9 +356,9 @@ func createOrUpdateEntry(w http.ResponseWriter, r *http.Request) { return } - cmsConfig := getConfig(ctx, accessToken, owner, repo, ref) - path := filepath.Join(cmsConfig.WorkDir, collection, entry) + entryData.Name = strings.ToLower(entryData.Name) + cmsConfig := getConfig(ctx, accessToken, owner, repo, ref) // if !entryData.SaveSchema { // schema := getSchema(ctx, accessToken, owner, repo, ref, collection, cmsConfig.WorkDir) // if schema.Available() { @@ -377,7 +377,15 @@ func createOrUpdateEntry(w http.ResponseWriter, r *http.Request) { return } - locales, statusCode, err := getLocales(ctx, accessToken, owner, repo, ref, path) + if len(contentData.Name) == 0 { + now := time.Now().UTC() + contentData.Name = entryData.Name + contentData.ID = entryData.Name + contentData.CreatedAt = &now + contentData.CreatedBy = entryData.Login + } + + locales, statusCode, err := getLocales(ctx, accessToken, owner, repo, ref) if err != nil { errReposGetTree().Status(statusCode).Log(r, err).Json(w) return @@ -481,9 +489,7 @@ func getEntry(w http.ResponseWriter, r *http.Request) { return } } else { - // get product files that has to exist -- HACK !!! - path := filepath.Join(cmsConfig.WorkDir, "product", strings.TrimPrefix(repo, "cms-")) - locales, statusCode, err := getLocales(ctx, accessToken, owner, repo, ref, path) + locales, statusCode, err := getLocales(ctx, accessToken, owner, repo, ref) if err != nil { errReposGetTree().Status(statusCode).Log(r, err).Json(w) return @@ -675,8 +681,7 @@ func getSetting(w http.ResponseWriter, r *http.Request) { setting := chi.URLParam(r, "setting") path := filepath.Join(cms.SettingsFolder, setting) - - fc, resp, err := gh.GetFileContent(ctx, accessToken, owner, repo, ref, path) + fc, resp, err := gh.GetFileContent(ctx, accessToken, owner, repo, ref, path+".json") if err != nil { errReposGetBlob().Status(resp.StatusCode).Log(r, err).Json(w) return diff --git a/internal/api/helpers.go b/internal/api/helpers.go index 3149015..3a97250 100644 --- a/internal/api/helpers.go +++ b/internal/api/helpers.go @@ -2,26 +2,51 @@ package api import ( "context" + "encoding/json" + "net/http" + "path/filepath" "github.com/moonwalker/moonbase/internal/cms" - "github.com/moonwalker/moonbase/pkg/content" gh "github.com/moonwalker/moonbase/pkg/github" ) -func getLocales(ctx context.Context, accessToken, owner, repo, branch, path string) ([]string, int, error) { - rcs, resp, err := gh.GetAllLocaleContents(ctx, accessToken, owner, repo, branch, path) +const localesConfig = "locales.json" + +func getLocales(ctx context.Context, accessToken, owner, repo, ref string) ([]string, int, error) { + path := filepath.Join(cms.SettingsFolder, localesConfig) + + fc, resp, err := gh.GetFileContent(ctx, accessToken, owner, repo, ref, path) if err != nil { return nil, resp.StatusCode, err } + blob, err := fc.GetContent() + if err != nil { + return nil, http.StatusInternalServerError, err + } - res := make([]string, 0) - for _, rc := range rcs { - if *rc.Type == "file" && *rc.Name != content.JsonSchemaName { - fn, l := cms.GetNameLocaleFromPath(*rc.Path) - if fn != "" { - res = append(res, l) - } - } + locales := make([]string, 0) + err = json.Unmarshal([]byte(blob), &locales) + if err != nil { + return nil, http.StatusInternalServerError, err } - return res, 0, nil + + return locales, http.StatusOK, nil } + +// func getLocales(ctx context.Context, accessToken, owner, repo, branch, path string) ([]string, int, error) { +// rcs, resp, err := gh.GetAllLocaleContents(ctx, accessToken, owner, repo, branch, path) +// if err != nil { +// return nil, resp.StatusCode, err +// } + +// res := make([]string, 0) +// for _, rc := range rcs { +// if *rc.Type == "file" && *rc.Name != content.JsonSchemaName { +// fn, l := cms.GetNameLocaleFromPath(*rc.Path) +// if fn != "" { +// res = append(res, l) +// } +// } +// } +// return res, 0, nil +// }