From 48bf9dc63439a108e817a811da1220792e9a908e Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Tue, 26 Dec 2023 09:32:38 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20Text=5Fc?= =?UTF-8?q?reated=5Fat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/pkg/db/crud/crud.go_ | 56 +++++------------------------- 1 file changed, 8 insertions(+), 48 deletions(-) diff --git a/bin/templates/pkg/db/crud/crud.go_ b/bin/templates/pkg/db/crud/crud.go_ index 303b13e..73bda68 100644 --- a/bin/templates/pkg/db/crud/crud.go_ +++ b/bin/templates/pkg/db/crud/crud.go_ @@ -146,12 +146,14 @@ 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 } @@ -208,7 +210,7 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error { return err } -// Delete_ctx - записывает is_deleted = true +// Delete_ctx - удаляет запись в БД func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error @@ -217,53 +219,11 @@ func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.Laws return err } - m2 := lawsuit_status_types.LawsuitStatusType{} - m2.ID = m.ID - err = crud.Read_ctx(ctx, &m2) - if err != nil { - return err - } + db := postgres_gorm.GetConnection() + db.WithContext(ctx) - 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) + tx := db.Delete(m) + err = tx.Error return err } @@ -304,7 +264,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).First(m) + tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).Order("is_deleted").Order("id").First(m) err = tx.Error return err