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

сделал api

This commit is contained in:
Nikitin Aleksandr 2023-11-29 15:06:50 +03:00
parent 66e2fd8b33
commit 634d316da3
25 changed files with 137 additions and 53 deletions

View File

@ -1,21 +1,21 @@
#---------------------Database settings--------------------- #---------------------Database settings---------------------
#DB_HOST - Database server name or ip-address. Only Postgres SQL #DB_HOST - Database server name or ip-address. Only Postgres SQL
DB_HOST="" DB_HOST=
#DB_NAME - Database table name #DB_NAME - Database table name
DB_NAME="" DB_NAME=
#DB_SCHEME - Database schema name #DB_SCHEME - Database schema name
DB_SCHEME="public" DB_SCHEME=
#DB_PORT - Database port number #DB_PORT - Database port number
DB_PORT="5432" DB_PORT=
#DB_USER - Database login (user) #DB_USER - Database login (user)
DB_USER="" DB_USER=
#DB_PASSWORD - Database login password #DB_PASSWORD - Database login password
DB_PASSWORD="" DB_PASSWORD=
#INCLUDE_TABLES - table name filter. Regular expression #INCLUDE_TABLES - table name filter. Regular expression
INCLUDE_TABLES="" INCLUDE_TABLES=""
@ -53,24 +53,23 @@ TEMPLATE_FOLDERNAME_CRUD="pkg/db/crud"
#TEMPLATE_FOLDERNAME_TABLES - folder name for create database tables models #TEMPLATE_FOLDERNAME_TABLES - folder name for create database tables models
TEMPLATE_FOLDERNAME_TABLES="pkg/db/tables" TEMPLATE_FOLDERNAME_TABLES="pkg/db/tables"
#TEMPLATE_FOLDERNAME_DB - folder name for create db constants #TEMPLATE_FOLDERNAME_DB - folder name for create db
TEMPLATE_FOLDERNAME_DB="pkg/db" TEMPLATE_FOLDERNAME_DB="pkg/db"
#TEMPLATE_FOLDERNAME_GRPC - folder name for create .proto file #TEMPLATE_FOLDERNAME_GRPC - folder name for create .proto file
TEMPLATE_FOLDERNAME_GRPC_PROTO="pkg/network/grpc" TEMPLATE_FOLDERNAME_GRPC_PROTO="api"
#TEMPLATE_FOLDERNAME_GRPC - folder name for grpc #TEMPLATE_FOLDERNAME_GRPC - folder name for grpc
TEMPLATE_FOLDERNAME_GRPC="pkg/network/grpc" TEMPLATE_FOLDERNAME_GRPC="pkg/network/grpc"
#TEMPLATE_FOLDERNAME_GRPC_SERVER - filder name for create grpc server files #TEMPLATE_FOLDERNAME_GRPC_SERVER - filder name for create grpc server files
TEMPLATE_FOLDERNAME_GRPC_SERVER="internal/server_grpc" TEMPLATE_FOLDERNAME_GRPC_SERVER="internal/app/grpc/server_grpc"
#TEMPLATE_FOLDERNAME_GRPC_CLIENT - folder name for create grpc client files #TEMPLATE_FOLDERNAME_GRPC_CLIENT - folder name for create grpc client files
TEMPLATE_FOLDERNAME_GRPC_CLIENT="pkg/network/grpc/grpc_client" TEMPLATE_FOLDERNAME_GRPC_CLIENT="pkg/network/grpc/grpc_client"
#TEMPLATE_FOLDERNAME_NRPC_SERVER - folder name for create nrpc server files #TEMPLATE_FOLDERNAME_NRPC_SERVER - folder name for create nrpc server files
TEMPLATE_FOLDERNAME_NRPC_SERVER="internal/server_nrpc" TEMPLATE_FOLDERNAME_NRPC_SERVER="internal/app/nrpc/server_nrpc"
#TEMPLATE_FOLDERNAME_NRPC - folder name for create common nrpc files #TEMPLATE_FOLDERNAME_NRPC - folder name for create common nrpc files
TEMPLATE_FOLDERNAME_NRPC="pkg/network/nrpc" TEMPLATE_FOLDERNAME_NRPC="pkg/network/nrpc"
@ -84,6 +83,9 @@ TEMPLATE_FOLDERNAME_CRUD_STARTER="pkg/crud_starter"
#TEMPLATE_FOLDERNAME_ALIAS - folder name for create alias.go file, with go types with aliases #TEMPLATE_FOLDERNAME_ALIAS - folder name for create alias.go file, with go types with aliases
TEMPLATE_FOLDERNAME_ALIAS="pkg/object_model/types/alias" TEMPLATE_FOLDERNAME_ALIAS="pkg/object_model/types/alias"
#TEMPLATE_FOLDER_CRUD_FUNCTIONS - folder name for create crud_functions.go file
TEMPLATE_FOLDER_CRUD_FUNCTIONS="pkg/db/crud_functions"
#TEXT_TEMPLATE_MODEL - model name text in templates, for replace to new model names #TEXT_TEMPLATE_MODEL - model name text in templates, for replace to new model names
TEXT_TEMPLATE_MODEL="LawsuitStatusType" TEXT_TEMPLATE_MODEL="LawsuitStatusType"

