...

Source file src/gitlab.hexacode.org/go-libs/microservice/config/config_option_strapi_schemas.go

Documentation: gitlab.hexacode.org/go-libs/microservice/config

     1  /*
     2   * Micro Service
     3   * Package: gitlab.hexacode.org/go-libs/microservice
     4   * Maintainer: Azzis Arswendo <azzis@hexacode.org>
     5   *
     6   * Copyright (C) 2023 Hexacode Teknologi Indonesia
     7   * All Rights Reserved
     8   */
     9  
    10  package config
    11  
    12  import (
    13  	"encoding/json"
    14  	"fmt"
    15  
    16  	"gitlab.hexacode.org/go-libs/hctypes"
    17  )
    18  
    19  var schema_template = hctypes.Dict{
    20  	"kind":           "collectionType",
    21  	"collectionName": "tests",
    22  	"info": hctypes.Dict{
    23  		"singularName": "test",
    24  		"pluralName":   "tests",
    25  		"displayName":  "Test",
    26  	},
    27  	"options": hctypes.Dict{
    28  		"draftAndPublish": true,
    29  	},
    30  	"pluginOptions": hctypes.Dict{},
    31  	"attributes": hctypes.Dict{
    32  		"name": hctypes.Dict{
    33  			"type": "string",
    34  		},
    35  		"email": hctypes.Dict{
    36  			"type": "email",
    37  		},
    38  	},
    39  }
    40  
    41  var controllers_template = `'use strict';
    42  
    43  /**
    44   * %s controller
    45   */
    46  
    47  const { createCoreController } = require('@strapi/strapi').factories;
    48  
    49  module.exports = createCoreController('api::%s.%s');`
    50  
    51  var routes_template = `'use strict';
    52  
    53  /**
    54   * %s router
    55   */
    56  
    57  const { createCoreRouter } = require('@strapi/strapi').factories;
    58  
    59  module.exports = createCoreRouter('api::%s.%s');`
    60  
    61  var services_template = `'use strict';
    62  
    63  /**
    64   * %s service
    65   */
    66  
    67  const { createCoreService } = require('@strapi/strapi').factories;
    68  
    69  module.exports = createCoreService('api::%s.%s');`
    70  
    71  func create_controllers(name string) string {
    72  	return fmt.Sprintf(controllers_template, name, name, name)
    73  }
    74  
    75  func create_routes(name string) string {
    76  	return fmt.Sprintf(routes_template, name, name, name)
    77  }
    78  
    79  func create_services(name string) string {
    80  	return fmt.Sprintf(services_template, name, name, name)
    81  }
    82  
    83  func create_strapi_schema_file(name string, label string, fields hctypes.Dict) string {
    84  	schema := schema_template.Copy()
    85  	schema["collectionName"] = name
    86  	schema["attributes"] = fields
    87  
    88  	info := schema["info"].(hctypes.Dict)
    89  	info["displayName"] = label
    90  	info["singularName"] = name
    91  	info["pluralName"] = fmt.Sprintf("%s-table", name)
    92  
    93  	schema["info"] = info
    94  
    95  	ret, err := json.MarshalIndent(schema, "", "  ")
    96  
    97  	if err != nil {
    98  		return ""
    99  	}
   100  
   101  	return string(ret)
   102  }
   103  
   104  func create_strapi_schema_user_file(fields hctypes.Dict) string {
   105  	schema := schema_template.Copy()
   106  	schema["collectionName"] = "up_users"
   107  	schema["attributes"] = fields
   108  
   109  	info := schema["info"].(hctypes.Dict)
   110  	info["displayName"] = "User"
   111  	info["singularName"] = "user"
   112  	info["pluralName"] = "users"
   113  
   114  	schema["info"] = info
   115  	schema["options"] = hctypes.Dict{
   116  		"draftAndPublish": false,
   117  	}
   118  
   119  	ret, err := json.MarshalIndent(schema, "", "  ")
   120  
   121  	if err != nil {
   122  		return ""
   123  	}
   124  
   125  	return string(ret)
   126  }
   127  
   128  // LoadOptionsStrapiSchemas loads Strapi schemas and returns a map of paths to the created schema files.
   129  //
   130  // No parameters.
   131  // Returns a map of strings where the key is the file path and the value is the created schema file content.
   132  func (conf *Config) LoadOptionsStrapiSchemas() map[string]string {
   133  	options := conf.Options
   134  	if options == nil {
   135  		return map[string]string{}
   136  	}
   137  
   138  	strapi_schemas, ok := options.GetList("strapi_schemas")
   139  
   140  	if !ok {
   141  		return map[string]string{}
   142  	}
   143  
   144  	ret := map[string]string{}
   145  
   146  	for i := 0; i < len(strapi_schemas); i++ {
   147  		strapi_schema, ok := strapi_schemas.GetDict(i)
   148  		if !ok {
   149  			continue
   150  		}
   151  
   152  		if strapi_schema == nil {
   153  			continue
   154  		}
   155  
   156  		name, ok := strapi_schema.GetString("name")
   157  		if !ok {
   158  			continue
   159  		}
   160  
   161  		label, ok := strapi_schema.GetString("label")
   162  		if !ok {
   163  			continue
   164  		}
   165  
   166  		fields, ok := strapi_schema.GetDict("fields")
   167  		if !ok {
   168  			continue
   169  		}
   170  
   171  		if name == "user" {
   172  			ret["user"] = create_strapi_schema_user_file(fields)
   173  		} else {
   174  			content_types_schema := fmt.Sprintf("%s/content-types/%s/schema.json", name, name)
   175  			controllers := fmt.Sprintf("%s/controllers/%s.js", name, name)
   176  			routes := fmt.Sprintf("%s/routes/%s.js", name, name)
   177  			services := fmt.Sprintf("%s/services/%s.js", name, name)
   178  
   179  			ret[content_types_schema] = create_strapi_schema_file(string(name), string(label), fields)
   180  			ret[controllers] = create_controllers(string(name))
   181  			ret[routes] = create_routes(string(name))
   182  			ret[services] = create_services(string(name))
   183  		}
   184  	}
   185  
   186  	return ret
   187  }
   188  

View as plain text