diff --git a/assembly/locator.go b/assembly/locator.go index 43b58ff..88779ec 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -44,18 +44,18 @@ func (l Locator) Config(cfg conf.Remote) Config { accessListRep := repository.NewAccessList(l.db) applicationRep := repository.NewApplication(l.db) domainRep := repository.NewDomain(l.db) - serviceRep := repository.NewService(l.db) + appGroupRep := repository.NewAppGroup(l.db) tokenRep := repository.NewToken(l.db) secureService := secure.NewService(tokenRep, accessListRep) accessListService := service.NewAccessList(txManager, accessListRep, applicationRep) - applicationService := service.NewApplication(txManager, applicationRep, domainRep, serviceRep, tokenRep) + applicationService := service.NewApplication(txManager, applicationRep, domainRep, appGroupRep, tokenRep) domainService := service.NewDomain(domainRep) - serviceService := service.NewService(domainRep, serviceRep) + serviceService := service.NewService(domainRep, appGroupRep) jwtService := service.NewTokenSource() tokenService := service.NewToken(jwtService, applicationService, txManager, - applicationRep, domainRep, serviceRep, tokenRep, + applicationRep, domainRep, appGroupRep, tokenRep, ) secureController := controller.NewSecure(secureService) diff --git a/controller/application.go b/controller/application.go index 6eb3004..b3f28a3 100644 --- a/controller/application.go +++ b/controller/application.go @@ -3,10 +3,11 @@ package controller import ( "context" + "isp-system-service/domain" + "github.com/pkg/errors" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "isp-system-service/domain" ) type ApplicationService interface { @@ -109,7 +110,7 @@ func (c Application) GetSystemTree(ctx context.Context) ([]*domain.DomainWithSer func (c Application) CreateUpdate(ctx context.Context, req domain.ApplicationCreateUpdateRequest) (*domain.ApplicationWithTokens, error) { result, err := c.service.CreateUpdate(ctx, req) switch { - case errors.Is(err, domain.ErrServiceNotFound): + case errors.Is(err, domain.ErrAppGroupNotFound): return nil, status.Errorf(codes.InvalidArgument, "service with id %d not found", req.ServiceId) case errors.Is(err, domain.ErrApplicationDuplicateName): return nil, status.Errorf(codes.AlreadyExists, "application with name %s already exists", req.Name) diff --git a/controller/service.go b/controller/service.go index f60a6fd..973cf62 100644 --- a/controller/service.go +++ b/controller/service.go @@ -3,10 +3,11 @@ package controller import ( "context" + "isp-system-service/domain" + "github.com/pkg/errors" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "isp-system-service/domain" ) type ServiceService interface { @@ -41,7 +42,7 @@ func NewService(service ServiceService) Service { func (c Service) GetById(ctx context.Context, req domain.Identity) (*domain.Service, error) { result, err := c.service.GetById(ctx, req.Id) switch { - case errors.Is(err, domain.ErrServiceNotFound): + case errors.Is(err, domain.ErrAppGroupNotFound): return nil, status.Errorf(codes.NotFound, "service with id %d not found", req.Id) case err != nil: return nil, err @@ -96,9 +97,9 @@ func (c Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdat switch { case errors.Is(err, domain.ErrDomainNotFound): return nil, status.Errorf(codes.InvalidArgument, "domain with id %d not found", req.DomainId) - case errors.Is(err, domain.ErrServiceNotFound): + case errors.Is(err, domain.ErrAppGroupNotFound): return nil, status.Errorf(codes.NotFound, "service with id %d not found", req.Id) - case errors.Is(err, domain.ErrServiceDuplicateName): + case errors.Is(err, domain.ErrAppGroupDuplicateName): return nil, status.Errorf(codes.AlreadyExists, "service with name %s already exists", req.Name) case err != nil: return nil, err diff --git a/controller/token.go b/controller/token.go index 16155ea..e78eca7 100644 --- a/controller/token.go +++ b/controller/token.go @@ -3,10 +3,11 @@ package controller import ( "context" + "isp-system-service/domain" + "github.com/pkg/errors" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "isp-system-service/domain" ) type TokenService interface { @@ -55,7 +56,7 @@ func (c Token) Create(ctx context.Context, req domain.TokenCreateRequest) (*doma switch { case errors.Is(err, domain.ErrApplicationNotFound): return nil, status.Errorf(codes.NotFound, "application with id %d not found", req.AppId) - case errors.Is(err, domain.ErrServiceNotFound): + case errors.Is(err, domain.ErrAppGroupNotFound): return nil, status.Errorf(codes.NotFound, "service for app_id id %d not found", req.AppId) case errors.Is(err, domain.ErrDomainNotFound): return nil, status.Errorf(codes.NotFound, "domain for app_id %d not found", req.AppId) diff --git a/domain/error.go b/domain/error.go index ad9a284..eb64a99 100644 --- a/domain/error.go +++ b/domain/error.go @@ -8,13 +8,14 @@ var ( ErrDomainNotFound = errors.New("domain not found") ErrDomainDuplicateName = errors.New("domain name already exist") - ErrServiceNotFound = errors.New("service not found") - ErrServiceDuplicateName = errors.New("service name already exist") + ErrAppGroupNotFound = errors.New("application group not found") + ErrAppGroupDuplicateName = errors.New("application group name already exist") ErrSystemNotFound = errors.New("system not found") ErrApplicationNotFound = errors.New("application not found") ErrApplicationDuplicateName = errors.New("application name already exist") + ErrApplicationDuplicateId = errors.New("application id already exist") ErrTokenNotFound = errors.New("token not found") ErrTokenExpired = errors.New("token is expired") diff --git a/entity/application.go b/entity/application.go index 4d38c74..1fcf51d 100644 --- a/entity/application.go +++ b/entity/application.go @@ -5,11 +5,11 @@ import ( ) type Application struct { - Id int - Name string - Description *string - ServiceId int - Type string - CreatedAt time.Time - UpdatedAt time.Time + Id int + Name string + Description *string + ApplicationGroupId int + Type string + CreatedAt time.Time + UpdatedAt time.Time } diff --git a/entity/service.go b/entity/service.go index 9a791d9..82e7d92 100644 --- a/entity/service.go +++ b/entity/service.go @@ -4,7 +4,7 @@ import ( "time" ) -type Service struct { +type AppGroup struct { Id int Name string Description *string diff --git a/entity/token.go b/entity/token.go index c4277f1..5074262 100644 --- a/entity/token.go +++ b/entity/token.go @@ -12,10 +12,10 @@ type Token struct { } type AuthData struct { - SystemId int - DomainId int - ServiceId int - AppId int - ExpireTime int - CreatedAt time.Time + SystemId int + DomainId int + ApplicationGroupId int + AppId int + ExpireTime int + CreatedAt time.Time } diff --git a/migrations/20250401144357_application_service.sql b/migrations/20250401144357_application_service.sql new file mode 100644 index 0000000..bc64391 --- /dev/null +++ b/migrations/20250401144357_application_service.sql @@ -0,0 +1,42 @@ +-- +goose Up +ALTER TABLE application ALTER COLUMN id DROP DEFAULT; + +DROP SEQUENCE application_id_seq; + +ALTER TABLE service RENAME TO application_group; + +ALTER TABLE application +RENAME COLUMN service_id TO application_group_id; + +ALTER INDEX uq_name_service_id +RENAME TO uq_name_application_group_id; + +ALTER TABLE application +RENAME CONSTRAINT "fk_service_id__domain_id" TO "fk_application_group_id"; + +-- +goose Down +ALTER TABLE application +RENAME CONSTRAINT "fk_application_group_id" TO "fk_service_id__domain_id"; + +ALTER INDEX uq_name_application_group_id +RENAME TO uq_name_service_id; + +ALTER TABLE application +RENAME COLUMN application_group_id TO service_id; + +ALTER TABLE application_group RENAME TO service; + +CREATE SEQUENCE application_id_seq; + +SELECT setval( + 'application_id_seq', COALESCE( + ( + SELECT MAX(id) + FROM isp_system_service.application + ), 1 + ) + ); + +ALTER TABLE application +ALTER COLUMN id +SET DEFAULT nextval('application_id_seq'); \ No newline at end of file diff --git a/migrations/20250401144357_change_application_pk_type.sql b/migrations/20250401144357_change_application_pk_type.sql deleted file mode 100644 index 3c64b07..0000000 --- a/migrations/20250401144357_change_application_pk_type.sql +++ /dev/null @@ -1,22 +0,0 @@ --- +goose Up -ALTER TABLE application -ALTER COLUMN id -DROP DEFAULT; - -DROP SEQUENCE application_id_seq; - --- +goose Down -CREATE SEQUENCE application_id_seq; - -SELECT setval( - 'application_id_seq', COALESCE( - ( - SELECT MAX(id) - FROM isp_system_service.application - ), 1 - ) - ); - -ALTER TABLE application -ALTER COLUMN id -SET DEFAULT nextval('application_id_seq'); \ No newline at end of file diff --git a/repository/application.go b/repository/application.go index 5d42bbe..3812c14 100644 --- a/repository/application.go +++ b/repository/application.go @@ -24,7 +24,7 @@ func NewApplication(db db.DB) Application { func (r Application) GetApplicationById(ctx context.Context, id int) (*entity.Application, error) { q := ` - SELECT id, name, description, service_id, type, created_at, updated_at + SELECT id, name, description, application_group_id, type, created_at, updated_at FROM application WHERE id = $1 ` @@ -34,7 +34,7 @@ func (r Application) GetApplicationById(ctx context.Context, id int) (*entity.Ap if errors.Is(err, sql.ErrNoRows) { return nil, domain.ErrApplicationNotFound } - return nil, errors.WithMessage(err, "select row db") + return nil, errors.WithMessagef(err, "exec query %s", q) } return &result, nil @@ -42,7 +42,7 @@ func (r Application) GetApplicationById(ctx context.Context, id int) (*entity.Ap func (r Application) GetApplicationByIdList(ctx context.Context, idList []int) ([]entity.Application, error) { q, args, err := query.New(). - Select("id", "name", "description", "service_id", "type", "created_at", "updated_at"). + Select("id", "name", "description", "application_group_id", "type", "created_at", "updated_at"). From("application"). Where(squirrel.Eq{"id": idList}). OrderBy("created_at DESC"). @@ -54,37 +54,37 @@ func (r Application) GetApplicationByIdList(ctx context.Context, idList []int) ( result := make([]entity.Application, 0) err = r.db.Select(ctx, &result, q, args...) if err != nil { - return nil, errors.WithMessagef(err, "select db") + return nil, errors.WithMessagef(err, "exec query %s", q) } return result, nil } -func (r Application) GetApplicationByServiceIdList(ctx context.Context, serviceIdList []int) ([]entity.Application, error) { +func (r Application) GetApplicationByAppGroupIdList(ctx context.Context, appGroupIdList []int) ([]entity.Application, error) { q, args, err := query.New(). - Select("id", "name", "description", "service_id", "type", "created_at", "updated_at"). + Select("id", "name", "description", "application_group_id", "type", "created_at", "updated_at"). From("application"). - Where(squirrel.Eq{"service_id": serviceIdList}). + Where(squirrel.Eq{"application_group_id": appGroupIdList}). OrderBy("created_at DESC"). ToSql() if err != nil { - return nil, errors.WithMessage(err, "build query") + return nil, errors.WithMessagef(err, "exec query %s", q) } result := make([]entity.Application, 0) err = r.db.Select(ctx, &result, q, args...) if err != nil { - return nil, errors.WithMessagef(err, "select db") + return nil, errors.WithMessagef(err, "exec query %s", q) } return result, nil } -func (r Application) GetApplicationByNameAndServiceId(ctx context.Context, name string, serviceId int) (*entity.Application, error) { +func (r Application) GetApplicationByNameAndAppGroupId(ctx context.Context, name string, serviceId int) (*entity.Application, error) { q := ` - SELECT id, name, description, service_id, type, created_at, updated_at + SELECT id, name, description, application_group_id, type, created_at, updated_at FROM application - WHERE name = $1 AND service_id = $2 + WHERE name = $1 AND application_group_id = $2 ` result := entity.Application{} err := r.db.SelectRow(ctx, &result, q, name, serviceId) @@ -92,41 +92,50 @@ func (r Application) GetApplicationByNameAndServiceId(ctx context.Context, name if errors.Is(err, sql.ErrNoRows) { return nil, nil //nolint:nilnil } - return nil, errors.WithMessagef(err, "select row db") + return nil, errors.WithMessagef(err, "exec query %s", q) } return &result, nil } -func (r Application) CreateApplication(ctx context.Context, name string, desc string, serviceId int, appType string) ( +func (r Application) CreateApplication(ctx context.Context, id int, name string, desc string, appGroupId int, appType string) ( *entity.Application, error) { q := ` INSERT INTO application - (name, description, service_id, type) - VALUES ($1, $2, $3, $4) - RETURNING id, name, description, service_id, type, created_at, updated_at + (id, name, description, application_group_id, type) + VALUES ($1, $2, $3, $4, $5) + ON CONFLICT (id) DO NOTHING + RETURNING id, name, description, application_group_id, type, created_at, updated_at ` result := entity.Application{} - err := r.db.SelectRow(ctx, &result, q, name, desc, serviceId, appType) - if err != nil { - return nil, errors.WithMessagef(err, "select row db") + err := r.db.SelectRow(ctx, &result, q, id, name, desc, appGroupId, appType) + switch { + case errors.Is(err, sql.ErrNoRows): + return nil, domain.ErrApplicationDuplicateId + case err != nil: + return nil, errors.WithMessagef(err, "exec query %s", q) + default: + return &result, nil } - - return &result, nil } -func (r Application) UpdateApplication(ctx context.Context, id int, name string, description string) (*entity.Application, error) { +func (r Application) UpdateApplication( + ctx context.Context, + appGroupId int, + name string, + description string, +) (*entity.Application, error) { q := ` UPDATE application SET name = $2, description = $3 WHERE id = $1 - RETURNING id, name, description, service_id, type, created_at, updated_at + RETURNING id, name, description, application_group_id, type, created_at, updated_at ` result := entity.Application{} - err := r.db.SelectRow(ctx, &result, q, id, name, description) + err := r.db.SelectRow(ctx, &result, q, appGroupId, name, description) if err != nil { - return nil, errors.WithMessagef(err, "select row db") + return nil, errors.WithMessagef(err, "exec query %s", q) } return &result, nil @@ -153,3 +162,13 @@ func (r Application) DeleteApplicationByIdList(ctx context.Context, idList []int return int(rowsAffected), nil } + +func (r Application) NextApplicationId(ctx context.Context) (int, error) { + q := `SELECT COALESCE(MAX(id)+1, 1) FROM application;` + nextAppId := 0 + err := r.db.SelectRow(ctx, &nextAppId, q) + if err != nil { + return -1, errors.WithMessagef(err, "exec query %s", q) + } + return nextAppId, nil +} diff --git a/repository/service.go b/repository/service.go index 150db01..bf28b8b 100644 --- a/repository/service.go +++ b/repository/service.go @@ -4,35 +4,36 @@ import ( "context" "database/sql" + "isp-system-service/domain" + "isp-system-service/entity" + "github.com/Masterminds/squirrel" "github.com/pkg/errors" "github.com/txix-open/isp-kit/db" "github.com/txix-open/isp-kit/db/query" - "isp-system-service/domain" - "isp-system-service/entity" ) -type Service struct { +type AppGroup struct { db db.DB } -func NewService(db db.DB) Service { - return Service{ +func NewAppGroup(db db.DB) AppGroup { + return AppGroup{ db: db, } } -func (r Service) GetServiceById(ctx context.Context, id int) (*entity.Service, error) { +func (r AppGroup) GetAppGroupById(ctx context.Context, id int) (*entity.AppGroup, error) { q := ` SELECT id, name, description, domain_id, created_at, updated_at - FROM service + FROM application_group WHERE id = $1 ` - result := entity.Service{} + result := entity.AppGroup{} err := r.db.SelectRow(ctx, &result, q, id) if err != nil { if errors.Is(err, sql.ErrNoRows) { - return nil, domain.ErrServiceNotFound + return nil, domain.ErrAppGroupNotFound } return nil, errors.WithMessage(err, "select row db") } @@ -40,10 +41,10 @@ func (r Service) GetServiceById(ctx context.Context, id int) (*entity.Service, e return &result, nil } -func (r Service) GetServiceByIdList(ctx context.Context, idList []int) ([]entity.Service, error) { +func (r AppGroup) GetAppGroupByIdList(ctx context.Context, idList []int) ([]entity.AppGroup, error) { q, arg, err := query.New(). Select("id", "name", "description", "domain_id", "created_at", "updated_at"). - From("service"). + From("application_group"). Where(squirrel.Eq{"id": idList}). OrderBy("created_at DESC"). ToSql() @@ -51,7 +52,7 @@ func (r Service) GetServiceByIdList(ctx context.Context, idList []int) ([]entity return nil, errors.WithMessage(err, "build query") } - result := make([]entity.Service, 0) + result := make([]entity.AppGroup, 0) err = r.db.Select(ctx, &result, q, arg...) if err != nil { return nil, errors.WithMessagef(err, "select db") @@ -60,10 +61,10 @@ func (r Service) GetServiceByIdList(ctx context.Context, idList []int) ([]entity return result, nil } -func (r Service) GetServiceByDomainId(ctx context.Context, domainIdList []int) ([]entity.Service, error) { +func (r AppGroup) GetAppGroupByDomainId(ctx context.Context, domainIdList []int) ([]entity.AppGroup, error) { q, arg, err := query.New(). Select("id", "name", "description", "domain_id", "created_at", "updated_at"). - From("service"). + From("application_group"). Where(squirrel.Eq{"domain_id": domainIdList}). OrderBy("created_at DESC"). ToSql() @@ -71,7 +72,7 @@ func (r Service) GetServiceByDomainId(ctx context.Context, domainIdList []int) ( return nil, errors.WithMessage(err, "build query") } - result := make([]entity.Service, 0) + result := make([]entity.AppGroup, 0) err = r.db.Select(ctx, &result, q, arg...) if err != nil { return nil, errors.WithMessagef(err, "select db") @@ -80,17 +81,17 @@ func (r Service) GetServiceByDomainId(ctx context.Context, domainIdList []int) ( return result, nil } -func (r Service) GetServiceByNameAndDomainId(ctx context.Context, name string, domainId int) (*entity.Service, error) { +func (r AppGroup) GetAppGroupByNameAndDomainId(ctx context.Context, name string, domainId int) (*entity.AppGroup, error) { q := ` SELECT id, name, description, domain_id, created_at, updated_at - FROM service + FROM application_group WHERE name = $1 AND domain_id = $2 ` - result := entity.Service{} + result := entity.AppGroup{} err := r.db.SelectRow(ctx, &result, q, name, domainId) if err != nil { if errors.Is(err, sql.ErrNoRows) { - return nil, domain.ErrServiceNotFound + return nil, domain.ErrAppGroupNotFound } return nil, errors.WithMessage(err, "select db") } @@ -98,14 +99,14 @@ func (r Service) GetServiceByNameAndDomainId(ctx context.Context, name string, d return &result, nil } -func (r Service) CreateService(ctx context.Context, name string, desc string, domainId int) (*entity.Service, error) { +func (r AppGroup) CreateAppGroup(ctx context.Context, name string, desc string, domainId int) (*entity.AppGroup, error) { q := ` - INSERT INTO service + INSERT INTO application_group (name, description, domain_id) VALUES ($1, $2, $3) RETURNING id, name, description, domain_id, created_at, updated_at ` - result := entity.Service{} + result := entity.AppGroup{} err := r.db.SelectRow(ctx, &result, q, name, desc, domainId) if err != nil { return nil, errors.WithMessage(err, "select row db") @@ -114,18 +115,18 @@ func (r Service) CreateService(ctx context.Context, name string, desc string, do return &result, nil } -func (r Service) UpdateService(ctx context.Context, id int, name string, description string) (*entity.Service, error) { +func (r AppGroup) UpdateAppGroup(ctx context.Context, id int, name string, description string) (*entity.AppGroup, error) { q := ` - UPDATE service + UPDATE application_group SET name = $1, description = $2 WHERE id = $3 RETURNING id, name, description, domain_id, created_at, updated_at ` - result := entity.Service{} + result := entity.AppGroup{} err := r.db.SelectRow(ctx, &result, q, name, description, id) if err != nil { if errors.Is(err, sql.ErrNoRows) { - return nil, domain.ErrServiceNotFound + return nil, domain.ErrAppGroupNotFound } return nil, errors.WithMessage(err, "select row db") } @@ -133,9 +134,9 @@ func (r Service) UpdateService(ctx context.Context, id int, name string, descrip return &result, nil } -func (r Service) DeleteService(ctx context.Context, idList []int) (int, error) { +func (r AppGroup) DeleteAppGroup(ctx context.Context, idList []int) (int, error) { q, args, err := query.New(). - Delete("service"). + Delete("application_group"). Where(squirrel.Eq{"id": idList}). ToSql() if err != nil { diff --git a/repository/token.go b/repository/token.go index 14b47fa..953cfc3 100644 --- a/repository/token.go +++ b/repository/token.go @@ -100,14 +100,14 @@ func (r Token) DeleteToken(ctx context.Context, tokens []string) (int, error) { func (r Token) AuthDataByToken(ctx context.Context, token string) (*entity.AuthData, error) { q := ` -SELECT system_id, domain_id, service_id, app_id, token.expire_time, token.created_at +SELECT system_id, domain_id, application_group_id, app_id, token.expire_time, token.created_at FROM token LEFT JOIN application ON token.app_id = application.id - LEFT JOIN service - ON application.service_id = service.id + LEFT JOIN application_group + ON application.application_group_id = application_group.id LEFT JOIN domain - ON service.domain_id = domain.id + ON application_group.domain_id = domain.id WHERE token = $1 ` result := entity.AuthData{} diff --git a/service/application.go b/service/application.go index aff2c45..9c5fa15 100644 --- a/service/application.go +++ b/service/application.go @@ -9,11 +9,6 @@ import ( "github.com/pkg/errors" ) -type ApplicationServiceRep interface { - GetServiceById(ctx context.Context, id int) (*entity.Service, error) - GetServiceByDomainId(ctx context.Context, domainIdList []int) ([]entity.Service, error) -} - type ApplicationDeleteTx interface { DeleteApplicationByIdList(ctx context.Context, idList []int) (int, error) } @@ -26,7 +21,7 @@ type Application struct { txRunner ApplicationTxRunner appRepo ApplicationRepo domainRepo DomainRepo - serviceRepo ApplicationServiceRep + serviceRepo AppGroupRepo tokenRepo TokenRepo } @@ -34,14 +29,14 @@ func NewApplication( txRunner ApplicationTxRunner, applicationRepo ApplicationRepo, domainRepo DomainRepo, - serviceRepo ApplicationServiceRep, + appGroupRepo AppGroupRepo, tokenRepo TokenRepo, ) Application { return Application{ txRunner: txRunner, appRepo: applicationRepo, domainRepo: domainRepo, - serviceRepo: serviceRepo, + serviceRepo: appGroupRepo, tokenRepo: tokenRepo, } } @@ -70,9 +65,9 @@ func (s Application) GetByIdList(ctx context.Context, idList []int) ([]*domain.A } func (s Application) GetByServiceId(ctx context.Context, id int) ([]*domain.ApplicationWithTokens, error) { - arr, err := s.appRepo.GetApplicationByServiceIdList(ctx, []int{id}) + arr, err := s.appRepo.GetApplicationByAppGroupIdList(ctx, []int{id}) if err != nil { - return nil, errors.WithMessage(err, "get application by service_id") + return nil, errors.WithMessage(err, "get application by app_group_id") } return s.EnrichWithTokens(ctx, arr) @@ -106,7 +101,7 @@ func (s Application) SystemTree(ctx context.Context, systemId int) ([]*domain.Do result[i] = r } - serviceEntityList, err := s.serviceRepo.GetServiceByDomainId(ctx, domainIdList) + serviceEntityList, err := s.serviceRepo.GetAppGroupByDomainId(ctx, domainIdList) if err != nil { return nil, errors.WithMessage(err, "get service by domain_id") } @@ -130,13 +125,13 @@ func (s Application) SystemTree(ctx context.Context, systemId int) ([]*domain.Do resultServiceByServiceId[serviceEntity.Id] = resultService } - applicationEntityList, err := s.appRepo.GetApplicationByServiceIdList(ctx, serviceIdList) + applicationEntityList, err := s.appRepo.GetApplicationByAppGroupIdList(ctx, serviceIdList) if err != nil { return nil, errors.WithMessage(err, "get application by service_id") } for _, applicationEntity := range applicationEntityList { - resultService := resultServiceByServiceId[applicationEntity.ServiceId] + resultService := resultServiceByServiceId[applicationEntity.ApplicationGroupId] description := "" if applicationEntity.Description != nil { description = *applicationEntity.Description @@ -154,14 +149,14 @@ func (s Application) SystemTree(ctx context.Context, systemId int) ([]*domain.Do } func (s Application) CreateUpdate(ctx context.Context, req domain.ApplicationCreateUpdateRequest) (*domain.ApplicationWithTokens, error) { - existed, err := s.appRepo.GetApplicationByNameAndServiceId(ctx, req.Name, req.ServiceId) + existed, err := s.appRepo.GetApplicationByNameAndAppGroupId(ctx, req.Name, req.ServiceId) switch { case errors.Is(err, domain.ErrApplicationNotFound): case err != nil: return nil, errors.WithMessage(err, "get application by name and service_id") } - _, err = s.serviceRepo.GetServiceById(ctx, req.ServiceId) + _, err = s.serviceRepo.GetAppGroupById(ctx, req.ServiceId) if err != nil { return nil, errors.WithMessage(err, "get service by id") } @@ -171,12 +166,17 @@ func (s Application) CreateUpdate(ctx context.Context, req domain.ApplicationCre return nil, domain.ErrApplicationDuplicateName } - _, err = s.appRepo.GetApplicationByNameAndServiceId(ctx, req.Name, req.ServiceId) + _, err = s.appRepo.GetApplicationByNameAndAppGroupId(ctx, req.Name, req.ServiceId) if err != nil { return nil, errors.WithMessage(err, "get application by name and service_id") } - app, err := s.appRepo.CreateApplication(ctx, req.Name, req.Description, req.ServiceId, req.Type) + appId, err := s.appRepo.NextApplicationId(ctx) + if err != nil { + return nil, errors.WithMessage(err, "next app id") + } + + app, err := s.appRepo.CreateApplication(ctx, appId, req.Name, req.Description, req.ServiceId, req.Type) if err != nil { return nil, errors.WithMessage(err, "create application") } @@ -270,7 +270,7 @@ func (s Application) convertApplication(req entity.Application) domain.Applicati Id: req.Id, Name: req.Name, Description: desc, - ServiceId: req.ServiceId, + ServiceId: req.ApplicationGroupId, Type: req.Type, CreatedAt: req.CreatedAt, UpdatedAt: req.UpdatedAt, diff --git a/service/baseline/service.go b/service/baseline/service.go index 9221eae..e085137 100644 --- a/service/baseline/service.go +++ b/service/baseline/service.go @@ -2,16 +2,19 @@ package baseline import ( "context" - "github.com/pkg/errors" - "github.com/txix-open/isp-kit/log" "isp-system-service/conf" "isp-system-service/entity" + + "github.com/pkg/errors" + "github.com/txix-open/isp-kit/log" ) +const adminAppId = 1 + type Transaction interface { CreateDomain(ctx context.Context, name string, desc string, systemId int) (*entity.Domain, error) - CreateService(ctx context.Context, name string, desc string, domainId int) (*entity.Service, error) - CreateApplication(ctx context.Context, name string, desc string, serviceId int, appType string) (*entity.Application, error) + CreateAppGroup(ctx context.Context, name string, desc string, domainId int) (*entity.AppGroup, error) + CreateApplication(ctx context.Context, id int, name string, desc string, appGroupId int, appType string) (*entity.Application, error) UpsertAccessList(ctx context.Context, e entity.AccessList) (int, error) SaveToken(ctx context.Context, token string, appId int, expireTime int) (*entity.Token, error) GetTokenById(ctx context.Context, token string) (*entity.Token, error) @@ -77,12 +80,12 @@ func (s Service) transaction(ctx context.Context, tx Transaction) error { return errors.WithMessage(err, "create domain") } - service, err := tx.CreateService(ctx, "rootService", "", domain.Id) + service, err := tx.CreateAppGroup(ctx, "rootService", "", domain.Id) if err != nil { return errors.WithMessage(err, "create service") } - app, err := tx.CreateApplication(ctx, "admin", "", service.Id, "SYSTEM") + app, err := tx.CreateApplication(ctx, adminAppId, "admin", "", service.Id, "SYSTEM") if err != nil { return errors.WithMessage(err, "create application") } diff --git a/service/interfaces.go b/service/interfaces.go index 064ebf3..6cb94cf 100644 --- a/service/interfaces.go +++ b/service/interfaces.go @@ -21,18 +21,19 @@ type DomainRepo interface { type ApplicationRepo interface { GetApplicationById(ctx context.Context, id int) (*entity.Application, error) GetApplicationByIdList(ctx context.Context, idList []int) ([]entity.Application, error) - GetApplicationByServiceIdList(ctx context.Context, serviceIdList []int) ([]entity.Application, error) - GetApplicationByNameAndServiceId(ctx context.Context, name string, serviceId int) (*entity.Application, error) - CreateApplication(ctx context.Context, name string, desc string, serviceId int, appType string) (*entity.Application, error) + GetApplicationByAppGroupIdList(ctx context.Context, appGroupIdList []int) ([]entity.Application, error) + GetApplicationByNameAndAppGroupId(ctx context.Context, name string, appGroupId int) (*entity.Application, error) + CreateApplication(ctx context.Context, id int, name string, desc string, appGroupId int, appType string) (*entity.Application, error) UpdateApplication(ctx context.Context, id int, name string, description string) (*entity.Application, error) + NextApplicationId(ctx context.Context) (int, error) } -type ServiceRepo interface { - GetServiceById(ctx context.Context, id int) (*entity.Service, error) - GetServiceByIdList(ctx context.Context, idList []int) ([]entity.Service, error) - GetServiceByDomainId(ctx context.Context, domainIdList []int) ([]entity.Service, error) - GetServiceByNameAndDomainId(ctx context.Context, name string, domainId int) (*entity.Service, error) - CreateService(ctx context.Context, name string, desc string, domainId int) (*entity.Service, error) - UpdateService(ctx context.Context, id int, name string, description string) (*entity.Service, error) - DeleteService(ctx context.Context, idList []int) (int, error) +type AppGroupRepo interface { + GetAppGroupById(ctx context.Context, id int) (*entity.AppGroup, error) + GetAppGroupByIdList(ctx context.Context, idList []int) ([]entity.AppGroup, error) + GetAppGroupByDomainId(ctx context.Context, domainIdList []int) ([]entity.AppGroup, error) + GetAppGroupByNameAndDomainId(ctx context.Context, name string, domainId int) (*entity.AppGroup, error) + CreateAppGroup(ctx context.Context, name string, desc string, domainId int) (*entity.AppGroup, error) + UpdateAppGroup(ctx context.Context, id int, name string, description string) (*entity.AppGroup, error) + DeleteAppGroup(ctx context.Context, idList []int) (int, error) } diff --git a/service/secure/service.go b/service/secure/service.go index 39abbc9..6c630d0 100644 --- a/service/secure/service.go +++ b/service/secure/service.go @@ -4,9 +4,10 @@ import ( "context" "time" - "github.com/pkg/errors" "isp-system-service/domain" "isp-system-service/entity" + + "github.com/pkg/errors" ) type TokenRep interface { @@ -46,7 +47,7 @@ func (s Service) Authenticate(ctx context.Context, token string) (*domain.AuthDa return &domain.AuthData{ SystemId: authData.SystemId, DomainId: authData.DomainId, - ServiceId: authData.ServiceId, + ServiceId: authData.ApplicationGroupId, ApplicationId: authData.AppId, }, nil } diff --git a/service/service.go b/service/service.go index 06fd18c..fd0ac21 100644 --- a/service/service.go +++ b/service/service.go @@ -3,19 +3,20 @@ package service import ( "context" - "github.com/pkg/errors" "isp-system-service/domain" "isp-system-service/entity" + + "github.com/pkg/errors" ) type Service struct { domainRepo DomainRepo - serviceRepo ServiceRepo + serviceRepo AppGroupRepo } func NewService( domainRepo DomainRepo, - serviceRepo ServiceRepo, + serviceRepo AppGroupRepo, ) Service { return Service{ domainRepo: domainRepo, @@ -24,7 +25,7 @@ func NewService( } func (s Service) GetById(ctx context.Context, id int) (*domain.Service, error) { - serviceEntity, err := s.serviceRepo.GetServiceById(ctx, id) + serviceEntity, err := s.serviceRepo.GetAppGroupById(ctx, id) if err != nil { return nil, errors.WithMessage(err, "get service by id") } @@ -34,7 +35,7 @@ func (s Service) GetById(ctx context.Context, id int) (*domain.Service, error) { } func (s Service) GetByIdList(ctx context.Context, idList []int) ([]domain.Service, error) { - serviceEntity, err := s.serviceRepo.GetServiceByIdList(ctx, idList) + serviceEntity, err := s.serviceRepo.GetAppGroupByIdList(ctx, idList) if err != nil { return nil, errors.WithMessage(err, "get system by id list") } @@ -47,7 +48,7 @@ func (s Service) GetByIdList(ctx context.Context, idList []int) ([]domain.Servic } func (s Service) GetByDomainId(ctx context.Context, domainId int) ([]domain.Service, error) { - serviceEntity, err := s.serviceRepo.GetServiceByDomainId(ctx, []int{domainId}) + serviceEntity, err := s.serviceRepo.GetAppGroupByDomainId(ctx, []int{domainId}) if err != nil { return nil, errors.WithMessage(err, "get system by id list") } @@ -62,9 +63,9 @@ func (s Service) GetByDomainId(ctx context.Context, domainId int) ([]domain.Serv func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdateRequest) (*domain.Service, error) { req.DomainId = 1 // temporary use only 1 domain, soon domain entity will be removed - existed, err := s.serviceRepo.GetServiceByNameAndDomainId(ctx, req.Name, req.DomainId) + existed, err := s.serviceRepo.GetAppGroupByNameAndDomainId(ctx, req.Name, req.DomainId) switch { - case errors.Is(err, domain.ErrServiceNotFound): + case errors.Is(err, domain.ErrAppGroupNotFound): case err != nil: return nil, errors.WithMessage(err, "get service by name and domain_id") } @@ -79,7 +80,7 @@ func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdat return nil, domain.ErrDomainDuplicateName } - serviceEntity, err := s.serviceRepo.CreateService(ctx, req.Name, req.Description, req.DomainId) + serviceEntity, err := s.serviceRepo.CreateAppGroup(ctx, req.Name, req.Description, req.DomainId) if err != nil { return nil, errors.WithMessage(err, "create service") } @@ -92,12 +93,12 @@ func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdat return nil, domain.ErrDomainDuplicateName } - _, err = s.serviceRepo.GetServiceById(ctx, req.Id) + _, err = s.serviceRepo.GetAppGroupById(ctx, req.Id) if err != nil { return nil, errors.WithMessage(err, "get service by id") } - serviceEntity, err := s.serviceRepo.UpdateService(ctx, req.Id, req.Name, req.Description) + serviceEntity, err := s.serviceRepo.UpdateAppGroup(ctx, req.Id, req.Name, req.Description) if err != nil { return nil, errors.WithMessage(err, "update service") } @@ -107,7 +108,7 @@ func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdat } func (s Service) Delete(ctx context.Context, idList []int) (int, error) { - result, err := s.serviceRepo.DeleteService(ctx, idList) + result, err := s.serviceRepo.DeleteAppGroup(ctx, idList) if err != nil { return 0, errors.WithMessage(err, "delete service") } @@ -115,7 +116,7 @@ func (s Service) Delete(ctx context.Context, idList []int) (int, error) { return result, nil } -func (s Service) convertService(req entity.Service) domain.Service { +func (s Service) convertService(req entity.AppGroup) domain.Service { desc := "" if req.Description != nil { desc = *req.Description diff --git a/service/token.go b/service/token.go index 3354d15..5a37acf 100644 --- a/service/token.go +++ b/service/token.go @@ -36,7 +36,7 @@ type Token struct { tx TokenTxRunner appRepo ApplicationRepo domainRepo DomainRepo - serviceRepo ServiceRepo + serviceRepo AppGroupRepo tokenRepo TokenRepo } @@ -46,7 +46,7 @@ func NewToken( tx TokenTxRunner, appRepo ApplicationRepo, domainRepo DomainRepo, - serviceRepo ServiceRepo, + appGroupRepo AppGroupRepo, tokenRepo TokenRepo, ) Token { return Token{ @@ -55,7 +55,7 @@ func NewToken( tx: tx, appRepo: appRepo, domainRepo: domainRepo, - serviceRepo: serviceRepo, + serviceRepo: appGroupRepo, tokenRepo: tokenRepo, } } @@ -80,7 +80,7 @@ func (s Token) Create(ctx context.Context, req domain.TokenCreateRequest) (*doma return nil, errors.WithMessage(err, "get application by id") } - serviceEntity, err := s.serviceRepo.GetServiceById(ctx, applicationEntity.ServiceId) + serviceEntity, err := s.serviceRepo.GetAppGroupById(ctx, applicationEntity.ApplicationGroupId) if err != nil { return nil, errors.WithMessage(err, "get service by id") } diff --git a/tests/general_insert_db_test.go b/tests/general_insert_db_test.go index d29e26f..33bf508 100644 --- a/tests/general_insert_db_test.go +++ b/tests/general_insert_db_test.go @@ -1,8 +1,9 @@ package tests_test import ( - "github.com/txix-open/isp-kit/test/dbt" "isp-system-service/entity" + + "github.com/txix-open/isp-kit/test/dbt" ) func InsertDomain(db *dbt.TestDb, value entity.Domain) { @@ -15,9 +16,9 @@ func InsertDomain(db *dbt.TestDb, value entity.Domain) { db.Must().ExecNamed(q, value) } -func InsertService(db *dbt.TestDb, value entity.Service) { +func InsertAppGroup(db *dbt.TestDb, value entity.AppGroup) { q := ` - INSERT INTO service + INSERT INTO application_group (id, name, description, domain_id, created_at, updated_at) VALUES (:id, :name, :description, :domain_id, :created_at, :updated_at) @@ -28,9 +29,9 @@ func InsertService(db *dbt.TestDb, value entity.Service) { func InsertApplication(db *dbt.TestDb, value entity.Application) { q := ` INSERT INTO application - (id, name, description, service_id, created_at, updated_at) + (id, name, description, application_group_id, created_at, updated_at) VALUES - (:id, :name, :description, :service_id, :created_at, :updated_at) + (:id, :name, :description, :application_group_id, :created_at, :updated_at) ` db.Must().ExecNamed(q, value) } diff --git a/tests/secure_test.go b/tests/secure_test.go index b82cfd0..b5e51d6 100644 --- a/tests/secure_test.go +++ b/tests/secure_test.go @@ -2,20 +2,22 @@ package tests_test import ( "context" - "github.com/txix-open/isp-kit/dbx" "testing" "time" + "github.com/txix-open/isp-kit/dbx" + + "isp-system-service/assembly" + "isp-system-service/conf" + "isp-system-service/domain" + "isp-system-service/entity" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/txix-open/isp-kit/grpc/client" "github.com/txix-open/isp-kit/test" "github.com/txix-open/isp-kit/test/dbt" "github.com/txix-open/isp-kit/test/grpct" - "isp-system-service/assembly" - "isp-system-service/conf" - "isp-system-service/domain" - "isp-system-service/entity" ) func TestSecureSuite(t *testing.T) { @@ -44,11 +46,11 @@ func (s *SecureSuite) SetupSuite() { InsertDomain(s.testDb, entity.Domain{ Id: 3, Name: "test_domain", SystemId: 1, CreatedAt: createdTime, UpdatedAt: createdTime, }) - InsertService(s.testDb, entity.Service{ - Id: 5, Name: "test_service", DomainId: 3, CreatedAt: createdTime, UpdatedAt: createdTime, + InsertAppGroup(s.testDb, entity.AppGroup{ + Id: 5, Name: "test_application_group", DomainId: 3, CreatedAt: createdTime, UpdatedAt: createdTime, }) InsertApplication(s.testDb, entity.Application{ - Id: 7, Name: "test_application", ServiceId: 5, CreatedAt: createdTime, UpdatedAt: createdTime, + Id: 7, Name: "test_application", ApplicationGroupId: 5, CreatedAt: createdTime, UpdatedAt: createdTime, }) } diff --git a/transaction/manager.go b/transaction/manager.go index 0f6cd0f..fc76758 100644 --- a/transaction/manager.go +++ b/transaction/manager.go @@ -88,7 +88,7 @@ func (m Manager) TokenRevokeTx(ctx context.Context, msgTx func(ctx context.Conte type baselineTx struct { repository.Locker repository.Domain - repository.Service + repository.AppGroup repository.Application repository.AccessList repository.Token @@ -99,7 +99,7 @@ func (m Manager) BaselineTx(ctx context.Context, txTx func(ctx context.Context, return txTx(ctx, baselineTx{ Locker: repository.NewLocker(tx), Domain: repository.NewDomain(tx), - Service: repository.NewService(tx), + AppGroup: repository.NewAppGroup(tx), Application: repository.NewApplication(tx), AccessList: repository.NewAccessList(tx), Token: repository.NewToken(tx),