View File

@ -5,7 +5,7 @@ FILEMAIN=./cmd/$(SERVICENAME)/main.go
FILEAPP=./bin/$(SERVICENAME) FILEAPP=./bin/$(SERVICENAME)
NEW_REPO=$(SERVICENAME) NEW_REPO=$(SERVICENAME)
CD_GENERATION_PROTO=cd ./pkg/network/grpc CD_GENERATION_PROTO=cd ./api
GENERATION_PROTO=generation_code.sh GENERATION_PROTO=generation_code.sh
@ -57,3 +57,4 @@ init:
$(CD_GENERATION_PROTO) $(CD_GENERATION_PROTO)
$(GENERATION_PROTO) $(GENERATION_PROTO)
go mod tidy go mod tidy
go mod vendor

View File

@ -25,4 +25,3 @@ func StartApp() {
stopapp.GetWaitGroup_Main().Wait() stopapp.GetWaitGroup_Main().Wait()
} }

View File

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

View File

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

View File

@ -1,21 +1,47 @@
convert_id.json: convert_id.json:
Mapping database table name . field name = field type in golang. Mapping database table name . field name = field type in golang.
For model file. For tables file.
For non standart rare case or type aliases For non standart rare case or type aliases
example:
{
"lawsuit_payments.id": "alias.PaymentId",
"lawsuit_payments.invoice_id": "alias.InvoiceId",
}
mapping.json mapping.json
Mapping Postgres types to Golang types Mapping Postgres types to Golang types
name_replace.json name_replace.json
Mapping database field name = golang field name Mapping database field name = golang field name
For model file. For tables file.
Replace standart field name to filled name Replace standart field name to filled name
For non standart rare case. For non standart rare case.
example:
{
"inn": "INN",
"json_file_id": "JSONFileID"
}
nullable.json nullable.json
List of golang field names, which need change 0 to null. List of golang field names, which need change 0 to null.
true = need change 0 to null true = need change 0 to null
For non standart rare case. For non standart rare case.
example:
{
"ext_id": true,
"created_at": false
}
model_crud_delete_functions.json
Mapping postgres tables to golang function name,
this functions will be deleted from model crud files.
example:
{
"lawsuits": "NewLawsuit,AsLawsuit"
}

View File

