Skip to content

Commit

Permalink
save new, getLocales
Browse files Browse the repository at this point in the history
  • Loading branch information
rucsi committed Jan 12, 2024
1 parent f3e2e19 commit 6728595
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
21 changes: 13 additions & 8 deletions internal/api/cms.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
49 changes: 37 additions & 12 deletions internal/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// }

0 comments on commit 6728595

Please sign in to comment.