From f6e7c602d1197f78c616b19607bda88adec24089 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Mon, 25 Dec 2023 17:15:24 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20CreatedA?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/pkg/db/crud/created_at.go_ | 5 ++ bin/templates/pkg/db/crud/crud.go_ | 57 ++++++++++++++++--- bin/templates/pkg/db/crud/crud_test.go_ | 28 ++++++++- bin/templates/pkg/db/crud/is_deleted.go_ | 2 +- bin/templates/pkg/db/crud/modified_at.go_ | 2 +- .../templates/configs_/convert_id.json | 11 ---- .../configs_/model_crud_delete_functions.json | 1 - .../templates/configs_/name_replace.json | 17 ------ .../default/templates/configs_/nullable.json | 2 - .../templates/pkg/db/crud/created_at.go_ | 5 ++ .../default/templates/pkg/db/crud/crud.go_ | 57 ++++++++++++++++--- .../templates/pkg/db/crud/crud_test.go_ | 28 ++++++++- .../templates/pkg/db/crud/is_deleted.go_ | 2 +- .../templates/pkg/db/crud/modified_at.go_ | 2 +- .../templates/pkg/db/crud/created_at.go_ | 5 ++ .../rapira/templates/pkg/db/crud/crud.go_ | 3 +- .../templates/pkg/db/crud/is_deleted.go_ | 2 +- .../templates/pkg/db/crud/modified_at.go_ | 2 +- internal/config/config.go | 1 + .../db_crud_tables/db_crud_tables.go | 21 ++++++- internal/load_configs/load_configs.go | 19 +++++++ 21 files changed, 209 insertions(+), 63 deletions(-) create mode 100644 bin/templates/pkg/db/crud/created_at.go_ create mode 100644 examples/default/templates/pkg/db/crud/created_at.go_ create mode 100644 examples/rapira/templates/pkg/db/crud/created_at.go_ diff --git a/bin/templates/pkg/db/crud/created_at.go_ b/bin/templates/pkg/db/crud/created_at.go_ new file mode 100644 index 0000000..dbc8a0c --- /dev/null +++ b/bin/templates/pkg/db/crud/created_at.go_ @@ -0,0 +1,5 @@ + + if m.CreatedAt.IsZero() == true || m.ID == 0 { + m.CreatedAt = time.Now() + } + diff --git a/bin/templates/pkg/db/crud/crud.go_ b/bin/templates/pkg/db/crud/crud.go_ index f936783..303b13e 100644 --- a/bin/templates/pkg/db/crud/crud.go_ +++ b/bin/templates/pkg/db/crud/crud.go_ @@ -146,14 +146,12 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ if is_create == true { if int64(m.ID) != 0 { TextError := fmt.Sprint("db.Save() ", TableName, " error: id !=0") - // log.Panic(sError) err = errors.New(TextError) return err } } else if int64(m.ID) == 0 { TextError := fmt.Sprint("db.Save() ", TableName, " error: id =0") err = errors.New(TextError) - // log.Panic(sError) return err } @@ -164,6 +162,7 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ // заполним даты //Text_modified_at //Text_is_deleted_deleted_at + //Text_created_at //колонки с null tx := db @@ -209,7 +208,7 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error { return err } -// Delete_ctx - удаляет запись в БД +// Delete_ctx - записывает is_deleted = true func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error @@ -218,11 +217,53 @@ func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.Laws return err } - db := postgres_gorm.GetConnection() - db.WithContext(ctx) + m2 := lawsuit_status_types.LawsuitStatusType{} + m2.ID = m.ID + err = crud.Read_ctx(ctx, &m2) + if err != nil { + return err + } - tx := db.Delete(m) - err = tx.Error + m2.IsDeleted = true + m.IsDeleted = true + + err = crud.Save_ctx(ctx, &m2) + + return err +} + +// Restore - записывает is_deleted = true +func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error { + var err error + + ctxMain := contextmain.GetContext() + ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) + defer ctxCancelFunc() + + err = crud.Restore_ctx(ctx, m) + return err +} + +// Restore_ctx - записывает is_deleted = true +func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { + var err error + + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + + m2 := lawsuit_status_types.LawsuitStatusType{} + m2.ID = m.ID + err = crud.Read_ctx(ctx, &m2) + if err != nil { + return err + } + + m2.IsDeleted = false + m.IsDeleted = false + + err = crud.Save_ctx(ctx, &m2) return err } @@ -263,7 +304,7 @@ func (crud Crud_DB) Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_type db := postgres_gorm.GetConnection() db.WithContext(ctx) - tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).Order("is_deleted").Order("id").First(m) + tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).First(m) err = tx.Error return err diff --git a/bin/templates/pkg/db/crud/crud_test.go_ b/bin/templates/pkg/db/crud/crud_test.go_ index eb32103..875143c 100644 --- a/bin/templates/pkg/db/crud/crud_test.go_ +++ b/bin/templates/pkg/db/crud/crud_test.go_ @@ -5,7 +5,6 @@ import ( "github.com/ManyakRus/starter/postgres_gorm" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "testing" - "math" ) const Postgres_ID_Test = 1 @@ -65,12 +64,35 @@ func TestDelete(t *testing.T) { crud := Crud_DB{} 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) } + if Otvet.IsDeleted == false { + err = crud.Delete(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + + err = crud.Restore(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + } else { + err = crud.Restore(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + + err = crud.Delete(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + + } + } diff --git a/bin/templates/pkg/db/crud/is_deleted.go_ b/bin/templates/pkg/db/crud/is_deleted.go_ index adc19f4..648c4da 100644 --- a/bin/templates/pkg/db/crud/is_deleted.go_ +++ b/bin/templates/pkg/db/crud/is_deleted.go_ @@ -1,4 +1,4 @@ -if m.IsDeleted == true && m.DeletedAt.IsZero() == true { + if m.IsDeleted == true && m.DeletedAt.IsZero() == true { m.DeletedAt = time.Now() } else if m.IsDeleted == false && m.DeletedAt.IsZero() == false { m.DeletedAt = time.Time{} diff --git a/bin/templates/pkg/db/crud/modified_at.go_ b/bin/templates/pkg/db/crud/modified_at.go_ index f648e98..7163e90 100644 --- a/bin/templates/pkg/db/crud/modified_at.go_ +++ b/bin/templates/pkg/db/crud/modified_at.go_ @@ -1 +1 @@ -m.ModifiedAt = time.Now() \ No newline at end of file + m.ModifiedAt = time.Now() diff --git a/examples/default/templates/configs_/convert_id.json b/examples/default/templates/configs_/convert_id.json index e12763f..bfd870e 100644 --- a/examples/default/templates/configs_/convert_id.json +++ b/examples/default/templates/configs_/convert_id.json @@ -1,14 +1,3 @@ { - "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/examples/default/templates/configs_/model_crud_delete_functions.json b/examples/default/templates/configs_/model_crud_delete_functions.json index 6a8d05d..2c63c08 100644 --- a/examples/default/templates/configs_/model_crud_delete_functions.json +++ b/examples/default/templates/configs_/model_crud_delete_functions.json @@ -1,3 +1,2 @@ { - "lawsuits": "NewLawsuit,AsLawsuit" } diff --git a/examples/default/templates/configs_/name_replace.json b/examples/default/templates/configs_/name_replace.json index cab0526..7a73a41 100644 --- a/examples/default/templates/configs_/name_replace.json +++ b/examples/default/templates/configs_/name_replace.json @@ -1,19 +1,2 @@ { - "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/examples/default/templates/configs_/nullable.json b/examples/default/templates/configs_/nullable.json index 3f0a502..2c63c08 100644 --- a/examples/default/templates/configs_/nullable.json +++ b/examples/default/templates/configs_/nullable.json @@ -1,4 +1,2 @@ { - "ext_id": true, - "created_at": false } diff --git a/examples/default/templates/pkg/db/crud/created_at.go_ b/examples/default/templates/pkg/db/crud/created_at.go_ new file mode 100644 index 0000000..dbc8a0c --- /dev/null +++ b/examples/default/templates/pkg/db/crud/created_at.go_ @@ -0,0 +1,5 @@ + + if m.CreatedAt.IsZero() == true || m.ID == 0 { + m.CreatedAt = time.Now() + } + diff --git a/examples/default/templates/pkg/db/crud/crud.go_ b/examples/default/templates/pkg/db/crud/crud.go_ index f936783..303b13e 100644 --- a/examples/default/templates/pkg/db/crud/crud.go_ +++ b/examples/default/templates/pkg/db/crud/crud.go_ @@ -146,14 +146,12 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ if is_create == true { if int64(m.ID) != 0 { TextError := fmt.Sprint("db.Save() ", TableName, " error: id !=0") - // log.Panic(sError) err = errors.New(TextError) return err } } else if int64(m.ID) == 0 { TextError := fmt.Sprint("db.Save() ", TableName, " error: id =0") err = errors.New(TextError) - // log.Panic(sError) return err } @@ -164,6 +162,7 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ // заполним даты //Text_modified_at //Text_is_deleted_deleted_at + //Text_created_at //колонки с null tx := db @@ -209,7 +208,7 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error { return err } -// Delete_ctx - удаляет запись в БД +// Delete_ctx - записывает is_deleted = true func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error @@ -218,11 +217,53 @@ func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.Laws return err } - db := postgres_gorm.GetConnection() - db.WithContext(ctx) + m2 := lawsuit_status_types.LawsuitStatusType{} + m2.ID = m.ID + err = crud.Read_ctx(ctx, &m2) + if err != nil { + return err + } - tx := db.Delete(m) - err = tx.Error + m2.IsDeleted = true + m.IsDeleted = true + + err = crud.Save_ctx(ctx, &m2) + + return err +} + +// Restore - записывает is_deleted = true +func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error { + var err error + + ctxMain := contextmain.GetContext() + ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) + defer ctxCancelFunc() + + err = crud.Restore_ctx(ctx, m) + return err +} + +// Restore_ctx - записывает is_deleted = true +func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { + var err error + + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + + m2 := lawsuit_status_types.LawsuitStatusType{} + m2.ID = m.ID + err = crud.Read_ctx(ctx, &m2) + if err != nil { + return err + } + + m2.IsDeleted = false + m.IsDeleted = false + + err = crud.Save_ctx(ctx, &m2) return err } @@ -263,7 +304,7 @@ func (crud Crud_DB) Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_type db := postgres_gorm.GetConnection() db.WithContext(ctx) - tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).Order("is_deleted").Order("id").First(m) + tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).First(m) err = tx.Error return err diff --git a/examples/default/templates/pkg/db/crud/crud_test.go_ b/examples/default/templates/pkg/db/crud/crud_test.go_ index eb32103..875143c 100644 --- a/examples/default/templates/pkg/db/crud/crud_test.go_ +++ b/examples/default/templates/pkg/db/crud/crud_test.go_ @@ -5,7 +5,6 @@ import ( "github.com/ManyakRus/starter/postgres_gorm" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "testing" - "math" ) const Postgres_ID_Test = 1 @@ -65,12 +64,35 @@ func TestDelete(t *testing.T) { crud := Crud_DB{} 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) } + if Otvet.IsDeleted == false { + err = crud.Delete(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + + err = crud.Restore(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + } else { + err = crud.Restore(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + + err = crud.Delete(&Otvet) + if err != nil { + t.Error("TestDelete() error: ", err) + } + + } + } diff --git a/examples/default/templates/pkg/db/crud/is_deleted.go_ b/examples/default/templates/pkg/db/crud/is_deleted.go_ index adc19f4..648c4da 100644 --- a/examples/default/templates/pkg/db/crud/is_deleted.go_ +++ b/examples/default/templates/pkg/db/crud/is_deleted.go_ @@ -1,4 +1,4 @@ -if m.IsDeleted == true && m.DeletedAt.IsZero() == true { + if m.IsDeleted == true && m.DeletedAt.IsZero() == true { m.DeletedAt = time.Now() } else if m.IsDeleted == false && m.DeletedAt.IsZero() == false { m.DeletedAt = time.Time{} diff --git a/examples/default/templates/pkg/db/crud/modified_at.go_ b/examples/default/templates/pkg/db/crud/modified_at.go_ index f648e98..7163e90 100644 --- a/examples/default/templates/pkg/db/crud/modified_at.go_ +++ b/examples/default/templates/pkg/db/crud/modified_at.go_ @@ -1 +1 @@ -m.ModifiedAt = time.Now() \ No newline at end of file + m.ModifiedAt = time.Now() diff --git a/examples/rapira/templates/pkg/db/crud/created_at.go_ b/examples/rapira/templates/pkg/db/crud/created_at.go_ new file mode 100644 index 0000000..dbc8a0c --- /dev/null +++ b/examples/rapira/templates/pkg/db/crud/created_at.go_ @@ -0,0 +1,5 @@ + + if m.CreatedAt.IsZero() == true || m.ID == 0 { + m.CreatedAt = time.Now() + } + diff --git a/examples/rapira/templates/pkg/db/crud/crud.go_ b/examples/rapira/templates/pkg/db/crud/crud.go_ index c4fe2cd..303b13e 100644 --- a/examples/rapira/templates/pkg/db/crud/crud.go_ +++ b/examples/rapira/templates/pkg/db/crud/crud.go_ @@ -146,14 +146,12 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ if is_create == true { if int64(m.ID) != 0 { TextError := fmt.Sprint("db.Save() ", TableName, " error: id !=0") - // log.Panic(sError) err = errors.New(TextError) return err } } else if int64(m.ID) == 0 { TextError := fmt.Sprint("db.Save() ", TableName, " error: id =0") err = errors.New(TextError) - // log.Panic(sError) return err } @@ -164,6 +162,7 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ // заполним даты //Text_modified_at //Text_is_deleted_deleted_at + //Text_created_at //колонки с null tx := db diff --git a/examples/rapira/templates/pkg/db/crud/is_deleted.go_ b/examples/rapira/templates/pkg/db/crud/is_deleted.go_ index adc19f4..648c4da 100644 --- a/examples/rapira/templates/pkg/db/crud/is_deleted.go_ +++ b/examples/rapira/templates/pkg/db/crud/is_deleted.go_ @@ -1,4 +1,4 @@ -if m.IsDeleted == true && m.DeletedAt.IsZero() == true { + if m.IsDeleted == true && m.DeletedAt.IsZero() == true { m.DeletedAt = time.Now() } else if m.IsDeleted == false && m.DeletedAt.IsZero() == false { m.DeletedAt = time.Time{} diff --git a/examples/rapira/templates/pkg/db/crud/modified_at.go_ b/examples/rapira/templates/pkg/db/crud/modified_at.go_ index f648e98..7163e90 100644 --- a/examples/rapira/templates/pkg/db/crud/modified_at.go_ +++ b/examples/rapira/templates/pkg/db/crud/modified_at.go_ @@ -1 +1 @@ -m.ModifiedAt = time.Now() \ No newline at end of file + m.ModifiedAt = time.Now() diff --git a/internal/config/config.go b/internal/config/config.go index 887de8a..5c214b9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -49,6 +49,7 @@ type SettingsINI struct { HAS_IS_DELETED bool READY_FOLDERNAME string TEXT_DB_MODIFIED_AT string + TEXT_DB_CREATED_AT string TEXT_DB_IS_DELETED string USE_DEFAULT_TEMPLATE bool PREFIX_SERVER_GRPC string diff --git a/internal/create_files/db_crud_tables/db_crud_tables.go b/internal/create_files/db_crud_tables/db_crud_tables.go index 52ecd06..dba3fc2 100644 --- a/internal/create_files/db_crud_tables/db_crud_tables.go +++ b/internal/create_files/db_crud_tables/db_crud_tables.go @@ -114,6 +114,7 @@ func CreateFiles(Table1 *types.Table) error { //TextDB = create_files.DeleteFuncFind_byExtIDCtx(TextDB, Table1) TextDB = AddTextOmit(TextDB, Table1) TextDB = ReplaceText_modified_at(TextDB, Table1) + TextDB = ReplaceText_created_at(TextDB, Table1) TextDB = ReplaceText_is_deleted_deleted_at(TextDB, Table1) TextDB = create_files.DeleteImportModel(TextDB) @@ -374,7 +375,7 @@ func ReplaceText_modified_at(s string, Table1 *types.Table) string { TextNew = "" } - TextFind := "//Text_modified_at" + TextFind := "\t//Text_modified_at\n" Otvet = strings.ReplaceAll(Otvet, TextFind, TextNew) return Otvet @@ -395,7 +396,23 @@ func ReplaceText_is_deleted_deleted_at(s string, Table1 *types.Table) string { TextNew = "" } - TextFind := "//Text_is_deleted_deleted_at" + TextFind := "\t//Text_is_deleted_deleted_at\n" + Otvet = strings.ReplaceAll(Otvet, TextFind, TextNew) + + return Otvet +} + +// ReplaceText_created_at - заменяет текст "Text_created_at" на текст из файла +func ReplaceText_created_at(s string, Table1 *types.Table) string { + Otvet := s + + TextNew := config.Settings.TEXT_DB_CREATED_AT + _, ok := Table1.MapColumns["created_at"] + if ok == false { + TextNew = "" + } + + TextFind := "\t//Text_created_at\n" Otvet = strings.ReplaceAll(Otvet, TextFind, TextNew) return Otvet diff --git a/internal/load_configs/load_configs.go b/internal/load_configs/load_configs.go index f69b427..0f86932 100644 --- a/internal/load_configs/load_configs.go +++ b/internal/load_configs/load_configs.go @@ -18,6 +18,7 @@ func LoadConfigsAll() { LoadNameReplace() LoadNullable() Load_TEXT_DB_MODIFIED_AT() + Load_TEXT_DB_CREATED_AT() Load_TEXT_DB_IS_DELETED() LoadConvertID() LoadMapModelCrudDeleteFunctions() @@ -160,3 +161,21 @@ func LoadMapModelCrudDeleteFunctions() { } } + +// Load_TEXT_DB_CREATED_AT - загружает текст created_at.go_ +func Load_TEXT_DB_CREATED_AT() { + DirTemplatesDB := create_files.Find_Template_DB_Foldername() + FileName := DirTemplatesDB + "created_at.go_" + + var err error + + //чтение файла + bytes, err := os.ReadFile(FileName) + if err != nil { + TextError := fmt.Sprint("ReadFile() error: ", err) + log.Error(TextError) + } + + config.Settings.TEXT_DB_CREATED_AT = string(bytes) + +}