From 4e85278a63fe5a070f639ce2e23814cced85893a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B8=D0=BD=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=20?= =?UTF-8?q?=D0=A2=D0=B8=D0=BC=D1=83=D1=80=20=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 3 Apr 2025 14:01:16 +0300 Subject: [PATCH] MSP-5186 Add /application_group/get_all endpoint --- controller/app_group.go | 15 +++++++++++++++ docs/swagger.yaml | 21 +++++++++++++++++++++ repository/app_group.go | 19 +++++++++++++++++++ routes/routes.go | 4 ++++ service/app_group.go | 12 ++++++++++++ service/interfaces.go | 1 + tests/app_group_test.go | 28 ++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+) diff --git a/controller/app_group.go b/controller/app_group.go index 171fcf0..9b50c28 100644 --- a/controller/app_group.go +++ b/controller/app_group.go @@ -15,6 +15,7 @@ type AppGroupService interface { Update(ctx context.Context, req domain.UpdateAppGroupRequest) (*domain.AppGroup, error) DeleteList(ctx context.Context, req domain.IdListRequest) (*domain.DeleteResponse, error) GetByIdList(ctx context.Context, idList []int) ([]domain.AppGroup, error) + GetAll(ctx context.Context) ([]domain.AppGroup, error) } type AppGroup struct { @@ -122,3 +123,17 @@ func (c AppGroup) DeleteList(ctx context.Context, req domain.IdListRequest) (*do func (c AppGroup) GetByIdList(ctx context.Context, req domain.IdListRequest) ([]domain.AppGroup, error) { return c.service.GetByIdList(ctx, req.IdList) } + +// GetAll godoc +// +// @Tags application_group +// @Summary Получить группы приложений +// @Description Возвращает все группы приложений +// @Accept json +// @Produce json +// @Success 200 {array} domain.AppGroup +// @Failure 500 {object} apierrors.Error +// @Router /application_group/get_all [POST] +func (c AppGroup) GetAll(ctx context.Context) ([]domain.AppGroup, error) { + return c.service.GetAll(ctx) +} diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 534432b..59d2f99 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -830,6 +830,27 @@ paths: summary: Удалить группы приложений tags: - application_group + /application_group/get_all: + post: + consumes: + - application/json + description: Возвращает все группы приложений + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/domain.AppGroup' + type: array + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/apierrors.Error' + summary: Получить группы приложений + tags: + - application_group /application_group/get_by_id_list: post: consumes: diff --git a/repository/app_group.go b/repository/app_group.go index 6203411..5ce5e30 100644 --- a/repository/app_group.go +++ b/repository/app_group.go @@ -163,3 +163,22 @@ func (r AppGroup) DeleteAppGroup(ctx context.Context, idList []int) (int, error) return int(rowsAffected), nil } + +func (r AppGroup) GetAllAppGroups(ctx context.Context) ([]entity.AppGroup, error) { + q, arg, err := query.New(). + Select("id", "name", "description", "domain_id", "created_at", "updated_at"). + From("application_group"). + OrderBy("created_at DESC"). + ToSql() + if err != nil { + return nil, errors.WithMessage(err, "build query") + } + + result := make([]entity.AppGroup, 0) + err = r.db.Select(ctx, &result, q, arg...) + if err != nil { + return nil, errors.WithMessagef(err, "exec query %s", q) + } + + return result, nil +} diff --git a/routes/routes.go b/routes/routes.go index 5200ba5..79f6d36 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -229,6 +229,10 @@ func applicationGroupCluster(c Controllers) []cluster.EndpointDescriptor { Path: "system/application_group/get_by_id_list", Inner: true, Handler: c.AppGroup.GetByIdList, + }, { + Path: "system/application_group/get_all", + Inner: true, + Handler: c.AppGroup.GetAll, }, } } diff --git a/service/app_group.go b/service/app_group.go index 03a8992..685770e 100644 --- a/service/app_group.go +++ b/service/app_group.go @@ -58,6 +58,18 @@ func (s AppGroup) GetByIdList(ctx context.Context, idList []int) ([]domain.AppGr return result, nil } +func (s AppGroup) GetAll(ctx context.Context) ([]domain.AppGroup, error) { + appGroups, err := s.repo.GetAllAppGroups(ctx) + if err != nil { + return nil, errors.WithMessage(err, "get all appGroups") + } + result := make([]domain.AppGroup, 0, len(appGroups)) + for _, appGroup := range appGroups { + result = append(result, s.convertAppGroup(appGroup)) + } + return result, nil +} + func (s AppGroup) convertAppGroup(appGroup entity.AppGroup) domain.AppGroup { return domain.AppGroup{ Id: appGroup.Id, diff --git a/service/interfaces.go b/service/interfaces.go index 2303026..ee8a5a6 100644 --- a/service/interfaces.go +++ b/service/interfaces.go @@ -34,6 +34,7 @@ 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) + GetAllAppGroups(ctx context.Context) ([]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) diff --git a/tests/app_group_test.go b/tests/app_group_test.go index 06fd16e..bc4eab0 100644 --- a/tests/app_group_test.go +++ b/tests/app_group_test.go @@ -247,6 +247,34 @@ func (s *AppGroupSuite) TestDeleteList() { s.Require().NoError(err) } +func (s *AppGroupSuite) TestGetAll() { + toGenerate := 10 + appGroups := make([]domain.AppGroup, 0, toGenerate) + for range 10 { + appGroup := s.createAppGroup() + appGroups = append(appGroups, domain.AppGroup{ + Id: appGroup.Id, + Name: appGroup.Name, + Description: appGroup.Description.String, + CreatedAt: time.Time{}, + UpdatedAt: time.Time{}, + }) + } + result := []domain.AppGroup{} + err := s.api.Invoke("system/application_group/get_all"). + JsonResponseBody(&result). + Do(s.T().Context()) + s.Require().NoError(err) + for i := range result { + s.Require().NotEmpty(result[i].CreatedAt) + result[i].CreatedAt = time.Time{} + + s.Require().NotEmpty(result[i].UpdatedAt) + result[i].UpdatedAt = time.Time{} + } + s.Require().ElementsMatch(appGroups, result) +} + func (s *AppGroupSuite) createAppGroup() *entity.AppGroup { appGroup, err := s.appGroupRepo.CreateAppGroup( s.T().Context(),