MSP-5186 Add /application/get_all endpoint
This commit is contained in:
1 parent
f8fa58a09c
commit
fa2f88c869
7 files changed
+182
No files matched your search
@@ -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)
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user