1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-12-22 00:36:41 +02:00

сделал CreatedAt

This commit is contained in:
Nikitin Aleksandr 2023-12-25 17:15:24 +03:00
parent fa53e2301d
commit f6e7c602d1
21 changed files with 209 additions and 63 deletions

View File

@ -0,0 +1,5 @@
if m.CreatedAt.IsZero() == true || m.ID == 0 {
m.CreatedAt = time.Now()
}

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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{}

View File

@ -1 +1 @@
m.ModifiedAt = time.Now()
m.ModifiedAt = time.Now()

View File

@ -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"
}

View File

@ -1,3 +1,2 @@
{
"lawsuits": "NewLawsuit,AsLawsuit"
}

View File

@ -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"
}

View File

@ -1,4 +1,2 @@
{
"ext_id": true,
"created_at": false
}

View File

@ -0,0 +1,5 @@
if m.CreatedAt.IsZero() == true || m.ID == 0 {
m.CreatedAt = time.Now()
}

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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{}

View File

@ -1 +1 @@
m.ModifiedAt = time.Now()
m.ModifiedAt = time.Now()

View File

@ -0,0 +1,5 @@
if m.CreatedAt.IsZero() == true || m.ID == 0 {
m.CreatedAt = time.Now()
}

View File

@ -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

View File

@ -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{}

View File

@ -1 +1 @@
m.ModifiedAt = time.Now()
m.ModifiedAt = time.Now()

View File

@ -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

View File

@ -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

View File

@ -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)
}