MSP-3428 CRUD application_group, service -> application_group
This commit is contained in:
1 parent
7108122102
commit
bef400d0d9
26 files changed
+559
-395
No files matched your search
+51
-51
@@ -11,8 +11,8 @@ import (
|
||||
type IApplicationApplicationRep 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)
|
||||
GetApplicationByApplicationGroupIdList(ctx context.Context, serviceIdList []int) ([]entity.Application, error)
|
||||
GetApplicationByNameAndApplicationGroupId(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)
|
||||
UpdateApplication(ctx context.Context, id int, name string, description string) (*entity.Application, error)
|
||||
}
|
||||
@@ -22,8 +22,8 @@ type IApplicationTokenRep interface {
|
||||
}
|
||||
|
||||
type IApplicationServiceRep interface {
|
||||
GetServiceById(ctx context.Context, id int) (*entity.Service, error)
|
||||
GetServiceByDomainId(ctx context.Context, domainIdList []int) ([]entity.Service, error)
|
||||
GetApplicationGroupById(ctx context.Context, id int) (*entity.ApplicationGroup, error)
|
||||
GetApplicationGroupByDomainId(ctx context.Context, domainIdList []int) ([]entity.ApplicationGroup, error)
|
||||
}
|
||||
|
||||
type IApplicationDomainRep interface {
|
||||
@@ -42,7 +42,7 @@ type Application struct {
|
||||
tx IApplicationTxRunner
|
||||
applicationRep IApplicationApplicationRep
|
||||
domainRep IApplicationDomainRep
|
||||
serviceRep IApplicationServiceRep
|
||||
groupRep IApplicationServiceRep
|
||||
tokenRep IApplicationTokenRep
|
||||
}
|
||||
|
||||
@@ -50,14 +50,14 @@ func NewApplication(
|
||||
tx IApplicationTxRunner,
|
||||
applicationRep IApplicationApplicationRep,
|
||||
domainRep IApplicationDomainRep,
|
||||
serviceRep IApplicationServiceRep,
|
||||
groupRep IApplicationServiceRep,
|
||||
tokenRep IApplicationTokenRep,
|
||||
) Application {
|
||||
return Application{
|
||||
tx: tx,
|
||||
applicationRep: applicationRep,
|
||||
domainRep: domainRep,
|
||||
serviceRep: serviceRep,
|
||||
groupRep: groupRep,
|
||||
tokenRep: tokenRep,
|
||||
}
|
||||
}
|
||||
@@ -85,26 +85,26 @@ func (s Application) GetByIdList(ctx context.Context, idList []int) ([]*domain.A
|
||||
return s.EnrichWithTokens(ctx, res)
|
||||
}
|
||||
|
||||
func (s Application) GetByServiceId(ctx context.Context, id int) ([]*domain.ApplicationWithTokens, error) {
|
||||
arr, err := s.applicationRep.GetApplicationByServiceIdList(ctx, []int{id})
|
||||
func (s Application) GetByApplicationGroupId(ctx context.Context, id int) ([]*domain.ApplicationWithTokens, error) {
|
||||
arr, err := s.applicationRep.GetApplicationByApplicationGroupIdList(ctx, []int{id})
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get application by service_id")
|
||||
return nil, errors.WithMessage(err, "get application by application_group_id")
|
||||
}
|
||||
|
||||
return s.EnrichWithTokens(ctx, arr)
|
||||
}
|
||||
|
||||
func (s Application) SystemTree(ctx context.Context, systemId int) ([]*domain.DomainWithService, error) {
|
||||
func (s Application) SystemTree(ctx context.Context, systemId int) ([]*domain.DomainWithApplicationGroup, error) {
|
||||
domainEntityList, err := s.domainRep.GetDomainBySystemId(ctx, systemId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get domains by system_id")
|
||||
}
|
||||
if len(domainEntityList) == 0 {
|
||||
return []*domain.DomainWithService{}, nil
|
||||
return []*domain.DomainWithApplicationGroup{}, nil
|
||||
}
|
||||
|
||||
result := make([]*domain.DomainWithService, len(domainEntityList))
|
||||
resultByDomainId := make(map[int]*domain.DomainWithService, len(domainEntityList))
|
||||
result := make([]*domain.DomainWithApplicationGroup, len(domainEntityList))
|
||||
resultByDomainId := make(map[int]*domain.DomainWithApplicationGroup, len(domainEntityList))
|
||||
domainIdList := make([]int, len(domainEntityList))
|
||||
for i, domainEntity := range domainEntityList {
|
||||
domainIdList[i] = domainEntity.Id
|
||||
@@ -112,52 +112,52 @@ func (s Application) SystemTree(ctx context.Context, systemId int) ([]*domain.Do
|
||||
if domainEntity.Description != nil {
|
||||
description = *domainEntity.Description
|
||||
}
|
||||
r := &domain.DomainWithService{
|
||||
Id: domainEntity.Id,
|
||||
Name: domainEntity.Name,
|
||||
Description: description,
|
||||
Services: make([]*domain.ServiceWithApps, 0),
|
||||
r := &domain.DomainWithApplicationGroup{
|
||||
Id: domainEntity.Id,
|
||||
Name: domainEntity.Name,
|
||||
Description: description,
|
||||
ApplicationGroup: make([]*domain.ApplicationGroupWithApps, 0),
|
||||
}
|
||||
resultByDomainId[domainEntity.Id] = r
|
||||
result[i] = r
|
||||
}
|
||||
|
||||
serviceEntityList, err := s.serviceRep.GetServiceByDomainId(ctx, domainIdList)
|
||||
applicationGroupEntityList, err := s.groupRep.GetApplicationGroupByDomainId(ctx, domainIdList)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get service by domain_id")
|
||||
return nil, errors.WithMessage(err, "get application group by domain_id")
|
||||
}
|
||||
|
||||
serviceIdList := make([]int, len(serviceEntityList))
|
||||
resultServiceByServiceId := make(map[int]*domain.ServiceWithApps, len(serviceEntityList))
|
||||
for i, serviceEntity := range serviceEntityList {
|
||||
serviceIdList[i] = serviceEntity.Id
|
||||
applicationGroupIdList := make([]int, len(applicationGroupEntityList))
|
||||
resultApplicationGroupById := make(map[int]*domain.ApplicationGroupWithApps, len(applicationGroupEntityList))
|
||||
for i, applicationGroupEntity := range applicationGroupEntityList {
|
||||
applicationGroupIdList[i] = applicationGroupEntity.Id
|
||||
description := ""
|
||||
if serviceEntity.Description != nil {
|
||||
description = *serviceEntity.Description
|
||||
if applicationGroupEntity.Description != nil {
|
||||
description = *applicationGroupEntity.Description
|
||||
}
|
||||
resultService := &domain.ServiceWithApps{
|
||||
Id: serviceEntity.Id,
|
||||
Name: serviceEntity.Name,
|
||||
resultApplicationGroup := &domain.ApplicationGroupWithApps{
|
||||
Id: applicationGroupEntity.Id,
|
||||
Name: applicationGroupEntity.Name,
|
||||
Description: description,
|
||||
Apps: make([]*domain.ApplicationSimple, 0),
|
||||
}
|
||||
r := resultByDomainId[serviceEntity.DomainId]
|
||||
r.Services = append(r.Services, resultService)
|
||||
resultServiceByServiceId[serviceEntity.Id] = resultService
|
||||
r := resultByDomainId[applicationGroupEntity.DomainId]
|
||||
r.ApplicationGroup = append(r.ApplicationGroup, resultApplicationGroup)
|
||||
resultApplicationGroupById[applicationGroupEntity.Id] = resultApplicationGroup
|
||||
}
|
||||
|
||||
applicationEntityList, err := s.applicationRep.GetApplicationByServiceIdList(ctx, serviceIdList)
|
||||
applicationEntityList, err := s.applicationRep.GetApplicationByApplicationGroupIdList(ctx, applicationGroupIdList)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "get application by service_id")
|
||||
return nil, errors.WithMessagef(err, "get application by application_group_id")
|
||||
}
|
||||
|
||||
for _, applicationEntity := range applicationEntityList {
|
||||
resultService := resultServiceByServiceId[applicationEntity.ServiceId]
|
||||
resultApplicationGroup := resultApplicationGroupById[applicationEntity.ApplicationGroupId]
|
||||
description := ""
|
||||
if applicationEntity.Description != nil {
|
||||
description = *applicationEntity.Description
|
||||
}
|
||||
resultService.Apps = append(resultService.Apps, &domain.ApplicationSimple{
|
||||
resultApplicationGroup.Apps = append(resultApplicationGroup.Apps, &domain.ApplicationSimple{
|
||||
Id: applicationEntity.Id,
|
||||
Name: applicationEntity.Name,
|
||||
Type: applicationEntity.Type,
|
||||
@@ -170,16 +170,16 @@ 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.applicationRep.GetApplicationByNameAndServiceId(ctx, req.Name, req.ServiceId)
|
||||
existed, err := s.applicationRep.GetApplicationByNameAndApplicationGroupId(ctx, req.Name, req.ApplicationGroupId)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrApplicationNotFound):
|
||||
case err != nil:
|
||||
return nil, errors.WithMessage(err, "get application by name and service_id")
|
||||
return nil, errors.WithMessage(err, "get application by name and application_group_id")
|
||||
}
|
||||
|
||||
_, err = s.serviceRep.GetServiceById(ctx, req.ServiceId)
|
||||
_, err = s.groupRep.GetApplicationGroupById(ctx, req.ApplicationGroupId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get service by id")
|
||||
return nil, errors.WithMessage(err, "get application group by id")
|
||||
}
|
||||
|
||||
if req.Id == 0 {
|
||||
@@ -187,12 +187,12 @@ func (s Application) CreateUpdate(ctx context.Context, req domain.ApplicationCre
|
||||
return nil, domain.ErrApplicationDuplicateName
|
||||
}
|
||||
|
||||
_, err = s.applicationRep.GetApplicationByNameAndServiceId(ctx, req.Name, req.ServiceId)
|
||||
_, err = s.applicationRep.GetApplicationByNameAndApplicationGroupId(ctx, req.Name, req.ApplicationGroupId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get application by name and service_id")
|
||||
return nil, errors.WithMessage(err, "get application by name and application_group_id")
|
||||
}
|
||||
|
||||
app, err := s.applicationRep.CreateApplication(ctx, req.Name, req.Description, req.ServiceId, req.Type)
|
||||
app, err := s.applicationRep.CreateApplication(ctx, req.Name, req.Description, req.ApplicationGroupId, req.Type)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create application")
|
||||
}
|
||||
@@ -283,12 +283,12 @@ func (s Application) convertApplication(req entity.Application) domain.Applicati
|
||||
}
|
||||
|
||||
return domain.Application{
|
||||
Id: req.Id,
|
||||
Name: req.Name,
|
||||
Description: desc,
|
||||
ServiceId: req.ServiceId,
|
||||
Type: req.Type,
|
||||
CreatedAt: req.CreatedAt,
|
||||
UpdatedAt: req.UpdatedAt,
|
||||
Id: req.Id,
|
||||
Name: req.Name,
|
||||
Description: desc,
|
||||
ApplicationGroupId: req.ApplicationGroupId,
|
||||
Type: req.Type,
|
||||
CreatedAt: req.CreatedAt,
|
||||
UpdatedAt: req.UpdatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
)
|
||||
|
||||
type IApplicationGroupRep interface {
|
||||
GetApplicationGroupById(ctx context.Context, id int) (*entity.ApplicationGroup, error)
|
||||
GetApplicationGroupByIdList(ctx context.Context, idList []int) ([]entity.ApplicationGroup, error)
|
||||
GetApplicationGroupByDomainId(ctx context.Context, domainIdList []int) ([]entity.ApplicationGroup, error)
|
||||
GetSApplicationGroupByNameAndDomainId(ctx context.Context, name string, domainId int) (*entity.ApplicationGroup, error)
|
||||
CreateApplicationGroup(ctx context.Context, name string, desc string, domainId int) (*entity.ApplicationGroup, error)
|
||||
UpdateApplicationGroup(ctx context.Context, id int, name string, description string) (*entity.ApplicationGroup, error)
|
||||
DeleteApplicationGroup(ctx context.Context, idList []int) (int, error)
|
||||
}
|
||||
|
||||
type IApplicationGroupDomainRep interface {
|
||||
GetDomainById(ctx context.Context, id int) (*entity.Domain, error)
|
||||
}
|
||||
|
||||
type ApplicationGroup struct {
|
||||
domainRep IApplicationGroupDomainRep
|
||||
groupRep IApplicationGroupRep
|
||||
}
|
||||
|
||||
func NewApplicationGroup(
|
||||
domainRep IApplicationGroupDomainRep,
|
||||
groupRep IApplicationGroupRep,
|
||||
) ApplicationGroup {
|
||||
return ApplicationGroup{
|
||||
domainRep: domainRep,
|
||||
groupRep: groupRep,
|
||||
}
|
||||
}
|
||||
|
||||
func (s ApplicationGroup) GetById(ctx context.Context, id int) (*domain.ApplicationGroup, error) {
|
||||
applicationGroupEntity, err := s.groupRep.GetApplicationGroupById(ctx, id)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "get application group by id")
|
||||
}
|
||||
|
||||
result := s.convertApplicationGroup(*applicationGroupEntity)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s ApplicationGroup) GetByIdList(ctx context.Context, idList []int) ([]domain.ApplicationGroup, error) {
|
||||
applicationGroupEntity, err := s.groupRep.GetApplicationGroupByIdList(ctx, idList)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get system by id list")
|
||||
}
|
||||
|
||||
result := make([]domain.ApplicationGroup, len(applicationGroupEntity))
|
||||
for i := range applicationGroupEntity {
|
||||
result[i] = s.convertApplicationGroup(applicationGroupEntity[i])
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s ApplicationGroup) GetByDomainId(ctx context.Context, domainId int) ([]domain.ApplicationGroup, error) {
|
||||
applicationGroupEntity, err := s.groupRep.GetApplicationGroupByDomainId(ctx, []int{domainId})
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get system by id list")
|
||||
}
|
||||
|
||||
result := make([]domain.ApplicationGroup, len(applicationGroupEntity))
|
||||
for i := range applicationGroupEntity {
|
||||
result[i] = s.convertApplicationGroup(applicationGroupEntity[i])
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s ApplicationGroup) CreateUpdate(ctx context.Context, req domain.ApplicationGroupCreateUpdateRequest) (*domain.ApplicationGroup, error) {
|
||||
req.DomainId = 1 // Хардкодом проставляем domainId = 1
|
||||
|
||||
existed, err := s.groupRep.GetSApplicationGroupByNameAndDomainId(ctx, req.Name, req.DomainId)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrApplicationGroupNotFound):
|
||||
case err != nil:
|
||||
return nil, errors.WithMessage(err, "get application group by name and domain_id")
|
||||
}
|
||||
|
||||
_, err = s.domainRep.GetDomainById(ctx, req.DomainId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get domain by id")
|
||||
}
|
||||
|
||||
if req.Id == 0 {
|
||||
if existed != nil {
|
||||
return nil, domain.ErrDomainDuplicateName
|
||||
}
|
||||
|
||||
applicationGroupEntity, err := s.groupRep.CreateApplicationGroup(ctx, req.Name, req.Description, req.DomainId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create application group")
|
||||
}
|
||||
|
||||
result := s.convertApplicationGroup(*applicationGroupEntity)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
if existed != nil && existed.Id != req.Id {
|
||||
return nil, domain.ErrDomainDuplicateName
|
||||
}
|
||||
|
||||
_, err = s.groupRep.GetApplicationGroupById(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get application group by id")
|
||||
}
|
||||
|
||||
applicationGroupEntity, err := s.groupRep.UpdateApplicationGroup(ctx, req.Id, req.Name, req.Description)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "update application group")
|
||||
}
|
||||
|
||||
result := s.convertApplicationGroup(*applicationGroupEntity)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s ApplicationGroup) Delete(ctx context.Context, idList []int) (int, error) {
|
||||
result, err := s.groupRep.DeleteApplicationGroup(ctx, idList)
|
||||
if err != nil {
|
||||
return 0, errors.WithMessage(err, "delete application group")
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s ApplicationGroup) convertApplicationGroup(req entity.ApplicationGroup) domain.ApplicationGroup {
|
||||
desc := ""
|
||||
if req.Description != nil {
|
||||
desc = *req.Description
|
||||
}
|
||||
result := domain.ApplicationGroup{
|
||||
Id: req.Id,
|
||||
Name: req.Name,
|
||||
Description: desc,
|
||||
DomainId: req.DomainId,
|
||||
CreatedAt: req.CreatedAt,
|
||||
UpdatedAt: req.UpdatedAt,
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -17,22 +17,22 @@ type AccessListRep interface {
|
||||
GetAccessListByAppIdAndMethod(ctx context.Context, appId int, method string) (*entity.AccessList, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
type ApplicationGroup struct {
|
||||
tokenRep TokenRep
|
||||
accessListRep AccessListRep
|
||||
}
|
||||
|
||||
func NewService(
|
||||
func NewApplicationGroup(
|
||||
tokenRep TokenRep,
|
||||
accessListRep AccessListRep,
|
||||
) Service {
|
||||
return Service{
|
||||
) ApplicationGroup {
|
||||
return ApplicationGroup{
|
||||
tokenRep: tokenRep,
|
||||
accessListRep: accessListRep,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Service) Authenticate(ctx context.Context, token string) (*domain.AuthData, error) {
|
||||
func (s ApplicationGroup) Authenticate(ctx context.Context, token string) (*domain.AuthData, error) {
|
||||
authData, err := s.tokenRep.AuthDataByToken(ctx, token)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get auth data by token")
|
||||
@@ -44,14 +44,14 @@ func (s Service) Authenticate(ctx context.Context, token string) (*domain.AuthDa
|
||||
}
|
||||
|
||||
return &domain.AuthData{
|
||||
SystemId: authData.SystemId,
|
||||
DomainId: authData.DomainId,
|
||||
ServiceId: authData.ServiceId,
|
||||
ApplicationId: authData.AppId,
|
||||
SystemId: authData.SystemId,
|
||||
DomainId: authData.DomainId,
|
||||
ApplicationGroupId: authData.ApplicationGroupIdId,
|
||||
ApplicationId: authData.AppId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Service) Authorize(ctx context.Context, appId int, endpoint string) (bool, error) {
|
||||
func (s ApplicationGroup) Authorize(ctx context.Context, appId int, endpoint string) (bool, error) {
|
||||
accessList, err := s.accessListRep.GetAccessListByAppIdAndMethod(ctx, appId, endpoint)
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "get access list by app_id and method")
|
||||
@@ -1,145 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
)
|
||||
|
||||
type IServiceServiceRep 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 IServiceDomainRep interface {
|
||||
GetDomainById(ctx context.Context, id int) (*entity.Domain, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
domainRep IServiceDomainRep
|
||||
serviceRep IServiceServiceRep
|
||||
}
|
||||
|
||||
func NewService(
|
||||
domainRep IServiceDomainRep,
|
||||
serviceRep IServiceServiceRep,
|
||||
) Service {
|
||||
return Service{
|
||||
domainRep: domainRep,
|
||||
serviceRep: serviceRep,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Service) GetById(ctx context.Context, id int) (*domain.Service, error) {
|
||||
serviceEntity, err := s.serviceRep.GetServiceById(ctx, id)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "get service by id")
|
||||
}
|
||||
|
||||
result := s.convertService(*serviceEntity)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s Service) GetByIdList(ctx context.Context, idList []int) ([]domain.Service, error) {
|
||||
serviceEntity, err := s.serviceRep.GetServiceByIdList(ctx, idList)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get system by id list")
|
||||
}
|
||||
|
||||
result := make([]domain.Service, len(serviceEntity))
|
||||
for i := range serviceEntity {
|
||||
result[i] = s.convertService(serviceEntity[i])
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Service) GetByDomainId(ctx context.Context, domainId int) ([]domain.Service, error) {
|
||||
serviceEntity, err := s.serviceRep.GetServiceByDomainId(ctx, []int{domainId})
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get system by id list")
|
||||
}
|
||||
|
||||
result := make([]domain.Service, len(serviceEntity))
|
||||
for i := range serviceEntity {
|
||||
result[i] = s.convertService(serviceEntity[i])
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdateRequest) (*domain.Service, error) {
|
||||
existed, err := s.serviceRep.GetServiceByNameAndDomainId(ctx, req.Name, req.DomainId)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrServiceNotFound):
|
||||
case err != nil:
|
||||
return nil, errors.WithMessage(err, "get service by name and domain_id")
|
||||
}
|
||||
|
||||
_, err = s.domainRep.GetDomainById(ctx, req.DomainId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get domain by id")
|
||||
}
|
||||
|
||||
if req.Id == 0 {
|
||||
if existed != nil {
|
||||
return nil, domain.ErrDomainDuplicateName
|
||||
}
|
||||
|
||||
serviceEntity, err := s.serviceRep.CreateService(ctx, req.Name, req.Description, req.DomainId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create service")
|
||||
}
|
||||
|
||||
result := s.convertService(*serviceEntity)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
if existed != nil && existed.Id != req.Id {
|
||||
return nil, domain.ErrDomainDuplicateName
|
||||
}
|
||||
|
||||
_, err = s.serviceRep.GetServiceById(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get service by id")
|
||||
}
|
||||
|
||||
serviceEntity, err := s.serviceRep.UpdateService(ctx, req.Id, req.Name, req.Description)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "update service")
|
||||
}
|
||||
|
||||
result := s.convertService(*serviceEntity)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s Service) Delete(ctx context.Context, idList []int) (int, error) {
|
||||
result, err := s.serviceRep.DeleteService(ctx, idList)
|
||||
if err != nil {
|
||||
return 0, errors.WithMessage(err, "delete service")
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Service) convertService(req entity.Service) domain.Service {
|
||||
desc := ""
|
||||
if req.Description != nil {
|
||||
desc = *req.Description
|
||||
}
|
||||
result := domain.Service{
|
||||
Id: req.Id,
|
||||
Name: req.Name,
|
||||
Description: desc,
|
||||
DomainId: req.DomainId,
|
||||
CreatedAt: req.CreatedAt,
|
||||
UpdatedAt: req.UpdatedAt,
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
+19
-19
@@ -21,7 +21,7 @@ type ITokenApplicationRep interface {
|
||||
}
|
||||
|
||||
type ITokenServiceRep interface {
|
||||
GetServiceById(ctx context.Context, id int) (*entity.Service, error)
|
||||
GetApplicationGroupById(ctx context.Context, id int) (*entity.ApplicationGroup, error)
|
||||
}
|
||||
|
||||
type ITokenDomainRep interface {
|
||||
@@ -46,13 +46,13 @@ type ITokenTxRunner interface {
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
jwt ITokenSource
|
||||
appEnrich ITokenAppEnrich
|
||||
tx ITokenTxRunner
|
||||
appRep ITokenApplicationRep
|
||||
domainRep ITokenDomainRep
|
||||
serviceRep ITokenServiceRep
|
||||
tokenRep ITokenTokenRep
|
||||
jwt ITokenSource
|
||||
appEnrich ITokenAppEnrich
|
||||
tx ITokenTxRunner
|
||||
appRep ITokenApplicationRep
|
||||
domainRep ITokenDomainRep
|
||||
groupRep ITokenServiceRep
|
||||
tokenRep ITokenTokenRep
|
||||
}
|
||||
|
||||
func NewToken(
|
||||
@@ -61,17 +61,17 @@ func NewToken(
|
||||
tx ITokenTxRunner,
|
||||
appRep ITokenApplicationRep,
|
||||
domainRep ITokenDomainRep,
|
||||
serviceRep ITokenServiceRep,
|
||||
groupRep ITokenServiceRep,
|
||||
tokenRep ITokenTokenRep,
|
||||
) Token {
|
||||
return Token{
|
||||
appEnrich: appEnrich,
|
||||
jwt: jwtGenerate,
|
||||
tx: tx,
|
||||
appRep: appRep,
|
||||
domainRep: domainRep,
|
||||
serviceRep: serviceRep,
|
||||
tokenRep: tokenRep,
|
||||
appEnrich: appEnrich,
|
||||
jwt: jwtGenerate,
|
||||
tx: tx,
|
||||
appRep: appRep,
|
||||
domainRep: domainRep,
|
||||
groupRep: groupRep,
|
||||
tokenRep: tokenRep,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,12 +95,12 @@ func (s Token) Create(ctx context.Context, req domain.TokenCreateRequest) (*doma
|
||||
return nil, errors.WithMessagef(err, "get application by id")
|
||||
}
|
||||
|
||||
serviceEntity, err := s.serviceRep.GetServiceById(ctx, applicationEntity.ServiceId)
|
||||
applicationGroupIdEntity, err := s.groupRep.GetApplicationGroupById(ctx, applicationEntity.ApplicationGroupId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "get service by id")
|
||||
return nil, errors.WithMessagef(err, "get application groupId by id")
|
||||
}
|
||||
|
||||
_, err = s.domainRep.GetDomainById(ctx, serviceEntity.DomainId)
|
||||
_, err = s.domainRep.GetDomainById(ctx, applicationGroupIdEntity.DomainId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "get domain by id")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user