From fa2f88c869da21b7595c4cbe8181537fd0104c7f 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: Tue, 1 Apr 2025 20:25:42 +0300 Subject: [PATCH] MSP-5186 Add /application/get_all endpoint --- controller/application.go | 14 +++++ docs/swagger.yaml | 21 ++++++++ repository/application.go | 19 +++++++ routes/routes.go | 5 ++ service/application.go | 13 +++++ service/interfaces.go | 1 + tests/application_test.go | 109 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 182 insertions(+) create mode 100644 tests/application_test.go diff --git a/controller/application.go b/controller/application.go index c2defca..e272ea0 100644 --- a/controller/application.go +++ b/controller/application.go @@ -18,6 +18,7 @@ type ApplicationService interface { CreateUpdate(ctx context.Context, req domain.ApplicationCreateUpdateRequest) (*domain.ApplicationWithTokens, error) Delete(ctx context.Context, idList []int) (int, error) NextId(ctx context.Context) (int, error) + GetAll(ctx context.Context) ([]domain.Application, error) } type Application struct { @@ -162,3 +163,16 @@ func (c Application) Delete(ctx context.Context, req []int) (*domain.DeleteRespo func (c Application) NextId(ctx context.Context) (int, error) { return c.service.NextId(ctx) } + +// GetAll godoc +// @Tags application +// @Summary Получить список приложений +// @Description Возвращает список приложений +// @Accept json +// @Produce json +// @Success 200 {array} domain.Application +// @Failure 500 {object} domain.GrpcError +// @Router /application/get_all [POST] +func (c Application) GetAll(ctx context.Context) ([]domain.Application, error) { + return c.service.GetAll(ctx) +} diff --git a/docs/swagger.yaml b/docs/swagger.yaml index db4b8c9..3819452 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -459,6 +459,27 @@ paths: summary: Удалить приложения tags: - application + /application/get_all: + post: + consumes: + - application/json + description: Возвращает список приложений + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/domain.Application' + type: array + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Получить список приложений + tags: + - application /application/get_application_by_id: post: consumes: diff --git a/repository/application.go b/repository/application.go index 36d9e04..0e788f7 100644 --- a/repository/application.go +++ b/repository/application.go @@ -172,3 +172,22 @@ func (r Application) NextApplicationId(ctx context.Context) (int, error) { } return nextAppId, nil } + +func (r Application) GetAllApplications(ctx context.Context) ([]entity.Application, error) { + q, args, err := query.New(). + Select("id", "name", "description", "application_group_id", "type", "created_at", "updated_at"). + From("application"). + OrderBy("created_at DESC"). + ToSql() + if err != nil { + 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, "exec query %s", q) + } + + return result, nil +} diff --git a/routes/routes.go b/routes/routes.go index 9c886ef..07b1b3a 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -166,6 +166,11 @@ func applicationCluster(c Controllers) []cluster.EndpointDescriptor { Inner: true, Handler: c.Application.NextId, }, + { + Path: "system/application/get_all", + Inner: true, + Handler: c.Application.GetAll, + }, } } diff --git a/service/application.go b/service/application.go index ed2e31f..01ade9b 100644 --- a/service/application.go +++ b/service/application.go @@ -264,6 +264,19 @@ func (s Application) NextId(ctx context.Context) (int, error) { return nextId, nil } +func (s Application) GetAll(ctx context.Context) ([]domain.Application, error) { + apps, err := s.appRepo.GetAllApplications(ctx) + if err != nil { + return nil, errors.WithMessage(err, "get applications list") + } + + result := make([]domain.Application, 0, len(apps)) + for _, app := range apps { + result = append(result, s.convertApplication(app)) + } + return result, nil +} + func (s Application) convertApplication(req entity.Application) domain.Application { desc := "" if req.Description != nil { diff --git a/service/interfaces.go b/service/interfaces.go index 6cb94cf..5d3773c 100644 --- a/service/interfaces.go +++ b/service/interfaces.go @@ -26,6 +26,7 @@ type ApplicationRepo interface { 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) + GetAllApplications(ctx context.Context) ([]entity.Application, error) } type AppGroupRepo interface { diff --git a/tests/application_test.go b/tests/application_test.go new file mode 100644 index 0000000..810c5a8 --- /dev/null +++ b/tests/application_test.go @@ -0,0 +1,109 @@ +package tests_test + +import ( + "isp-system-service/assembly" + "isp-system-service/conf" + "isp-system-service/domain" + "isp-system-service/repository" + "testing" + "time" + + "github.com/stretchr/testify/suite" + "github.com/txix-open/isp-kit/dbx" + "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/fake" + "github.com/txix-open/isp-kit/test/grpct" +) + +func TestApplicationSuite(t *testing.T) { + t.Parallel() + suite.Run(t, &ApplicationSuite{}) +} + +type ApplicationSuite struct { + suite.Suite + test *test.Test + testDb *dbt.TestDb + domainRepo repository.Domain + appRepo repository.Application + appGroupRepo repository.AppGroup + api *client.Client +} + +func (s *ApplicationSuite) SetupTest() { + s.test, _ = test.New(s.T()) + + s.testDb = dbt.New(s.test, dbx.WithMigrationRunner("../migrations", s.test.Logger())) + + locator := assembly.NewLocator(s.testDb, s.test.Logger()) + config := locator.Config(conf.Remote{}) + _, s.api = grpct.TestServer(s.test, config.Handler) + s.domainRepo = repository.NewDomain(s.testDb) + s.appRepo = repository.NewApplication(s.testDb) + s.appGroupRepo = repository.NewAppGroup(s.testDb) +} + +func (s *ApplicationSuite) TestGetAllApplications() { + toInsert := fake.It[[]domain.Application](fake.MaxSliceSize(2), fake.MinSliceSize(2)) + expectedApps := s.insertApps(toInsert) + + result := []domain.Application{} + err := s.api.Invoke("system/application/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(expectedApps, result) +} + +func (s *ApplicationSuite) insertApps(toInsert []domain.Application) []domain.Application { + createdDomain, err := s.domainRepo.CreateDomain( + s.T().Context(), + fake.It[string](), + fake.It[string](), + 1, + ) + s.Require().NoError(err) + + appGroup, err := s.appGroupRepo.CreateAppGroup( + s.T().Context(), + fake.It[string](), + fake.It[string](), + createdDomain.Id, + ) + s.Require().NoError(err) + + toExpect := make([]domain.Application, 0, len(toInsert)) + for _, app := range toInsert { + createdApp, err := s.appRepo.CreateApplication( + s.T().Context(), + app.Id, + app.Name, + app.Description, + appGroup.Id, + app.Type, + ) + s.Require().NoError(err) + s.Require().NotEmpty(createdApp.CreatedAt) + s.Require().NotEmpty(createdApp.UpdatedAt) + + toExpect = append(toExpect, domain.Application{ + Id: createdApp.Id, + Name: createdApp.Name, + Description: *createdApp.Description, + ServiceId: createdApp.ApplicationGroupId, + Type: createdApp.Type, + CreatedAt: time.Time{}, + UpdatedAt: time.Time{}, + }) + } + return toExpect +}