refactoring

This commit is contained in:
DeniskaRediska committed 2019-11-12 12:48:03 +03:00
1 parent f215008349
commit 0aec014161
13 files changed
+330 -311

No files matched your search

+50 -48
View File
@@ -9,11 +9,16 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"isp-system-service/domain"
"isp-system-service/entity"
"isp-system-service/model"
"isp-system-service/redis"
)
var Application applicationController
type applicationController struct{}
// GetApplications godoc
// @Tags application
// @Summary Получить список приложений
@@ -21,15 +26,15 @@ import (
// @Accept json
// @Produce json
// @Param body body []integer false "Массив идентификаторов приложений"
// @Success 200 {array} controller.AppWithToken
// @Success 200 {array} domain.AppWithToken
// @Failure 500 {object} structure.GrpcError
// @Router /application/get_applications [POST]
func GetApplications(list []int32) ([]*AppWithToken, error) {
func (c applicationController) GetApplications(list []int32) ([]*domain.AppWithToken, error) {
res, err := model.AppRep.GetApplications(list)
if err != nil {
return nil, err
}
return enrichWithTokens(res...)
return c.enrichWithTokens(res...)
}
// GetApplicationsByServiceId godoc
@@ -38,16 +43,16 @@ func GetApplications(list []int32) ([]*AppWithToken, error) {
// @Description Возвращает список приложений по запрошенныму идентификатору сервиса
// @Accept json
// @Produce json
// @Param body body controller.Identity true "Идентификатор серсиса"
// @Success 200 {array} controller.AppWithToken
// @Param body body domain.Identity true "Идентификатор серсиса"
// @Success 200 {array} domain.AppWithToken
// @Failure 500 {object} structure.GrpcError
// @Router /application/get_applications_by_service_id [POST]
func GetApplicationsByServiceId(identity Identity) ([]*AppWithToken, error) {
func (c applicationController) GetApplicationsByServiceId(identity domain.Identity) ([]*domain.AppWithToken, error) {
arr, err := model.AppRep.GetApplicationsByServiceId(identity.Id)
if err != nil {
return nil, err
}
return enrichWithTokens(arr...)
return c.enrichWithTokens(arr...)
}
// CreateUpdateApplication godoc
@@ -57,13 +62,13 @@ func GetApplicationsByServiceId(identity Identity) ([]*AppWithToken, error) {
// @Accept json
// @Produce json
// @Param body body entity.Application true "Объект приложения"
// @Success 200 {object} controller.AppWithToken
// @Success 200 {object} domain.AppWithToken
// @Failure 400 {object} structure.GrpcError
// @Failure 404 {object} structure.GrpcError
// @Failure 409 {object} structure.GrpcError
// @Failure 500 {object} structure.GrpcError
// @Router /application/create_update_application [POST]
func CreateUpdateApplication(app entity.Application) (*AppWithToken, error) {
func (c applicationController) CreateUpdateApplication(app entity.Application) (*domain.AppWithToken, error) {
existed, err := model.AppRep.GetApplicationByNameAndServiceId(app.Name, app.ServiceId)
if err != nil {
return nil, err
@@ -80,8 +85,8 @@ func CreateUpdateApplication(app entity.Application) (*AppWithToken, error) {
return nil, status.Errorf(codes.AlreadyExists, "Application with name %s already exists", app.Name)
}
app, e := model.AppRep.CreateApplication(app)
enrichWithTokens(app)
return &AppWithToken{App: app, Tokens: []entity.Token{}}, e
_, _ = c.enrichWithTokens(app)
return &domain.AppWithToken{App: app, Tokens: []entity.Token{}}, e
} else {
if existed != nil && existed.Id != app.Id {
return nil, status.Errorf(codes.AlreadyExists, "Application with name %s already exists", app.Name)
@@ -97,7 +102,7 @@ func CreateUpdateApplication(app entity.Application) (*AppWithToken, error) {
if e != nil {
return nil, e
}
arr, err := enrichWithTokens(app)
arr, err := c.enrichWithTokens(app)
if err != nil {
return nil, err
}
@@ -111,13 +116,13 @@ func CreateUpdateApplication(app entity.Application) (*AppWithToken, error) {
// @Description Возвращает описание приложения по его идентификатору
// @Accept json
// @Produce json
// @Param body body controller.Identity true "Идентификатор приложения"
// @Success 200 {object} controller.AppWithToken
// @Param body body domain.Identity true "Идентификатор приложения"
// @Success 200 {object} domain.AppWithToken
// @Failure 400 {object} structure.GrpcError
// @Failure 404 {object} structure.GrpcError
// @Failure 500 {object} structure.GrpcError
// @Router /application/get_application_by_id [POST]
func GetApplicationById(identity Identity) (*AppWithToken, error) {
func (c applicationController) GetApplicationById(identity domain.Identity) (*domain.AppWithToken, error) {
application, err := model.AppRep.GetApplicationById(identity.Id)
if err != nil {
return nil, err
@@ -125,7 +130,7 @@ func GetApplicationById(identity Identity) (*AppWithToken, error) {
if application == nil {
return nil, status.Errorf(codes.NotFound, "Application with id %d not found", identity.Id)
}
arr, err := enrichWithTokens(*application)
arr, err := c.enrichWithTokens(*application)
if err != nil {
return nil, err
}
@@ -139,13 +144,13 @@ func GetApplicationById(identity Identity) (*AppWithToken, error) {
// @Accept json
// @Produce json
// @Param body body []integer false "Массив идентификаторов приложений"
// @Success 200 {object} controller.AppWithToken
// @Success 200 {object} domain.DeleteResponse
// @Failure 400 {object} structure.GrpcError
// @Failure 500 {object} structure.GrpcError
// @Router /application/delete_applications [POST]
func DeleteApplications(list []int32) (DeleteResponse, error) {
func (applicationController) DeleteApplications(list []int32) (domain.DeleteResponse, error) {
if len(list) == 0 {
return DeleteResponse{}, status.Errorf(codes.InvalidArgument, "At least one id are required")
return domain.DeleteResponse{}, status.Errorf(codes.InvalidArgument, "At least one id are required")
}
var (
@@ -157,7 +162,7 @@ func DeleteApplications(list []int32) (DeleteResponse, error) {
appRep model.AppRepository, tokenRep model.TokenRepository, accessRep model.AccessListRepository) error {
for _, appId := range list {
if _, err := revokeTokensForApp(Identity{appId}, &tokenRep); err != nil {
if _, err := Token.revokeTokensForApp(domain.Identity{appId}, &tokenRep); err != nil {
return err
}
}
@@ -166,18 +171,16 @@ func DeleteApplications(list []int32) (DeleteResponse, error) {
return err
}
redisDelRequest := make([]string, 0)
for _, id := range list {
accessList, err := accessRep.DeleteById(id)
if err != nil {
return err
}
redisDelArray := make([]string, len(accessList))
for i, access := range accessList {
redisDelArray[i] = fmt.Sprintf("%d|%s", id, access.Method)
}
redisDelRequest = append(redisDelRequest, redisDelArray...)
accessList, err := accessRep.DeleteByIdList(list)
if err != nil {
return err
}
redisDelRequest := make([]string, len(accessList))
for i, access := range accessList {
redisDelRequest[i] = fmt.Sprintf("%d|%s", access.AppId, access.Method)
}
if len(redisDelRequest) > 0 {
if _, err := redis.Client.Get().UseDb(rdLib.ApplicationPermissionDb, func(p rd.Pipeliner) error {
if _, err := p.Del(redisDelRequest...).Result(); err != nil {
@@ -191,9 +194,9 @@ func DeleteApplications(list []int32) (DeleteResponse, error) {
}
return nil
}); err != nil {
return DeleteResponse{}, err
return domain.DeleteResponse{}, err
} else {
return DeleteResponse{Deleted: count}, nil
return domain.DeleteResponse{Deleted: count}, nil
}
}
@@ -203,10 +206,10 @@ func DeleteApplications(list []int32) (DeleteResponse, error) {
// @Description Возвращает описание взаимосвязей сервисов и приложений
// @Accept json
// @Produce json
// @Success 200 {array} controller.DomainWithServices
// @Success 200 {array} domain.DomainWithServices
// @Failure 500 {object} structure.GrpcError
// @Router /application/get_system_tree [POST]
func GetSystemTree(md metadata.MD) ([]*DomainWithServices, error) {
func (applicationController) GetSystemTree(md metadata.MD) ([]*domain.DomainWithServices, error) {
sysId, err := utils.ResolveMetadataIdentity(utils.SystemIdHeader, md)
if err != nil {
return nil, err
@@ -218,14 +221,14 @@ func GetSystemTree(md metadata.MD) ([]*DomainWithServices, error) {
l := len(domains)
if l == 0 {
return []*DomainWithServices{}, nil
return []*domain.DomainWithServices{}, nil
}
idList := make([]int32, l)
domainsMap := make(map[int32]*DomainWithServices, l)
res := make([]*DomainWithServices, l)
domainsMap := make(map[int32]*domain.DomainWithServices, l)
res := make([]*domain.DomainWithServices, l)
for i, d := range domains {
idList[i] = d.Id
dws := &DomainWithServices{Id: d.Id, Name: d.Name, Description: d.Description, Services: make([]*ServiceWithApps, 0)}
dws := &domain.DomainWithServices{Id: d.Id, Name: d.Name, Description: d.Description, Services: make([]*domain.ServiceWithApps, 0)}
domainsMap[d.Id] = dws
res[i] = dws
}
@@ -236,11 +239,11 @@ func GetSystemTree(md metadata.MD) ([]*DomainWithServices, error) {
l = len(services)
idList = make([]int32, l)
servicesMap := make(map[int32]*ServiceWithApps, l)
servicesMap := make(map[int32]*domain.ServiceWithApps, l)
for i, s := range services {
idList[i] = s.Id
d := domainsMap[s.DomainId]
swa := &ServiceWithApps{Id: s.Id, Name: s.Name, Description: s.Description, Apps: make([]*SimpleApp, 0)}
swa := &domain.ServiceWithApps{Id: s.Id, Name: s.Name, Description: s.Description, Apps: make([]*domain.SimpleApp, 0)}
d.Services = append(d.Services, swa)
servicesMap[s.Id] = swa
}
@@ -251,7 +254,7 @@ func GetSystemTree(md metadata.MD) ([]*DomainWithServices, error) {
for _, app := range apps {
s := servicesMap[app.ServiceId]
s.Apps = append(s.Apps, &SimpleApp{Id: app.Id,
s.Apps = append(s.Apps, &domain.SimpleApp{Id: app.Id,
Name: app.Name,
Type: app.Type,
Description: app.Description,
@@ -262,17 +265,17 @@ func GetSystemTree(md metadata.MD) ([]*DomainWithServices, error) {
return res, nil
}
func enrichWithTokens(apps ...entity.Application) ([]*AppWithToken, error) {
func (applicationController) enrichWithTokens(apps ...entity.Application) ([]*domain.AppWithToken, error) {
l := len(apps)
if len(apps) == 0 {
return []*AppWithToken{}, nil
return []*domain.AppWithToken{}, nil
}
idList := make([]int32, l)
appMap := make(map[int32]*AppWithToken, l)
enriched := make([]*AppWithToken, l)
appMap := make(map[int32]*domain.AppWithToken, l)
enriched := make([]*domain.AppWithToken, l)
for i, a := range apps {
idList[i] = a.Id
awt := &AppWithToken{App: a, Tokens: make([]entity.Token, 0)}
awt := &domain.AppWithToken{App: a, Tokens: make([]entity.Token, 0)}
appMap[a.Id] = awt
enriched[i] = awt
}
@@ -285,5 +288,4 @@ func enrichWithTokens(apps ...entity.Application) ([]*AppWithToken, error) {
app.Tokens = append(app.Tokens, v)
}
return enriched, nil
}