Skip to content

Commit

Permalink
EmptyLocalisedContent
Browse files Browse the repository at this point in the history
  • Loading branch information
rucsi committed Jan 12, 2024
1 parent e112d87 commit f3e2e19
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 18 deletions.
40 changes: 30 additions & 10 deletions internal/api/cms.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"path/filepath"
"strings"
"time"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -352,6 +353,7 @@ func createOrUpdateEntry(w http.ResponseWriter, r *http.Request) {
if len(entryData.Name) == 0 {
m := "missing entry name"
errReposCommitBlob().Details(m).Log(r, errors.New(m)).Json(w)
return
}

cmsConfig := getConfig(ctx, accessToken, owner, repo, ref)
Expand Down Expand Up @@ -462,17 +464,35 @@ func getEntry(w http.ResponseWriter, r *http.Request) {
return
}

// Get files in directory
path := filepath.Join(cmsConfig.WorkDir, collection, entry)
rc, resp, err := gh.GetAllLocaleContents(ctx, accessToken, owner, repo, ref, path)
if err != nil {
errReposGetBlob().Status(resp.StatusCode).Log(r, err).Json(w)
}
var mc *content.MergedContentData

mc, err := cms.MergeLocalisedContent(rc, *cs)
if err != nil {
errCmsMergeLocalizedContent().Status(resp.StatusCode).Log(r, err).Json(w)
return
if entry != "_new" {
// Get files in directory
path := filepath.Join(cmsConfig.WorkDir, collection, entry)
rc, resp, err := gh.GetAllLocaleContents(ctx, accessToken, owner, repo, ref, path)
if err != nil {
errReposGetBlob().Status(resp.StatusCode).Log(r, err).Json(w)
return
}

mc, err = cms.MergeLocalisedContent(rc, *cs)
if err != nil {
errCmsMergeLocalizedContent().Log(r, err).Json(w)
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)
if err != nil {
errReposGetTree().Status(statusCode).Log(r, err).Json(w)
return
}
mc, err = cms.GetEmptyLocalisedContent(*cs, locales)
if err != nil {
errCmsMergeLocalizedContent().Log(r, err).Json(w)
return
}
}

data := &localizedEntry{Name: mc.Name, Type: mc.Type, Content: mc, Schema: *cs}
Expand Down
44 changes: 36 additions & 8 deletions internal/cms/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ type localePayloads struct {
Contents string `json:"contents"`
}

func getLocalizedFields(cs content.Schema) map[string]bool {
localizedFields := make(map[string]bool)
//Build Localized field bool from schema
for _, csf := range cs.Fields {
if csf.Localized {
localizedFields[csf.ID] = csf.Localized
}
}
return localizedFields
}

func GetNameLocaleFromPath(path string) (string, string) {
dirs := strings.Split(filepath.Dir(path), "/")
name := dirs[len(dirs)-1]
Expand All @@ -29,14 +40,7 @@ func MergeLocalisedContent(rc []*github.RepositoryContent, cs content.Schema) (*
result := &content.MergedContentData{}
result.Fields = make(map[string]map[string]interface{})

localizedFields := make(map[string]bool)

//Build Localized field bool from schema
for _, csf := range cs.Fields {
if csf.Localized {
localizedFields[csf.ID] = csf.Localized
}
}
localizedFields := getLocalizedFields(cs)

// Get default locale content
for _, c := range rc {
Expand Down Expand Up @@ -143,3 +147,27 @@ func SeparateLocalisedContent(user string, mcd content.MergedContentData, locale

return res, nil
}

func GetEmptyLocalisedContent(cs content.Schema, locales []string) (*content.MergedContentData, error) {
result := &content.MergedContentData{}
result.Fields = make(map[string]map[string]interface{})

localizedFields := getLocalizedFields(cs)

for _, csf := range cs.Fields {
k := csf.ID

if result.Fields[k] == nil {
result.Fields[k] = make(map[string]interface{})
}
result.Fields[k][content.DefaultLocale] = nil

if localizedFields[k] {
for _, l := range locales {
result.Fields[k][l] = nil
}
}
}

return result, nil
}

0 comments on commit f3e2e19

Please sign in to comment.