mirror of
https://github.com/ManyakRus/crud_generator.git
synced 2024-11-21 05:05:52 +02:00
сделал objects
This commit is contained in:
parent
29b00469ee
commit
d93684f67e
54
bin/templates/pkg/db/crud_objects/crud_tables_object.go_
Normal file
54
bin/templates/pkg/db/crud_objects/crud_tables_object.go_
Normal file
@ -0,0 +1,54 @@
|
||||
package crud_object_lawsuit_status_types
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/ManyakRus/starter/contextmain"
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
"github.com/ManyakRus/starter/postgres_gorm"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud/crud_lawsuit_status_types"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/db_constants"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/objects/object_lawsuit_status_types"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Crud_DB - объект для CRUD операций через БД
|
||||
type Crud_DB struct {
|
||||
}
|
||||
|
||||
// ReadObject - находит запись в БД по ID, также заполняет внешние поля
|
||||
func (crud Crud_DB) ReadObject(m *object_lawsuit_status_types.ObjectLawsuitStatusType) error {
|
||||
var err error
|
||||
|
||||
ctxMain := contextmain.GetContext()
|
||||
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(db_constants.TIMEOUT_DB_SECONDS))
|
||||
defer ctxCancelFunc()
|
||||
|
||||
db := postgres_gorm.GetConnection()
|
||||
|
||||
err = ReadObject_ctx(ctx, db, m)
|
||||
return err
|
||||
}
|
||||
|
||||
// ReadObject_ctx - находит запись в БД по ID, также заполняет внешние поля
|
||||
func ReadObject_ctx(ctx context.Context, db *gorm.DB, m *object_lawsuit_status_types.ObjectLawsuitStatusType) error {
|
||||
var err error
|
||||
|
||||
if micro.ContextDone(ctx) == true {
|
||||
err = context.Canceled
|
||||
return err
|
||||
}
|
||||
|
||||
//заполним model
|
||||
Model := m.LawsuitStatusType
|
||||
err = crud_lawsuit_status_types.Read_ctx(ctx, db, &Model)
|
||||
if err != nil {
|
||||
err = fmt.Errorf(m.TableNameDB()+" Read() FieldNamesWithPercent, error: %w", FieldNamesWithComma, err)
|
||||
}
|
||||
m.LawsuitStatusType = Model
|
||||
|
||||
//TextFillManyFields
|
||||
|
||||
return err
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
//заполним FieldNameForeign
|
||||
FieldNameForeign := TableNameForeign.FieldNameForeign{}
|
||||
err = crud_TableNameForeign.Read_ctx(ctx, db, &FieldNameForeign)
|
||||
if err != nil {
|
||||
err = fmt.Errorf(FieldNameForeign.TableNameDB()+" Read() FieldNamesWithPercent, error: %w", FieldNamesWithComma, err)
|
||||
}
|
||||
m.Color = Color
|
||||
|
@ -220,10 +220,28 @@ type SettingsINI struct {
|
||||
NEED_USE_DB_VIEWS bool
|
||||
|
||||
//Objects
|
||||
NEED_CREATE_OBJECTS bool
|
||||
PREFIX_OBJECT string
|
||||
TEMPLATE_FOLDERNAME_OBJECTS string
|
||||
TEMPLATES_OBJECTS_FILENAME string
|
||||
NEED_CREATE_OBJECTS bool
|
||||
PREFIX_OBJECT string
|
||||
PREFIX_CRUD_OBJECT string
|
||||
TEMPLATES_OBJECTS_FOLDERNAME string
|
||||
TEMPLATES_MODEL_OBJECTS_FILENAME string
|
||||
|
||||
TEMPLATES_CRUD_OBJECTS_FOLDERNAME string
|
||||
|
||||
TEMPLATES_CRUD_TABLE_READOBJECT_FILENAME string
|
||||
TEMPLATES_CRUD_TABLE_READOBJECT_TEST_FILENAME string
|
||||
TEMPLATES_CRUD_TABLE_READOBJECT_FUNCTION_FILENAME string
|
||||
TEMPLATES_CRUD_TABLE_READOBJECT_FUNCTION_TEST_FILENAME string
|
||||
|
||||
TEMPLATES_GRPC_SERVER_READOBJECT_FILENAME string
|
||||
TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_FILENAME string
|
||||
TEMPLATES_GRPC_SERVER_READOBJECT_TEST_FILENAME string
|
||||
TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_TEST_FILENAME string
|
||||
|
||||
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FILENAME string
|
||||
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_TEST_FILENAME string
|
||||
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_FILENAME string
|
||||
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_TEST_FILENAME string
|
||||
|
||||
//Text DELETED
|
||||
TEXT_DELETED_TABLE string
|
||||
@ -1154,14 +1172,14 @@ func FillSettings() {
|
||||
Settings.TEMPLATES_NAME_PRIMARYKEYS_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATE_FOLDERNAME_OBJECTS"
|
||||
Name = "TEMPLATES_OBJECTS_FOLDERNAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATE_FOLDERNAME_OBJECTS = s
|
||||
Settings.TEMPLATES_OBJECTS_FOLDERNAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_OBJECTS_FILENAME"
|
||||
Name = "TEMPLATES_MODEL_OBJECTS_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_OBJECTS_FILENAME = s
|
||||
Settings.TEMPLATES_MODEL_OBJECTS_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "PREFIX_OBJECT"
|
||||
@ -1193,6 +1211,76 @@ func FillSettings() {
|
||||
s = Getenv(Name, true)
|
||||
Settings.NEED_CREATE_OBJECTS = BoolFromString(s)
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_CRUD_OBJECTS_FOLDERNAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_CRUD_OBJECTS_FOLDERNAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_CRUD_TABLE_READOBJECT_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_CRUD_TABLE_READOBJECT_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_CRUD_TABLE_READOBJECT_TEST_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_CRUD_TABLE_READOBJECT_TEST_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_CRUD_TABLE_READOBJECT_FUNCTION_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_CRUD_TABLE_READOBJECT_FUNCTION_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_GRPC_SERVER_READOBJECT_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_GRPC_SERVER_READOBJECT_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_CRUD_TABLE_READOBJECT_FUNCTION_TEST_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_CRUD_TABLE_READOBJECT_FUNCTION_TEST_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_GRPC_SERVER_READOBJECT_TEST_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_GRPC_SERVER_READOBJECT_TEST_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_TEST_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_TEST_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_TEST_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_TEST_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_TEST_FILENAME"
|
||||
s = Getenv(Name, true)
|
||||
Settings.TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_TEST_FILENAME = s
|
||||
|
||||
//
|
||||
Name = "PREFIX_CRUD_OBJECT"
|
||||
s = Getenv(Name, true)
|
||||
Settings.PREFIX_CRUD_OBJECT = s
|
||||
|
||||
}
|
||||
|
||||
// CurrentDirectory - возвращает текущую директорию ОС
|
||||
|
@ -715,6 +715,15 @@ func Find_ModelTableURL(TableName string) string {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// Find_ObjectTableURL - возвращает URL репозитория object для таблицы TableName
|
||||
func Find_ObjectTableURL(TableName string) string {
|
||||
Otvet := ""
|
||||
|
||||
Otvet = config.Settings.SERVICE_REPOSITORY_URL + "/" + config.Settings.TEMPLATES_OBJECTS_FOLDERNAME + "/" + TableName
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// Find_CrudFuncURL - возвращает URL репозитория crud_func
|
||||
func Find_CrudFuncURL(string) string {
|
||||
Otvet := ""
|
||||
@ -2815,3 +2824,87 @@ func CreateDirectory(DirectoryName string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IsForeignColumn - возвращает true если у этой колонки есть ссылка на другую таблицу (foreign key)
|
||||
func IsForeignColumn(MapAll map[string]*types.Table, Column1 *types.Column) bool {
|
||||
Otvet := false
|
||||
|
||||
TableNameF := Column1.TableKey
|
||||
ColumnNameF := Column1.ColumnKey
|
||||
if TableNameF == "" || ColumnNameF == "" {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = true
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// Find_TableF_ColumnF - для колонки с foreign keys возвращает связанные Table и Column
|
||||
func Find_TableF_ColumnF(MapAll map[string]*types.Table, Column1 *types.Column) (*types.Table, *types.Column) {
|
||||
TableF := &types.Table{}
|
||||
ColumnF := &types.Column{}
|
||||
|
||||
TableNameF := Column1.TableKey
|
||||
ColumnNameF := Column1.ColumnKey
|
||||
if TableNameF == "" || ColumnNameF == "" {
|
||||
return TableF, ColumnF
|
||||
}
|
||||
|
||||
TableF, ok := MapAll[TableNameF]
|
||||
if ok == false {
|
||||
return TableF, ColumnF
|
||||
}
|
||||
|
||||
ColumnF, ok = TableF.MapColumns[ColumnNameF]
|
||||
if ok == false {
|
||||
return TableF, ColumnF
|
||||
}
|
||||
|
||||
return TableF, ColumnF
|
||||
}
|
||||
|
||||
// Find_FieldNamesWithPercent_from_Mass - возвращает строку в формате "Имя1: %v, Имя2: %v"
|
||||
func Find_FieldNamesWithPercent_from_Mass(MassColumns []*types.Column) string {
|
||||
Otvet := ""
|
||||
|
||||
Comma := ""
|
||||
for _, ColumnPK1 := range MassColumns {
|
||||
Otvet = Otvet + Comma + ColumnPK1.NameGo + ": %v"
|
||||
Comma = ", "
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// Find_FieldNamesWithComma_from_Mass - возвращает строку в формате "Имя1: %v, Имя2: %v"
|
||||
func Find_FieldNamesWithComma_from_Mass(MassColumns []*types.Column) string {
|
||||
Otvet := ""
|
||||
Comma := ""
|
||||
for _, ColumnPK1 := range MassColumns {
|
||||
Otvet = Otvet + Comma + ColumnPK1.NameGo
|
||||
Comma = ", "
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// Find_FieldNamesWithPercent_from_Table - возвращает строку в формате "Имя1: %v, Имя2: %v"
|
||||
func Find_FieldNamesWithPercent_from_Table(Table1 *types.Table) string {
|
||||
Otvet := ""
|
||||
|
||||
MassPK := Find_PrimaryKeyColumns(Table1)
|
||||
Otvet = Find_FieldNamesWithPercent_from_Mass(MassPK)
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// Find_FieldNamesWithComma_from_Table - возвращает строку в формате "Имя1: %v, Имя2: %v"
|
||||
func Find_FieldNamesWithComma_from_Table(Table1 *types.Table) string {
|
||||
Otvet := ""
|
||||
|
||||
MassPK := Find_PrimaryKeyColumns(Table1)
|
||||
Otvet = Find_FieldNamesWithComma_from_Mass(MassPK)
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
273
internal/create_files/crud_objects/crud_objects.go
Normal file
273
internal/create_files/crud_objects/crud_objects.go
Normal file
@ -0,0 +1,273 @@
|
||||
package crud_object
|
||||
|
||||
import (
|
||||
"github.com/ManyakRus/crud_generator/internal/config"
|
||||
"github.com/ManyakRus/crud_generator/internal/create_files"
|
||||
"github.com/ManyakRus/crud_generator/internal/types"
|
||||
"github.com/ManyakRus/starter/log"
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CreateFiles_ReadObject - создаёт 1 файл в папке crud
|
||||
func CreateFiles_ReadObject(MapAll map[string]*types.Table, Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
DirBin := micro.ProgramDir_bin()
|
||||
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
||||
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
||||
DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATES_CRUD_OBJECTS_FOLDERNAME + micro.SeparatorFile()
|
||||
DirReadyCrud := DirReady + config.Settings.TEMPLATES_CRUD_OBJECTS_FOLDERNAME + micro.SeparatorFile()
|
||||
|
||||
FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_READOBJECT_FILENAME
|
||||
TableName := strings.ToLower(Table1.Name)
|
||||
DirReadyTable := DirReadyCrud + config.Settings.PREFIX_CRUD_OBJECT + TableName
|
||||
FilenameReady := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_CRUD_OBJECT + TableName
|
||||
|
||||
//создадим каталог
|
||||
create_files.CreateDirectory(DirReadyTable)
|
||||
|
||||
//загрузим шаблон файла
|
||||
bytes, err := os.ReadFile(FilenameTemplateCrud)
|
||||
if err != nil {
|
||||
log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err)
|
||||
}
|
||||
TextCrud := string(bytes)
|
||||
|
||||
//загрузим шаблон файла функции
|
||||
FilenameTemplateCrudFunction := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_READALL_FUNCTION_FILENAME
|
||||
bytes, err = os.ReadFile(FilenameTemplateCrudFunction)
|
||||
if err != nil {
|
||||
log.Panic("ReadFile() ", FilenameTemplateCrudFunction, " error: ", err)
|
||||
}
|
||||
TextTemplatedFunction := string(bytes)
|
||||
|
||||
//заменим имя пакета на новое
|
||||
TextCrud = create_files.Replace_PackageName(TextCrud, DirReadyTable)
|
||||
|
||||
//ModelName := Table1.NameGo
|
||||
//заменим импорты
|
||||
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
||||
TextCrud = create_files.Delete_TemplateRepositoryImports(TextCrud)
|
||||
|
||||
//
|
||||
ModelTableURL := create_files.Find_ModelTableURL(TableName)
|
||||
TextCrud = create_files.AddImport(TextCrud, ModelTableURL)
|
||||
|
||||
//
|
||||
ObjectTableURL := create_files.Find_ObjectTableURL(TableName)
|
||||
TextCrud = create_files.AddImport(TextCrud, ObjectTableURL)
|
||||
|
||||
//
|
||||
ConstantsURL := create_files.Find_DBConstantsURL()
|
||||
TextCrud = create_files.AddImport(TextCrud, ConstantsURL)
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
//FieldNamesWithPercent
|
||||
FieldNamesWithPercent := create_files.Find_FieldNamesWithPercent_from_Table(Table1)
|
||||
TextCrud = strings.ReplaceAll(TextCrud, "FieldNamesWithPercent", FieldNamesWithPercent)
|
||||
|
||||
//FieldNamesWithComma
|
||||
FieldNamesWithComma := create_files.Find_FieldNamesWithComma_from_Table(Table1)
|
||||
TextCrud = strings.ReplaceAll(TextCrud, "FieldNamesWithComma", FieldNamesWithComma)
|
||||
|
||||
//создание функций
|
||||
TextCrudFunc := CreateFiles_ReadObjectTable(MapAll, Table1, TextTemplatedFunction)
|
||||
if TextCrudFunc == "" {
|
||||
return err
|
||||
}
|
||||
TextCrud = TextCrud + TextCrudFunc
|
||||
|
||||
//создание текста
|
||||
TextCrud = create_files.Replace_TemplateModel_to_Model(TextCrud, Table1.NameGo)
|
||||
TextCrud = create_files.Replace_TemplateTableName_to_TableName(TextCrud, Table1.Name)
|
||||
TextCrud = create_files.AddText_ModuleGenerated(TextCrud)
|
||||
|
||||
//TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
||||
//TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
||||
//TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud
|
||||
|
||||
//замена импортов на новые URL
|
||||
TextCrud = create_files.Replace_RepositoryImportsURL(TextCrud)
|
||||
|
||||
//uuid
|
||||
TextCrud = create_files.CheckAndAdd_ImportUUID_FromText(TextCrud)
|
||||
|
||||
//alias
|
||||
TextCrud = create_files.CheckAndAdd_ImportAlias(TextCrud)
|
||||
|
||||
//удаление пустого импорта
|
||||
TextCrud = create_files.Delete_EmptyImport(TextCrud)
|
||||
|
||||
//удаление пустых строк
|
||||
TextCrud = create_files.Delete_EmptyLines(TextCrud)
|
||||
|
||||
//запись файла
|
||||
err = os.WriteFile(FilenameReady, []byte(TextCrud), config.Settings.FILE_PERMISSIONS)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateFiles_ReadObjectTable - создаёт текст всех функций
|
||||
func CreateFiles_ReadObjectTable(MapAll map[string]*types.Table, Table1 *types.Table, TextTemplateFunction string) string {
|
||||
Otvet := ""
|
||||
|
||||
for _, Column1 := range Table1.MapColumns {
|
||||
IsForeignColumn := create_files.IsForeignColumn(MapAll, Column1)
|
||||
if IsForeignColumn == false {
|
||||
continue
|
||||
}
|
||||
Otvet1 := CreateFiles_ReadObject_Table1(MapAll, Table1, Column1, TextTemplateFunction)
|
||||
Otvet = Otvet + Otvet1
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// CreateFiles_ReadObject_Table1 - создаёт текст всех функций
|
||||
func CreateFiles_ReadObject_Table1(MapAll map[string]*types.Table, Table1 *types.Table, Column1 *types.Column, TextTemplateFunction string) string {
|
||||
Otvet := TextTemplateFunction
|
||||
|
||||
TableF, ColumnF := create_files.Find_TableF_ColumnF(MapAll, Column1)
|
||||
|
||||
//FieldNameForeign
|
||||
FieldNameForeign := ColumnF.Name
|
||||
Otvet = strings.ReplaceAll(Otvet, "FieldNameForeign", FieldNameForeign)
|
||||
|
||||
//TableNameForeign
|
||||
TableNameForeign := TableF.Name
|
||||
Otvet = strings.ReplaceAll(Otvet, "TableNameForeign", TableNameForeign)
|
||||
|
||||
//FieldNamesWithPercent
|
||||
FieldNamesWithPercent := create_files.Find_FieldNamesWithPercent_from_Table(Table1)
|
||||
Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithPercent", FieldNamesWithPercent)
|
||||
|
||||
//FieldNamesWithComma
|
||||
FieldNamesWithComma := create_files.Find_FieldNamesWithComma_from_Table(Table1)
|
||||
Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithComma", FieldNamesWithComma)
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// CreateFiles_ReadObject_Test - создаёт 1 файл в папке crud
|
||||
func CreateFiles_ReadObject_Test(MapAll map[string]*types.Table, Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
DirBin := micro.ProgramDir_bin()
|
||||
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
||||
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
||||
DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATES_CRUD_OBJECTS_FOLDERNAME + micro.SeparatorFile()
|
||||
DirReadyCrud := DirReady + config.Settings.TEMPLATES_CRUD_OBJECTS_FOLDERNAME + micro.SeparatorFile()
|
||||
|
||||
FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_READALL_TEST_FILENAME
|
||||
TableName := strings.ToLower(Table1.Name)
|
||||
DirReadyTable := DirReadyCrud + config.Settings.PREFIX_CRUD_OBJECT + TableName
|
||||
FilenameReady := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_CRUD_OBJECT + TableName
|
||||
|
||||
//создадим каталог
|
||||
create_files.CreateDirectory(DirReadyTable)
|
||||
|
||||
//загрузим шаблон файла
|
||||
bytes, err := os.ReadFile(FilenameTemplateCrud)
|
||||
if err != nil {
|
||||
log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err)
|
||||
}
|
||||
TextCrud := string(bytes)
|
||||
|
||||
//загрузим шаблон файла функции
|
||||
FilenameTemplateCrudFunction := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_READALL_FUNCTION_TEST_FILENAME
|
||||
bytes, err = os.ReadFile(FilenameTemplateCrudFunction)
|
||||
if err != nil {
|
||||
log.Panic("ReadFile() ", FilenameTemplateCrudFunction, " error: ", err)
|
||||
}
|
||||
TextTemplatedFunction := string(bytes)
|
||||
|
||||
//заменим имя пакета на новое
|
||||
TextCrud = create_files.Replace_PackageName(TextCrud, DirReadyTable)
|
||||
|
||||
//ModelName := Table1.NameGo
|
||||
//заменим импорты
|
||||
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
||||
TextCrud = create_files.Delete_TemplateRepositoryImports(TextCrud)
|
||||
|
||||
//ModelTableURL := create_files.Find_ModelTableURL(TableName)
|
||||
//TextCrud = create_files.AddImport(TextCrud, ModelTableURL)
|
||||
|
||||
CrudFuncURL := create_files.Find_CrudFuncURL(TableName)
|
||||
TextCrud = create_files.AddImport(TextCrud, CrudFuncURL)
|
||||
|
||||
}
|
||||
|
||||
//создание функций
|
||||
TextCrudFunc := CreateFiles_ReadObject_TestTable(Table1, TextTemplatedFunction)
|
||||
if TextCrudFunc == "" {
|
||||
return err
|
||||
}
|
||||
TextCrud = TextCrud + TextCrudFunc
|
||||
|
||||
//создание текста
|
||||
TextCrud = create_files.Replace_TemplateModel_to_Model(TextCrud, Table1.NameGo)
|
||||
TextCrud = create_files.Replace_TemplateTableName_to_TableName(TextCrud, Table1.Name)
|
||||
TextCrud = create_files.AddText_ModuleGenerated(TextCrud)
|
||||
|
||||
//TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
||||
//TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
||||
//TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud
|
||||
|
||||
//замена импортов на новые URL
|
||||
TextCrud = create_files.Replace_RepositoryImportsURL(TextCrud)
|
||||
|
||||
//uuid
|
||||
TextCrud = create_files.CheckAndAdd_ImportUUID_FromText(TextCrud)
|
||||
|
||||
//alias
|
||||
TextCrud = create_files.CheckAndAdd_ImportAlias(TextCrud)
|
||||
|
||||
//удаление пустого импорта
|
||||
TextCrud = create_files.Delete_EmptyImport(TextCrud)
|
||||
|
||||
//удаление пустых строк
|
||||
TextCrud = create_files.Delete_EmptyLines(TextCrud)
|
||||
|
||||
//запись файла
|
||||
err = os.WriteFile(FilenameReady, []byte(TextCrud), config.Settings.FILE_PERMISSIONS)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateFiles_ReadObject_TestTable - создаёт текст всех функций
|
||||
func CreateFiles_ReadObject_TestTable(MapAll map[string]*types.Table, Table1 *types.Table, TextTemplateFunction string) string {
|
||||
Otvet := ""
|
||||
|
||||
for TableReadObject1, _ := range types.MapReadObject {
|
||||
if TableReadObject1.Name != Table1.Name {
|
||||
continue
|
||||
}
|
||||
Otvet1 := CreateFiles_ReadObject_Test_Table1(Table1, TextTemplateFunction)
|
||||
Otvet = Otvet + Otvet1
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// CreateFiles_ReadObject_Test_Table1 - создаёт текст всех функций
|
||||
func CreateFiles_ReadObject_Test_Table1(Table1 *types.Table, TextTemplateFunction string) string {
|
||||
Otvet := TextTemplateFunction
|
||||
|
||||
//
|
||||
TextFieldsDefaultValue := create_files.Find_PrimaryKeysDefaultValues(Table1)
|
||||
|
||||
//
|
||||
Otvet = strings.ReplaceAll(Otvet, "FieldNamesDefaultValues", TextFieldsDefaultValue)
|
||||
|
||||
//
|
||||
PrimaryKeyNamesWithComma := create_files.Find_PrimaryKeyNamesWithComma(Table1)
|
||||
Otvet = strings.ReplaceAll(Otvet, "PrimaryKeyNamesWithComma", PrimaryKeyNamesWithComma)
|
||||
|
||||
return Otvet
|
||||
}
|
@ -18,10 +18,10 @@ func CreateFiles(MapAll map[string]*types.Table, Table1 *types.Table) error {
|
||||
DirBin := micro.ProgramDir_bin()
|
||||
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
||||
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
||||
DirTemplatesObjects := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_OBJECTS + micro.SeparatorFile()
|
||||
DirReadyDB := DirReady + config.Settings.TEMPLATE_FOLDERNAME_OBJECTS + micro.SeparatorFile()
|
||||
DirTemplatesObjects := DirTemplates + config.Settings.TEMPLATES_OBJECTS_FOLDERNAME + micro.SeparatorFile()
|
||||
DirReadyDB := DirReady + config.Settings.TEMPLATES_OBJECTS_FOLDERNAME + micro.SeparatorFile()
|
||||
|
||||
FilenameTemplateObject := DirTemplatesObjects + config.Settings.TEMPLATES_OBJECTS_FILENAME
|
||||
FilenameTemplateObject := DirTemplatesObjects + config.Settings.TEMPLATES_MODEL_OBJECTS_FILENAME
|
||||
TableName := strings.ToLower(Table1.Name)
|
||||
DirReadyTable := DirReadyDB + config.Settings.PREFIX_OBJECT + TableName
|
||||
FilenameReadyObjects := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_OBJECT + TableName + ".go"
|
||||
|
Loading…
Reference in New Issue
Block a user