@ -1,21 +1,21 @@
#---------------------Database settings--------------------- #---------------------Database settings---------------------
#DB_HOST - Database server name or ip-address. Only Postgres SQL #DB_HOST - Database server name or ip-address. Only Postgres SQL
DB_HOST="" DB_HOST=
#DB_NAME - Database table name #DB_NAME - Database table name
DB_NAME="" DB_NAME=
#DB_SCHEME - Database schema name #DB_SCHEME - Database schema name
DB_SCHEME="" DB_SCHEME=
#DB_PORT - Database port number #DB_PORT - Database port number
DB_PORT="" DB_PORT=
#DB_USER - Database login (user) #DB_USER - Database login (user)
DB_USER="" DB_USER=
#DB_PASSWORD - Database login password #DB_PASSWORD - Database login password
DB_PASSWORD="" DB_PASSWORD=
#INCLUDE_TABLES - table name filter. Regular expression #INCLUDE_TABLES - table name filter. Regular expression
INCLUDE_TABLES="" INCLUDE_TABLES=""
@ -53,12 +53,11 @@ TEMPLATE_FOLDERNAME_CRUD="pkg/db/crud"
#TEMPLATE_FOLDERNAME_TABLES - folder name for create database tables models #TEMPLATE_FOLDERNAME_TABLES - folder name for create database tables models
TEMPLATE_FOLDERNAME_TABLES="pkg/db/tables" TEMPLATE_FOLDERNAME_TABLES="pkg/db/tables"
#TEMPLATE_FOLDERNAME_DB - folder name for create db constants #TEMPLATE_FOLDERNAME_DB - folder name for create db
TEMPLATE_FOLDERNAME_DB="pkg/db" TEMPLATE_FOLDERNAME_DB="pkg/db"
#TEMPLATE_FOLDERNAME_GRPC - folder name for create .proto file #TEMPLATE_FOLDERNAME_GRPC - folder name for create .proto file
TEMPLATE_FOLDERNAME_GRPC_PROTO="pkg/network/grpc" TEMPLATE_FOLDERNAME_GRPC_PROTO="api"
#TEMPLATE_FOLDERNAME_GRPC - folder name for grpc #TEMPLATE_FOLDERNAME_GRPC - folder name for grpc
TEMPLATE_FOLDERNAME_GRPC="pkg/network/grpc" TEMPLATE_FOLDERNAME_GRPC="pkg/network/grpc"
@ -84,6 +83,9 @@ TEMPLATE_FOLDERNAME_CRUD_STARTER="pkg/crud_starter"
#TEMPLATE_FOLDERNAME_ALIAS - folder name for create alias.go file, with go types with aliases #TEMPLATE_FOLDERNAME_ALIAS - folder name for create alias.go file, with go types with aliases
TEMPLATE_FOLDERNAME_ALIAS="pkg/object_model/types/alias" TEMPLATE_FOLDERNAME_ALIAS="pkg/object_model/types/alias"
#TEMPLATE_FOLDER_CRUD_FUNCTIONS - folder name for create crud_functions.go file
TEMPLATE_FOLDER_CRUD_FUNCTIONS="pkg/db/crud_functions"
#TEXT_TEMPLATE_MODEL - model name text in templates, for replace to new model names #TEXT_TEMPLATE_MODEL - model name text in templates, for replace to new model names
TEXT_TEMPLATE_MODEL="LawsuitStatusType" TEXT_TEMPLATE_MODEL="LawsuitStatusType"

View File

@ -281,4 +281,3 @@ func (s *ServerGRPC) LawsuitStatusType_FindByExtID(ctx context.Context, Request
return &Otvet, err return &Otvet, err
} }

View File

@ -3,7 +3,7 @@ package server_grpc
import ( import (
"errors" "errors"
"github.com/ManyakRus/starter/micro" "github.com/ManyakRus/starter/micro"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_client/constants" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/constants"
) )
// ErrorModelVersion // ErrorModelVersion

View File

@ -92,4 +92,3 @@ func Start() {
func CloseConnection() { func CloseConnection() {
Conn.Stop() Conn.Stop()
} }

View File

@ -190,4 +190,3 @@ func Test_server_LawsuitStatusType_FindByExtID(t *testing.T) {
t.Error("Test_server_LawsuitStatusType_FindByExtId() error: ModelString=''") t.Error("Test_server_LawsuitStatusType_FindByExtId() error: ModelString=''")
} }
} }

View File

