diff --git a/internal/api/cms.go b/internal/api/cms.go index b04093a..9c448ba 100644 --- a/internal/api/cms.go +++ b/internal/api/cms.go @@ -10,6 +10,7 @@ import ( "io" "net/http" "path/filepath" + "strings" "time" "github.com/go-chi/chi/v5" @@ -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) @@ -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} diff --git a/internal/cms/merge.go b/internal/cms/merge.go index 401e348..0c52d53 100644 --- a/internal/cms/merge.go +++ b/internal/cms/merge.go @@ -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] @@ -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 { @@ -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 +}