From 67b9f1cb39b1d7f151761e6c8874be692038fc4f Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Tue, 9 Jan 2024 18:11:21 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20./crud?= =?UTF-8?q?=5Ffunctions=5Frename.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/Makefile_ | 4 +- bin/templates/configs_/convert_id.json | 11 ++ .../configs_/crud_functions_rename.json | 6 + .../configs_/model_crud_delete_functions.json | 1 + bin/templates/configs_/name_replace.json | 17 ++ bin/templates/configs_/nullable.json | 2 + bin/templates/configs_/settings.txt | 8 +- .../app/grpc/server_grpc/server_grpc_test.go_ | 3 +- .../starter_tables/starter_tables.go_ | 12 ++ .../starter_tables/starter_tables_test.go_ | 12 ++ .../grpc_client/grpc_client_table_test.go_ | 42 ++++- .../nrpc_client/nrpc_client_table_test.go_ | 41 ++++- .../pkg/object_model/entities/model_crud.go_ | 34 ++-- internal/constants/constants.go | 4 + .../create_files/crud_starter/crud_starter.go | 24 ++- .../crud_starter_tables.go | 163 ++++++++++++++++++ .../crud_starter_tables_test.go | 1 + internal/logic/logic.go | 8 + 18 files changed, 352 insertions(+), 41 deletions(-) create mode 100644 bin/templates/pkg/crud_starter/starter_tables/starter_tables.go_ create mode 100644 bin/templates/pkg/crud_starter/starter_tables/starter_tables_test.go_ create mode 100644 internal/create_files/crud_starter_tables/crud_starter_tables.go create mode 100644 internal/create_files/crud_starter_tables/crud_starter_tables_test.go diff --git a/bin/templates/Makefile_ b/bin/templates/Makefile_ index b8896c4..a7db086 100644 --- a/bin/templates/Makefile_ +++ b/bin/templates/Makefile_ @@ -5,7 +5,8 @@ FILEMAIN=./cmd/$(SERVICENAME)/main.go FILEAPP=./bin/$(SERVICENAME) NEW_REPO=$(SERVICENAME) -GENERATION_PROTO=cd ./api && ./generation_code.sh +CD_GENERATION_PROTO=cd ./api +GENERATION_PROTO=generation_code.sh run: @@ -53,6 +54,7 @@ conn: init: clear go mod init gitlab.aescorp.ru/dsp_dev/claim/sync_service + $(CD_GENERATION_PROTO) $(GENERATION_PROTO) go mod tidy go mod vendor \ No newline at end of file diff --git a/bin/templates/configs_/convert_id.json b/bin/templates/configs_/convert_id.json index bfd870e..e12763f 100644 --- a/bin/templates/configs_/convert_id.json +++ b/bin/templates/configs_/convert_id.json @@ -1,3 +1,14 @@ { + "lawsuit_payments.id": "alias.PaymentId", + "lawsuit_payments.invoice_id": "alias.InvoiceId", + "lawsuit_payments.lawsuit_id": "alias.LawsuitId", + "lawsuit_payments.id": "alias.PaymentId", + "invoice_debt_type_stats.invoice_id": "alias.InvoiceId", + "invoice_debt_type_stats.lawsuit_id": "alias.LawsuitId", + "contract_black_items.contract_number": "alias.ContractNumber", + "contract_white_items.contract_number": "alias.ContractNumber", + "lawsuits.number_claim": "alias.ClaimNumber", + "lawsuits.number": "alias.LawsuitNumber", + "contracts.number": "alias.ContractNumber" } diff --git a/bin/templates/configs_/crud_functions_rename.json b/bin/templates/configs_/crud_functions_rename.json index 7a73a41..10c97af 100644 --- a/bin/templates/configs_/crud_functions_rename.json +++ b/bin/templates/configs_/crud_functions_rename.json @@ -1,2 +1,8 @@ { + "lawsuits": [ + { + "old": "create_update_ctx", + "new": "create_update_ctx_original" + } + ] } \ No newline at end of file diff --git a/bin/templates/configs_/model_crud_delete_functions.json b/bin/templates/configs_/model_crud_delete_functions.json index 2c63c08..d8c0ee0 100644 --- a/bin/templates/configs_/model_crud_delete_functions.json +++ b/bin/templates/configs_/model_crud_delete_functions.json @@ -1,2 +1,3 @@ { + "lawsuits": "NewLawsuit,AsLawsuit,create_update_ctx" } diff --git a/bin/templates/configs_/name_replace.json b/bin/templates/configs_/name_replace.json index 7a73a41..cab0526 100644 --- a/bin/templates/configs_/name_replace.json +++ b/bin/templates/configs_/name_replace.json @@ -1,2 +1,19 @@ { + "inn": "INN", + "kpp": "KPP", + "okpo": "OKPO", + "ogrn": "OGRN", + "okato": "OKATO", + "nsi_flat": "NSIFlat", + "nsi_flat_id": "NSIFlatID", + "nsi_id": "NSIID", + "www": "WWW", + "bik": "BIK", + "city_name": "City", + "edms_link": "EDMSLink", + "code_nsi": "CodeNSI", + "json_updated_at": "JSONUpdatedAt", + "pdf_updated_at": "PDFUpdatedAt", + "pdf_file_id": "PDFFileID", + "json_file_id": "JSONFileID" } \ No newline at end of file diff --git a/bin/templates/configs_/nullable.json b/bin/templates/configs_/nullable.json index 2c63c08..3f0a502 100644 --- a/bin/templates/configs_/nullable.json +++ b/bin/templates/configs_/nullable.json @@ -1,2 +1,4 @@ { + "ext_id": true, + "created_at": false } diff --git a/bin/templates/configs_/settings.txt b/bin/templates/configs_/settings.txt index 667ff6b..6647da1 100644 --- a/bin/templates/configs_/settings.txt +++ b/bin/templates/configs_/settings.txt @@ -21,15 +21,15 @@ DB_PASSWORD= INCLUDE_TABLES="" #INCLUDE_TABLES - table name ignore filter. Regular expression -EXCLUDE_TABLES= +EXCLUDE_TABLES="ext_" #---------------------Template settings--------------------- #SERVICE_REPOSITORY_URL - URL of your new service repository (Github, gitlab, etc.) -SERVICE_REPOSITORY_URL="gitlab.aescorp.ru/dsp_dev/claim/crud_service" +SERVICE_REPOSITORY_URL="gitlab.aescorp.ru/dsp_dev/claim/sync_service" #SERVICE_NAME - service name for your new service -SERVICE_NAME=crud_service +SERVICE_NAME=sync_service #TEMPLATE_SERVICE_NAME - service name in your template files TEMPLATE_SERVICE_NAME=sync_service @@ -96,7 +96,7 @@ TEXT_TEMPLATE_TABLENAME="lawsuit_status_types" USE_DEFAULT_TEMPLATE=true #HAS_IS_DELETED - fill "true" if you have "is_deleted" column, and want Delete() Restore() functions. -HAS_IS_DELETED=false +HAS_IS_DELETED=true #---------------------Create files settings--------------------- diff --git a/bin/templates/internal/app/grpc/server_grpc/server_grpc_test.go_ b/bin/templates/internal/app/grpc/server_grpc/server_grpc_test.go_ index 5ad483a..3457d7b 100644 --- a/bin/templates/internal/app/grpc/server_grpc/server_grpc_test.go_ +++ b/bin/templates/internal/app/grpc/server_grpc/server_grpc_test.go_ @@ -7,7 +7,6 @@ import ( "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_proto" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "testing" - "math" ) // LawsuitStatusType_ID_Test - ID таблицы для тестирования @@ -38,7 +37,7 @@ func Test_server_LawsuitStatusType_Delete(t *testing.T) { ctx := context.Background() Request := grpc_proto.RequestId{} - Request.Id = math.MinInt64 + Request.Id = LawsuitStatusType_ID_Test Request.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion() server1 := &ServerGRPC{} diff --git a/bin/templates/pkg/crud_starter/starter_tables/starter_tables.go_ b/bin/templates/pkg/crud_starter/starter_tables/starter_tables.go_ new file mode 100644 index 0000000..273603b --- /dev/null +++ b/bin/templates/pkg/crud_starter/starter_tables/starter_tables.go_ @@ -0,0 +1,12 @@ +package crud_starter_lawsuit_status_types + +import ( + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" +) + +// SetCrudInterface - заполняет интерфейс crud: DB, GRPC, NRPC +func SetCrudInterface(crud lawsuit_status_types.ICrud_LawsuitStatusType) { + lawsuit_status_types.Crud_LawsuitStatusType = crud + + return +} diff --git a/bin/templates/pkg/crud_starter/starter_tables/starter_tables_test.go_ b/bin/templates/pkg/crud_starter/starter_tables/starter_tables_test.go_ new file mode 100644 index 0000000..c36e33f --- /dev/null +++ b/bin/templates/pkg/crud_starter/starter_tables/starter_tables_test.go_ @@ -0,0 +1,12 @@ +package starter_crud_starter_lawsuit_status_types + +import ( + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud/crud_lawsuit_status_types" + "testing" +) + +func TestSetCrudInterface(t *testing.T) { + crud := crud_lawsuit_status_types.Crud_DB{} + + SetCrudInterface(crud) +} diff --git a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_test.go_ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_test.go_ index 261d9a4..ad385b8 100644 --- a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_test.go_ +++ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_test.go_ @@ -4,7 +4,6 @@ import ( config "github.com/ManyakRus/starter/config_main" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_client" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" - "math" "testing" ) @@ -111,11 +110,44 @@ func TestDelete(t *testing.T) { crud := Crud_GRPC{} Otvet := lawsuit_status_types.LawsuitStatusType{} - Otvet.ID = math.MinInt64 - - err := crud.Delete(&Otvet) + Otvet.ID = Postgres_ID_Test + err := crud.Read(&Otvet) if err != nil { - t.Error("TestDelete() error: ", err) + t.Error("TestRead() error: ", err) + } + + if Otvet.IsDeleted == false { + err = crud.Delete(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + if Otvet.ID == 0 { + t.Error("TestDelete() error: ID =0") + } + + err = crud.Restore(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + if Otvet.ID == 0 { + t.Error("TestDelete() error: ID =0") + } + } else { + err = crud.Restore(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + if Otvet.ID == 0 { + t.Error("TestDelete() error: ID =0") + } + + err = crud.Delete(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + if Otvet.ID == 0 { + t.Error("TestDelete() error: ID =0") + } } } diff --git a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table_test.go_ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table_test.go_ index 2a096d3..f28599b 100644 --- a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table_test.go_ +++ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table_test.go_ @@ -5,7 +5,6 @@ import ( "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/nrpc/nrpc_client" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "testing" - "math" ) // Postgres_ID_Test - ID таблицы для тестирования @@ -111,10 +110,44 @@ func TestDelete(t *testing.T) { crud := Crud_NRPC{} Otvet := lawsuit_status_types.LawsuitStatusType{} - Otvet.ID = math.MinInt64 - err := crud.Delete(&Otvet) + Otvet.ID = Postgres_ID_Test + err := crud.Read(&Otvet) if err != nil { - t.Error("TestDelete() error: ", err) + t.Error("TestRead() error: ", err) + } + + if Otvet.IsDeleted == false { + err = crud.Delete(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + if Otvet.ID == 0 { + t.Error("TestDelete() error: ID =0") + } + + err = crud.Restore(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + if Otvet.ID == 0 { + t.Error("TestDelete() error: ID =0") + } + } else { + err = crud.Restore(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + if Otvet.ID == 0 { + t.Error("TestDelete() error: ID =0") + } + + err = crud.Delete(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + if Otvet.ID == 0 { + t.Error("TestDelete() error: ID =0") + } } } diff --git a/bin/templates/pkg/object_model/entities/model_crud.go_ b/bin/templates/pkg/object_model/entities/model_crud.go_ index d00a2b5..3e77ba8 100644 --- a/bin/templates/pkg/object_model/entities/model_crud.go_ +++ b/bin/templates/pkg/object_model/entities/model_crud.go_ @@ -11,8 +11,8 @@ import ( // versionLawsuitStatusType - версия структуры модели, с учётом имен и типов полей var versionLawsuitStatusType uint32 -// crud_LawsuitStatusType - объект контроллер crud операций -var crud_LawsuitStatusType ICrud_LawsuitStatusType +// Crud_LawsuitStatusType - объект контроллер crud операций +var Crud_LawsuitStatusType ICrud_LawsuitStatusType // интерфейс стандартных CRUD операций, для использования в DB или GRPC или NRPC type ICrud_LawsuitStatusType interface { @@ -92,84 +92,84 @@ func (m LawsuitStatusType) GetJSON() (string, error) { // Read - находит запись в БД по ID, и заполняет в объект func (m *LawsuitStatusType) Read() error { - if crud_LawsuitStatusType == nil { + if Crud_LawsuitStatusType == nil { return constants.ErrorCrudIsNotInit } - err := crud_LawsuitStatusType.Read(m) + err := Crud_LawsuitStatusType.Read(m) return err } // Save - записывает объект в БД по ID func (m *LawsuitStatusType) Save() error { - if crud_LawsuitStatusType == nil { + if Crud_LawsuitStatusType == nil { return constants.ErrorCrudIsNotInit } - err := crud_LawsuitStatusType.Save(m) + err := Crud_LawsuitStatusType.Save(m) return err } // Update - обновляет объект в БД по ID func (m *LawsuitStatusType) Update() error { - if crud_LawsuitStatusType == nil { + if Crud_LawsuitStatusType == nil { return constants.ErrorCrudIsNotInit } - err := crud_LawsuitStatusType.Update(m) + err := Crud_LawsuitStatusType.Update(m) return err } // Create - создаёт объект в БД с новым ID func (m *LawsuitStatusType) Create() error { - if crud_LawsuitStatusType == nil { + if Crud_LawsuitStatusType == nil { return constants.ErrorCrudIsNotInit } - err := crud_LawsuitStatusType.Create(m) + err := Crud_LawsuitStatusType.Create(m) return err } // Delete - устанавливает признак пометки удаления в БД func (m *LawsuitStatusType) Delete() error { - if crud_LawsuitStatusType == nil { + if Crud_LawsuitStatusType == nil { return constants.ErrorCrudIsNotInit } - err := crud_LawsuitStatusType.Delete(m) + err := Crud_LawsuitStatusType.Delete(m) return err } // Restore - снимает признак пометки удаления в БД func (m *LawsuitStatusType) Restore() error { - if crud_LawsuitStatusType == nil { + if Crud_LawsuitStatusType == nil { return constants.ErrorCrudIsNotInit } - err := crud_LawsuitStatusType.Restore(m) + err := Crud_LawsuitStatusType.Restore(m) return err } // Find_ByExtID - находит объект по ExtID func (m *LawsuitStatusType) Find_ByExtID() error { - if crud_LawsuitStatusType == nil { + if Crud_LawsuitStatusType == nil { return constants.ErrorCrudIsNotInit } - err := crud_LawsuitStatusType.Find_ByExtID(m) + err := Crud_LawsuitStatusType.Find_ByExtID(m) return err } // SetCrudInterface - заполняет интерфейс crud: DB, GRPC, NRPC func (m LawsuitStatusType) SetCrudInterface(crud ICrud_LawsuitStatusType) { - crud_LawsuitStatusType = crud + Crud_LawsuitStatusType = crud return } diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 047b15c..6fc4b7c 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -35,3 +35,7 @@ const SERVER_GRPC_FUNC_FILENAME = "server_grpc_func.go" const MAKEFILE_FILENAME = "Makefile" const ENV_FILENAME = ".env" + +const STARTER_TABLES_FILENAME = "starter_tables.go_" +const STARTER_TABLES_FILENAME_TEST = "starter_tables_test.go_" +const STARTER_TABLES_PREFIX = "crud_starter_" diff --git a/internal/create_files/crud_starter/crud_starter.go b/internal/create_files/crud_starter/crud_starter.go index e1742c4..4aba839 100644 --- a/internal/create_files/crud_starter/crud_starter.go +++ b/internal/create_files/crud_starter/crud_starter.go @@ -98,6 +98,7 @@ import ( sort.Strings(keys) //все таблицы + TextStarter := "" TextModel := "" TextDB := "" TextGRPC := "" @@ -115,19 +116,29 @@ import ( continue } - TextModel = TextModel + FindTextImportModel1(Table1) + TextStarter = TextStarter + FindTextImportStarter1(Table1) + //TextModel = TextModel + FindTextImportModel1(Table1) TextDB = TextDB + FindTextImportDB1(Table1) TextGRPC = TextGRPC + FindTextImportGRPC1(Table1) TextNRPC = TextNRPC + FindTextImportNRPC1(Table1) } - Otvet = Otvet + TextModel + "\n" + TextDB + "\n" + TextGRPC + "\n" + TextNRPC + Otvet = Otvet + TextStarter + "\n" + TextModel + "\n" + TextDB + "\n" + TextGRPC + "\n" + TextNRPC Otvet = Otvet + "\n)" return Otvet } +// FindTextImportStarter1 - возвращает текст импорта crud_starter_ для 1 таблицы +func FindTextImportStarter1(Table1 *types.Table) string { + TableName := strings.ToLower(Table1.Name) + DB_URL := config.Settings.SERVICE_REPOSITORY_URL + "/" + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + "/" + constants.STARTER_TABLES_PREFIX + TableName + Otvet := "\n\t\"" + DB_URL + `"` + + return Otvet +} + // FindTextImportModel1 - возвращает текст импорта Model для 1 таблицы func FindTextImportModel1(Table1 *types.Table) string { TableName := strings.ToLower(Table1.Name) @@ -206,8 +217,7 @@ func InitCrudTransport_DB() {` // FindTextNRPC1 - возвращает текст всех функций .proto для таблицы func FindTextDB1(Table1 *types.Table) string { TableName := strings.ToLower(Table1.Name) - ModelName := Table1.NameGo - Otvet := "\n\t" + TableName + "." + ModelName + "{}.SetCrudInterface(" + config.Settings.PREFIX_CRUD + TableName + ".Crud_DB{})" + Otvet := "\n\t" + constants.STARTER_TABLES_PREFIX + TableName + ".SetCrudInterface(" + config.Settings.PREFIX_CRUD + TableName + ".Crud_DB{})" return Otvet } @@ -255,8 +265,7 @@ func InitCrudTransport_GRPC() {` // FindTextNRPC1 - возвращает текст всех функций .proto для таблицы func FindTextGRPC1(Table1 *types.Table) string { TableName := strings.ToLower(Table1.Name) - ModelName := Table1.NameGo - Otvet := "\n\t" + TableName + "." + ModelName + "{}.SetCrudInterface(grpc_" + TableName + ".Crud_GRPC{})" + Otvet := "\n\t" + constants.STARTER_TABLES_PREFIX + TableName + ".SetCrudInterface(grpc_" + TableName + ".Crud_GRPC{})" return Otvet } @@ -303,8 +312,7 @@ func InitCrudTransport_NRPC() {` // FindTextNRPC1 - возвращает текст всех функций .proto для таблицы func FindTextNRPC1(Table1 *types.Table) string { TableName := strings.ToLower(Table1.Name) - ModelName := Table1.NameGo - Otvet := "\n\t" + TableName + "." + ModelName + "{}.SetCrudInterface(nrpc_" + TableName + ".Crud_NRPC{})" + Otvet := "\n\t" + constants.STARTER_TABLES_PREFIX + TableName + ".SetCrudInterface(nrpc_" + TableName + ".Crud_NRPC{})" return Otvet } diff --git a/internal/create_files/crud_starter_tables/crud_starter_tables.go b/internal/create_files/crud_starter_tables/crud_starter_tables.go new file mode 100644 index 0000000..f6b583d --- /dev/null +++ b/internal/create_files/crud_starter_tables/crud_starter_tables.go @@ -0,0 +1,163 @@ +package crud_starter_tables + +import ( + "github.com/ManyakRus/crud_generator/internal/config" + "github.com/ManyakRus/crud_generator/internal/constants" + "github.com/ManyakRus/crud_generator/internal/create_files" + "github.com/ManyakRus/crud_generator/internal/folders" + "github.com/ManyakRus/crud_generator/internal/types" + "github.com/ManyakRus/starter/log" + "github.com/ManyakRus/starter/micro" + "os" + "strings" +) + +// CreateAllFiles - создаёт все файлы в папке db +func CreateAllFiles(MapAll map[string]*types.Table) error { + var err error + + for _, Table1 := range MapAll { + //проверка что таблица нормальная + err1 := create_files.CheckGoodTable(Table1) + if err1 != nil { + log.Warn(err1) + continue + } + + //файлы starter + if config.Settings.NEED_CREATE_DB == true { + err = CreateFiles(Table1) + if err != nil { + log.Error("CreateFiles() table: ", Table1.Name, " error: ", err) + return err + } + } + //тестовые файлы starter + if config.Settings.NEED_CREATE_DB_TEST == true { + err = CreateTestFiles(Table1) + if err != nil { + log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) + return err + } + } + } + + return err +} + +// CreateFiles - создаёт 1 файл в папке db +func CreateFiles(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() + DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + "starter_tables" + micro.SeparatorFile() + DirReadyDB := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + + FilenameTemplateDB := DirTemplatesDB + constants.STARTER_TABLES_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyDB + constants.STARTER_TABLES_PREFIX + TableName + FilenameReadyDB := DirReadyTable + micro.SeparatorFile() + constants.STARTER_TABLES_PREFIX + TableName + ".go" + + //создадим каталог + ok, err := micro.FileExists(DirReadyTable) + if ok == false { + err = os.MkdirAll(DirReadyTable, 0777) + if err != nil { + log.Panic("Mkdir() ", DirReadyTable, " error: ", err) + } + } + + bytes, err := os.ReadFile(FilenameTemplateDB) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateDB, " error: ", err) + } + TextDB := string(bytes) + + //заменим имя пакета на новое + TextDB = create_files.ReplacePackageName(TextDB, DirReadyTable) + + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextDB = create_files.DeleteTemplateRepositoryImports(TextDB) + + ModelTableURL := create_files.FindModelTableURL(TableName) + TextDB = create_files.AddImport(TextDB, ModelTableURL) + + } + + //создание текста + ModelName := Table1.NameGo + TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + TextDB = config.Settings.TEXT_MODULE_GENERATED + TextDB + + //замена импортов на новые URL + TextDB = create_files.ReplaceServiceURLImports(TextDB) + + //удаление пустого импорта + TextDB = create_files.DeleteEmptyImport(TextDB) + + //запись файла + err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS) + + return err +} + +// CreateTestFiles - создаёт 1 файл в папке db +func CreateTestFiles(Table1 *types.Table) error { + var err error + + TableName := strings.ToLower(Table1.Name) + + //чтение файлов + DirBin := micro.ProgramDir_bin() + DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile() + DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + "starter_tables" + micro.SeparatorFile() + DirReadyDB := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + + FilenameTemplateDB := DirTemplatesDB + constants.STARTER_TABLES_FILENAME_TEST + DirReadyTable := DirReadyDB + constants.STARTER_TABLES_PREFIX + TableName + FilenameReadyDB := DirReadyTable + micro.SeparatorFile() + constants.STARTER_TABLES_PREFIX + TableName + "_test.go" + + //создадим папку готовых файлов + folders.CreateFolder(DirReadyTable) + + bytes, err := os.ReadFile(FilenameTemplateDB) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateDB, " error: ", err) + } + TextDB := string(bytes) + + //заменим имя пакета на новое + TextDB = create_files.ReplacePackageName(TextDB, DirReadyTable) + + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextDB = create_files.DeleteTemplateRepositoryImports(TextDB) + + ModelTableURL := create_files.FindModelTableURL(TableName) + TextDB = create_files.AddImport(TextDB, ModelTableURL) + + } + + //создание текста + ModelName := Table1.NameGo + TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + TextDB = config.Settings.TEXT_MODULE_GENERATED + TextDB + + //замена импортов на новые URL + TextDB = create_files.ReplaceServiceURLImports(TextDB) + + //удаление пустого импорта + TextDB = create_files.DeleteEmptyImport(TextDB) + + //запись файла + err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS) + + return err +} diff --git a/internal/create_files/crud_starter_tables/crud_starter_tables_test.go b/internal/create_files/crud_starter_tables/crud_starter_tables_test.go new file mode 100644 index 0000000..6ca8971 --- /dev/null +++ b/internal/create_files/crud_starter_tables/crud_starter_tables_test.go @@ -0,0 +1 @@ +package crud_starter_tables diff --git a/internal/logic/logic.go b/internal/logic/logic.go index 6f83208..b620ac8 100644 --- a/internal/logic/logic.go +++ b/internal/logic/logic.go @@ -3,6 +3,7 @@ package logic import ( "github.com/ManyakRus/crud_generator/internal/config" "github.com/ManyakRus/crud_generator/internal/create_files/crud_starter" + "github.com/ManyakRus/crud_generator/internal/create_files/crud_starter_tables" "github.com/ManyakRus/crud_generator/internal/create_files/db_crud_tables" "github.com/ManyakRus/crud_generator/internal/create_files/db_tables" "github.com/ManyakRus/crud_generator/internal/create_files/env_file" @@ -162,6 +163,13 @@ func StartFillAll() error { return err } + //tables + err = crud_starter_tables.CreateAllFiles(MapAll) + if err != nil { + //log.Error("db_tables.CreateAllFiles() error: ", err) + return err + } + //env err = env_file.CreateAllFiles() if err != nil {