@ -5,12 +5,17 @@ import (
"github.com/ManyakRus/starter/log" "github.com/ManyakRus/starter/log"
"github.com/ManyakRus/starter/stopapp" "github.com/ManyakRus/starter/stopapp"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/grpc_proto"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/internal/app/grpc/server_grpc" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/internal/app/grpc/server_grpc"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_proto"
"os" "os"
"strings"
"time" "time"
) )
//// server is used to implement UnimplementedSyncServiceServer.
//type server struct {
//}
type SettingsINI struct { type SettingsINI struct {
NATS_HOST string NATS_HOST string
NATS_PORT string NATS_PORT string
@ -77,6 +82,14 @@ func FillSettings() {
Settings.NATS_PORT = os.Getenv("BUS_LOCAL_PORT") Settings.NATS_PORT = os.Getenv("BUS_LOCAL_PORT")
} }
//удалим лишний :порт
if Settings.NATS_PORT != "" {
pos1 := strings.Index(Settings.NATS_HOST, ":")
if pos1 > 0 {
Settings.NATS_HOST = Settings.NATS_HOST[0:pos1]
}
}
//проверка //проверка
if Settings.NATS_HOST == "" { if Settings.NATS_HOST == "" {
log.Panic("Need fill BUS_LOCAL_HOST ! in OS Environment ") log.Panic("Need fill BUS_LOCAL_HOST ! in OS Environment ")
@ -89,6 +102,7 @@ func FillSettings() {
// WaitStop - ожидает отмену глобального контекста // WaitStop - ожидает отмену глобального контекста
func WaitStop() { func WaitStop() {
defer stopapp.GetWaitGroup_Main().Done()
select { select {
case <-contextmain.GetContext().Done(): case <-contextmain.GetContext().Done():
@ -100,7 +114,7 @@ func WaitStop() {
//закрываем соединение //закрываем соединение
CloseConnection() CloseConnection()
stopapp.GetWaitGroup_Main().Done()
} }
// Start - необходимые процедуры для запуска сервера GRPC // Start - необходимые процедуры для запуска сервера GRPC

View File

@ -1,7 +1,15 @@
package constants package constants
import (
"errors"
)
const CONNECTION_ID_TEST = 3 const CONNECTION_ID_TEST = 3
const TIMEOUT_DB_SECONDS = 30 const TIMEOUT_DB_SECONDS = 30
const TEXT_RECORD_NOT_FOUND = "record not found" const TEXT_RECORD_NOT_FOUND = "record not found"
const TextCrudIsNotInit = "Need initializate crud with InitCrudTransport_NRPC() function at first."
var ErrorCrudIsNotInit error = errors.New(TextCrudIsNotInit)

View File

@ -8,9 +8,9 @@ import (
"github.com/ManyakRus/starter/postgres_gorm" "github.com/ManyakRus/starter/postgres_gorm"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/constants" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/constants"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types"
"gorm.io/gorm"
"time" "time"
"github.com/ManyakRus/starter/contextmain" "github.com/ManyakRus/starter/contextmain"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud_functions"
) )
// TableName - имя таблицы в БД Postgres // TableName - имя таблицы в БД Postgres
@ -170,7 +170,6 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ
MassOmit := make([]string, 0) MassOmit := make([]string, 0)
var ColumnName string var ColumnName string
//игнор пустых колонок //игнор пустых колонок
tx = tx.Omit(MassOmit...) tx = tx.Omit(MassOmit...)
@ -185,18 +184,14 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ
return err return err
} }
// запишем NULL в пустые колонки //запишем NULL в пустые колонки
for f := 0; f < len(MassOmit); f++ { MapOmit := crud_functions.MapOmit_from_MassOmit(MassOmit)
ColumnName := MassOmit[f] tx = db.Model(&m).Updates(MapOmit)
tx = db.Model(&m).Update(ColumnName, gorm.Expr("NULL"))
err = tx.Error err = tx.Error
if err != nil { if err != nil {
TextError := fmt.Sprint("db.Update() ", TableName, " id: ", m.ID, " error: ", err) TextError := fmt.Sprint("Updates() ", TableName, " id: ", m.ID, " error: ", err)
err = errors.New(TextError) err = errors.New(TextError)
return err return err
// log.Panic(sError)
}
} }
return err return err

View File

@ -0,0 +1,13 @@
package crud_functions
import "gorm.io/gorm"
// MapOmit_from_MassOmit - создает MapOmit из MassOmit
func MapOmit_from_MassOmit(MassOmit []string) map[string]interface{} {
MapOmit := make(map[string]interface{}, 0)
for _, v := range MassOmit {
MapOmit[v] = gorm.Expr("NULL")
}
return MapOmit
}

View File

@ -0,0 +1 @@
package crud_functions

View File

@ -10,4 +10,3 @@ type LawsuitStatusType struct {
tables.NameStruct tables.NameStruct
Code string `json:"code" gorm:"column:code;default:0"` Code string `json:"code" gorm:"column:code;default:0"`
} }

View File

@ -319,4 +319,3 @@ func (crud Crud_GRPC) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) er
return err return err
} }

View File

@ -182,4 +182,3 @@ func TestFindByExtID(t *testing.T) {
t.Error("TestFindByExtID() error: ID =0") t.Error("TestFindByExtID() error: ID =0")
} }
} }

View File

