/* * Micro Service * Package: gitlab.hexacode.org/go-libs/microservice * Maintainer: Azzis Arswendo * * Copyright (C) 2023 Hexacode Teknologi Indonesia * All Rights Reserved */ package config import ( "encoding/json" "fmt" "gitlab.hexacode.org/go-libs/hctypes" ) var schema_template = hctypes.Dict{ "kind": "collectionType", "collectionName": "tests", "info": hctypes.Dict{ "singularName": "test", "pluralName": "tests", "displayName": "Test", }, "options": hctypes.Dict{ "draftAndPublish": true, }, "pluginOptions": hctypes.Dict{}, "attributes": hctypes.Dict{ "name": hctypes.Dict{ "type": "string", }, "email": hctypes.Dict{ "type": "email", }, }, } var controllers_template = `'use strict'; /** * %s controller */ const { createCoreController } = require('@strapi/strapi').factories; module.exports = createCoreController('api::%s.%s');` var routes_template = `'use strict'; /** * %s router */ const { createCoreRouter } = require('@strapi/strapi').factories; module.exports = createCoreRouter('api::%s.%s');` var services_template = `'use strict'; /** * %s service */ const { createCoreService } = require('@strapi/strapi').factories; module.exports = createCoreService('api::%s.%s');` func create_controllers(name string) string { return fmt.Sprintf(controllers_template, name, name, name) } func create_routes(name string) string { return fmt.Sprintf(routes_template, name, name, name) } func create_services(name string) string { return fmt.Sprintf(services_template, name, name, name) } func create_strapi_schema_file(name string, label string, fields hctypes.Dict) string { schema := schema_template.Copy() schema["collectionName"] = name schema["attributes"] = fields info := schema["info"].(hctypes.Dict) info["displayName"] = label info["singularName"] = name info["pluralName"] = fmt.Sprintf("%s-table", name) schema["info"] = info ret, err := json.MarshalIndent(schema, "", " ") if err != nil { return "" } return string(ret) } func create_strapi_schema_user_file(fields hctypes.Dict) string { schema := schema_template.Copy() schema["collectionName"] = "up_users" schema["attributes"] = fields info := schema["info"].(hctypes.Dict) info["displayName"] = "User" info["singularName"] = "user" info["pluralName"] = "users" schema["info"] = info schema["options"] = hctypes.Dict{ "draftAndPublish": false, } ret, err := json.MarshalIndent(schema, "", " ") if err != nil { return "" } return string(ret) } // LoadOptionsStrapiSchemas loads Strapi schemas and returns a map of paths to the created schema files. // // No parameters. // Returns a map of strings where the key is the file path and the value is the created schema file content. func (conf *Config) LoadOptionsStrapiSchemas() map[string]string { options := conf.Options if options == nil { return map[string]string{} } strapi_schemas, ok := options.GetList("strapi_schemas") if !ok { return map[string]string{} } ret := map[string]string{} for i := 0; i < len(strapi_schemas); i++ { strapi_schema, ok := strapi_schemas.GetDict(i) if !ok { continue } if strapi_schema == nil { continue } name, ok := strapi_schema.GetString("name") if !ok { continue } label, ok := strapi_schema.GetString("label") if !ok { continue } fields, ok := strapi_schema.GetDict("fields") if !ok { continue } if name == "user" { ret["user"] = create_strapi_schema_user_file(fields) } else { content_types_schema := fmt.Sprintf("%s/content-types/%s/schema.json", name, name) controllers := fmt.Sprintf("%s/controllers/%s.js", name, name) routes := fmt.Sprintf("%s/routes/%s.js", name, name) services := fmt.Sprintf("%s/services/%s.js", name, name) ret[content_types_schema] = create_strapi_schema_file(string(name), string(label), fields) ret[controllers] = create_controllers(string(name)) ret[routes] = create_routes(string(name)) ret[services] = create_services(string(name)) } } return ret }