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---------------------
#DB_HOST - Database server name or ip-address. Only Postgres SQL
DB_HOST=""
DB_HOST=
#DB_NAME - Database table name
DB_NAME=""
DB_NAME=
#DB_SCHEME - Database schema name
DB_SCHEME="public"
DB_SCHEME=
#DB_PORT - Database port number
DB_PORT="5432"
DB_PORT=
#DB_USER - Database login (user)
DB_USER=""
DB_USER=
#DB_PASSWORD - Database login password
DB_PASSWORD=""
DB_PASSWORD=
#INCLUDE_TABLES - table name filter. Regular expression
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="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_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="pkg/network/grpc"
#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="pkg/network/grpc/grpc_client"
#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="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="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="LawsuitStatusType"

View File

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

View File

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

View File

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

View File

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

View File

@ -1,21 +1,47 @@
convert_id.json:
Mapping database table name . field name = field type in golang.
For model file.
For tables file.
For non standart rare case or type aliases
example:
{
"lawsuit_payments.id": "alias.PaymentId",
"lawsuit_payments.invoice_id": "alias.InvoiceId",
}
mapping.json
Mapping Postgres types to Golang types
name_replace.json
Mapping database field name = golang field name
For model file.
For tables file.
Replace standart field name to filled name
For non standart rare case.
example:
{
"inn": "INN",
"json_file_id": "JSONFileID"
}
nullable.json
List of golang field names, which need change 0 to null.
true = need change 0 to null
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---------------------
#DB_HOST - Database server name or ip-address. Only Postgres SQL
DB_HOST=""
DB_HOST=
#DB_NAME - Database table name
DB_NAME=""
DB_NAME=
#DB_SCHEME - Database schema name
DB_SCHEME=""
DB_SCHEME=
#DB_PORT - Database port number
DB_PORT=""
DB_PORT=
#DB_USER - Database login (user)
DB_USER=""
DB_USER=
#DB_PASSWORD - Database login password
DB_PASSWORD=""
DB_PASSWORD=
#INCLUDE_TABLES - table name filter. Regular expression
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="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_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="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="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="LawsuitStatusType"

View File

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

View File

@ -3,7 +3,7 @@ package server_grpc
import (
"errors"
"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

View File

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

View File

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

View File

@ -5,12 +5,17 @@ import (
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/starter/stopapp"
"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/pkg/network/grpc/grpc_proto"
"os"
"strings"
"time"
)
//// server is used to implement UnimplementedSyncServiceServer.
//type server struct {
//}
type SettingsINI struct {
NATS_HOST string
NATS_PORT string
@ -77,6 +82,14 @@ func FillSettings() {
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 == "" {
log.Panic("Need fill BUS_LOCAL_HOST ! in OS Environment ")
@ -89,6 +102,7 @@ func FillSettings() {
// WaitStop - ожидает отмену глобального контекста
func WaitStop() {
defer stopapp.GetWaitGroup_Main().Done()
select {
case <-contextmain.GetContext().Done():
@ -100,7 +114,7 @@ func WaitStop() {
//закрываем соединение
CloseConnection()
stopapp.GetWaitGroup_Main().Done()
}
// Start - необходимые процедуры для запуска сервера GRPC

View File

@ -1,7 +1,15 @@
package constants
import (
"errors"
)
const CONNECTION_ID_TEST = 3
const TIMEOUT_DB_SECONDS = 30
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"
"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"
"gorm.io/gorm"
"time"
"github.com/ManyakRus/starter/contextmain"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud_functions"
)
// TableName - имя таблицы в БД Postgres
@ -170,7 +170,6 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ
MassOmit := make([]string, 0)
var ColumnName string
//игнор пустых колонок
tx = tx.Omit(MassOmit...)
@ -186,17 +185,13 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ
}
//запишем NULL в пустые колонки
for f := 0; f < len(MassOmit); f++ {
ColumnName := MassOmit[f]
tx = db.Model(&m).Update(ColumnName, gorm.Expr("NULL"))
MapOmit := crud_functions.MapOmit_from_MassOmit(MassOmit)
tx = db.Model(&m).Updates(MapOmit)
err = tx.Error
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)
return err
// log.Panic(sError)
}
}
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
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
}

View File

@ -182,4 +182,3 @@ func TestFindByExtID(t *testing.T) {
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
}

View File

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

View File

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

View File

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