@ -289,4 +289,3 @@ func (crud Crud_NRPC) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) er
return err return err
} }

View File

@ -182,4 +182,3 @@ func TestFindByExtID(t *testing.T) {
t.Error("TestFindByExtId() error: ID =0") t.Error("TestFindByExtId() error: ID =0")
} }
} }

View File

@ -8,4 +8,3 @@ import (
type LawsuitStatusType struct { type LawsuitStatusType struct {
table_lawsuit_status_types.Table_LawsuitStatusType table_lawsuit_status_types.Table_LawsuitStatusType
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/calc_struct_version" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/calc_struct_version"
"reflect" "reflect"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/constants"
) )
// versionLawsuitStatusType - версия структуры модели, с учётом имен и типов полей // versionLawsuitStatusType - версия структуры модели, с учётом имен и типов полей
@ -91,6 +92,10 @@ func (m LawsuitStatusType) GetJSON() (string, error) {
// Read - находит запись в БД по ID, и заполняет в объект // Read - находит запись в БД по ID, и заполняет в объект
func (m *LawsuitStatusType) Read() error { func (m *LawsuitStatusType) Read() error {
if crud_LawsuitStatusType == nil {
return constants.ErrorCrudIsNotInit
}
err := crud_LawsuitStatusType.Read(m) err := crud_LawsuitStatusType.Read(m)
return err return err
@ -98,6 +103,10 @@ func (m *LawsuitStatusType) Read() error {
// Save - записывает объект в БД по ID // Save - записывает объект в БД по ID
func (m *LawsuitStatusType) Save() error { func (m *LawsuitStatusType) Save() error {
if crud_LawsuitStatusType == nil {
return constants.ErrorCrudIsNotInit
}
err := crud_LawsuitStatusType.Save(m) err := crud_LawsuitStatusType.Save(m)
return err return err
@ -105,6 +114,10 @@ func (m *LawsuitStatusType) Save() error {
// Update - обновляет объект в БД по ID // Update - обновляет объект в БД по ID
func (m *LawsuitStatusType) Update() error { func (m *LawsuitStatusType) Update() error {
if crud_LawsuitStatusType == nil {
return constants.ErrorCrudIsNotInit
}
err := crud_LawsuitStatusType.Update(m) err := crud_LawsuitStatusType.Update(m)
return err return err
@ -112,6 +125,10 @@ func (m *LawsuitStatusType) Update() error {
// Create - создаёт объект в БД с новым ID // Create - создаёт объект в БД с новым ID
func (m *LawsuitStatusType) Create() error { func (m *LawsuitStatusType) Create() error {
if crud_LawsuitStatusType == nil {
return constants.ErrorCrudIsNotInit
}
err := crud_LawsuitStatusType.Create(m) err := crud_LawsuitStatusType.Create(m)
return err return err
@ -119,6 +136,10 @@ func (m *LawsuitStatusType) Create() error {
// Delete - устанавливает признак пометки удаления в БД // Delete - устанавливает признак пометки удаления в БД
func (m *LawsuitStatusType) Delete() error { func (m *LawsuitStatusType) Delete() error {
if crud_LawsuitStatusType == nil {
return constants.ErrorCrudIsNotInit
}
err := crud_LawsuitStatusType.Delete(m) err := crud_LawsuitStatusType.Delete(m)
return err return err
@ -126,6 +147,10 @@ func (m *LawsuitStatusType) Delete() error {
// Restore - снимает признак пометки удаления в БД // Restore - снимает признак пометки удаления в БД
func (m *LawsuitStatusType) Restore() error { func (m *LawsuitStatusType) Restore() error {
if crud_LawsuitStatusType == nil {
return constants.ErrorCrudIsNotInit
}
err := crud_LawsuitStatusType.Restore(m) err := crud_LawsuitStatusType.Restore(m)
return err return err
@ -133,6 +158,10 @@ func (m *LawsuitStatusType) Restore() error {
// Find_ByExtID - находит объект по ExtID // Find_ByExtID - находит объект по ExtID
func (m *LawsuitStatusType) Find_ByExtID() error { func (m *LawsuitStatusType) Find_ByExtID() error {
if crud_LawsuitStatusType == nil {
return constants.ErrorCrudIsNotInit
}
err := crud_LawsuitStatusType.Find_ByExtID(m) err := crud_LawsuitStatusType.Find_ByExtID(m)
return err return err