MSP-5186 Add /application/create_application endpoint
This commit is contained in:
1 parent
fa2f88c869
commit
e2ed71196b
7 files changed
+281
-12
No files matched your search
@@ -19,6 +19,7 @@ type ApplicationService interface {
|
||||
Delete(ctx context.Context, idList []int) (int, error)
|
||||
NextId(ctx context.Context) (int, error)
|
||||
GetAll(ctx context.Context) ([]domain.Application, error)
|
||||
Create(ctx context.Context, req domain.CreateApplicationRequest) (*domain.ApplicationWithTokens, error)
|
||||
}
|
||||
|
||||
type Application struct {
|
||||
@@ -117,7 +118,7 @@ func (c Application) CreateUpdate(ctx context.Context, req domain.ApplicationCre
|
||||
case errors.Is(err, domain.ErrApplicationDuplicateName):
|
||||
return nil, status.Errorf(codes.AlreadyExists, "application with name %s already exists", req.Name)
|
||||
case errors.Is(err, domain.ErrApplicationNotFound):
|
||||
return nil, status.Errorf(codes.NotFound, "Application with id %d not found", req.Id)
|
||||
return nil, status.Errorf(codes.NotFound, "application with id %d not found", req.Id)
|
||||
case err != nil:
|
||||
return nil, err
|
||||
default:
|
||||
@@ -176,3 +177,30 @@ func (c Application) NextId(ctx context.Context) (int, error) {
|
||||
func (c Application) GetAll(ctx context.Context) ([]domain.Application, error) {
|
||||
return c.service.GetAll(ctx)
|
||||
}
|
||||
|
||||
// Create godoc
|
||||
// @Tags application
|
||||
// @Summary Создать приложение
|
||||
// @Description Если приложение с такими идентификатором или связкой `applicationGroupId`-`name` существует, то возвращает ошибку
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.ApplicationCreateUpdateRequest true "Объект приложения"
|
||||
// @Success 200 {object} domain.ApplicationWithTokens
|
||||
// @Failure 400 {object} domain.GrpcError
|
||||
// @Failure 404 {object} domain.GrpcError
|
||||
// @Failure 409 {object} domain.GrpcError
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /application/create_application [POST]
|
||||
func (c Application) Create(ctx context.Context, req domain.CreateApplicationRequest) (*domain.ApplicationWithTokens, error) {
|
||||
result, err := c.service.Create(ctx, req)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrAppGroupNotFound):
|
||||
return nil, status.Errorf(codes.InvalidArgument, "application group with id %d not found", req.ApplicationGroupId)
|
||||
case errors.Is(err, domain.ErrApplicationDuplicateName):
|
||||
return nil, status.Errorf(codes.AlreadyExists, "application with name %s already exists", req.Name)
|
||||
case errors.Is(err, domain.ErrApplicationDuplicateId):
|
||||
return nil, status.Errorf(codes.AlreadyExists, "application with id %d already exists", req.Id)
|
||||
default:
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
@@ -133,6 +133,26 @@ definitions:
|
||||
authorized:
|
||||
type: boolean
|
||||
type: object
|
||||
domain.CreateApplicationRequest:
|
||||
properties:
|
||||
applicationGroupId:
|
||||
type: integer
|
||||
description:
|
||||
type: string
|
||||
id:
|
||||
type: integer
|
||||
name:
|
||||
type: string
|
||||
type:
|
||||
enum:
|
||||
- SYSTEM
|
||||
- MOBILE
|
||||
type: string
|
||||
required:
|
||||
- applicationGroupId
|
||||
- name
|
||||
- type
|
||||
type: object
|
||||
domain.DeleteResponse:
|
||||
properties:
|
||||
deleted:
|
||||
@@ -388,6 +408,45 @@ paths:
|
||||
summary: Настроить доступность метода для приложения
|
||||
tags:
|
||||
- accessList
|
||||
/application/create_application:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Если приложение с такими идентификатором или связкой `applicationGroupId`-`name`
|
||||
существует, то возвращает ошибку
|
||||
parameters:
|
||||
- description: Объект приложения
|
||||
in: body
|
||||
name: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/domain.CreateApplicationRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/domain.ApplicationWithTokens'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"404":
|
||||
description: Not Found
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"409":
|
||||
description: Conflict
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
summary: Создать приложение
|
||||
tags:
|
||||
- application
|
||||
/application/create_update_application:
|
||||
post:
|
||||
consumes:
|
||||
|
||||
@@ -4,6 +4,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ApplicationSystemType = "SYSTEM"
|
||||
ApplicationMobileType = "MOBILE"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
Id int
|
||||
Name string
|
||||
@@ -34,3 +39,11 @@ type ApplicationSimple struct {
|
||||
Type string
|
||||
Tokens []Token
|
||||
}
|
||||
|
||||
type CreateApplicationRequest struct {
|
||||
Id int
|
||||
Name string `validate:"required"`
|
||||
Description string
|
||||
ApplicationGroupId int `validate:"required"`
|
||||
Type string `validate:"required,oneof=SYSTEM MOBILE"`
|
||||
}
|
||||
+34
-10
@@ -4,12 +4,22 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/db"
|
||||
"github.com/txix-open/isp-kit/db/query"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
)
|
||||
|
||||
const (
|
||||
pgUniqueViolationErrorCode = "23505"
|
||||
pgFkViolationErrorCode = "23503"
|
||||
applicationPkConstrainName = "application_pkey"
|
||||
applicationUniqueNameConstrainName = "uq_name_application_group_id"
|
||||
applicationFkAppGroupConstraintName = "fk_application_group_id"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
@@ -104,19 +114,14 @@ func (r Application) CreateApplication(ctx context.Context, id int, name string,
|
||||
INSERT INTO application
|
||||
(id, name, description, application_group_id, type)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
RETURNING id, name, description, application_group_id, type, created_at, updated_at
|
||||
`
|
||||
result := entity.Application{}
|
||||
err := r.db.SelectRow(ctx, &result, q, id, name, desc, appGroupId, appType)
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, domain.ErrApplicationDuplicateId
|
||||
case err != nil:
|
||||
return nil, errors.WithMessagef(err, "exec query %s", q)
|
||||
default:
|
||||
return &result, nil
|
||||
if err != nil {
|
||||
return nil, r.handleCreateError(err, q)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r Application) UpdateApplication(
|
||||
@@ -191,3 +196,22 @@ func (r Application) GetAllApplications(ctx context.Context) ([]entity.Applicati
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r Application) handleCreateError(err error, q string) error {
|
||||
var pgErr *pgconn.PgError
|
||||
if !errors.As(err, &pgErr) {
|
||||
return errors.WithMessagef(err, "exec query %s", q)
|
||||
}
|
||||
switch {
|
||||
case pgErr.Code == pgUniqueViolationErrorCode:
|
||||
switch pgErr.ConstraintName {
|
||||
case applicationPkConstrainName:
|
||||
return domain.ErrApplicationDuplicateId
|
||||
case applicationUniqueNameConstrainName:
|
||||
return domain.ErrApplicationDuplicateName
|
||||
}
|
||||
case pgErr.Code == pgFkViolationErrorCode && pgErr.ConstraintName == applicationFkAppGroupConstraintName:
|
||||
return domain.ErrAppGroupNotFound
|
||||
}
|
||||
return errors.WithMessagef(err, "exec query %s", q)
|
||||
}
|
||||
@@ -171,6 +171,11 @@ func applicationCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
Inner: true,
|
||||
Handler: c.Application.GetAll,
|
||||
},
|
||||
{
|
||||
Path: "system/application/create_application",
|
||||
Inner: true,
|
||||
Handler: c.Application.Create,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -277,6 +277,26 @@ func (s Application) GetAll(ctx context.Context) ([]domain.Application, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Application) Create(ctx context.Context, req domain.CreateApplicationRequest) (*domain.ApplicationWithTokens, error) {
|
||||
appId := req.Id
|
||||
var err error
|
||||
if appId == 0 {
|
||||
appId, err = s.appRepo.NextApplicationId(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "next application id")
|
||||
}
|
||||
}
|
||||
app, err := s.appRepo.CreateApplication(ctx, appId, req.Name, req.Description, req.ApplicationGroupId, req.Type)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create application")
|
||||
}
|
||||
|
||||
return &domain.ApplicationWithTokens{
|
||||
App: s.convertApplication(*app),
|
||||
Tokens: make([]domain.Token, 0),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Application) convertApplication(req entity.Application) domain.Application {
|
||||
desc := ""
|
||||
if req.Description != nil {
|
||||
|
||||
+121
-1
@@ -4,6 +4,7 @@ import (
|
||||
"isp-system-service/assembly"
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/repository"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -15,6 +16,8 @@ import (
|
||||
"github.com/txix-open/isp-kit/test/dbt"
|
||||
"github.com/txix-open/isp-kit/test/fake"
|
||||
"github.com/txix-open/isp-kit/test/grpct"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func TestApplicationSuite(t *testing.T) {
|
||||
@@ -64,7 +67,120 @@ func (s *ApplicationSuite) TestGetAllApplications() {
|
||||
s.Require().ElementsMatch(expectedApps, result)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) insertApps(toInsert []domain.Application) []domain.Application {
|
||||
func (s *ApplicationSuite) TestCreate_HappyPath() {
|
||||
result := domain.ApplicationWithTokens{}
|
||||
apiReq := fake.It[domain.CreateApplicationRequest]()
|
||||
apiReq.Type = domain.ApplicationSystemType
|
||||
appGroup := s.createAppGroup()
|
||||
apiReq.ApplicationGroupId = appGroup.Id
|
||||
|
||||
err := s.api.Invoke("system/application/create_application").
|
||||
JsonRequestBody(apiReq).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.Require().NotEmpty(result.App.CreatedAt)
|
||||
result.App.CreatedAt = time.Time{}
|
||||
|
||||
s.Require().NotEmpty(result.App.UpdatedAt)
|
||||
result.App.UpdatedAt = time.Time{}
|
||||
|
||||
expectedApp := domain.Application{
|
||||
Id: apiReq.Id,
|
||||
Name: apiReq.Name,
|
||||
Description: apiReq.Description,
|
||||
Type: apiReq.Type,
|
||||
ServiceId: apiReq.ApplicationGroupId,
|
||||
}
|
||||
s.Require().Equal(expectedApp, result.App)
|
||||
s.Require().Empty(result.Tokens)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestCreate_WithEmptyAppId_HappyPath() {
|
||||
nextAppId, err := s.appRepo.NextApplicationId(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
|
||||
result := domain.ApplicationWithTokens{}
|
||||
apiReq := fake.It[domain.CreateApplicationRequest]()
|
||||
apiReq.Type = domain.ApplicationMobileType
|
||||
appGroup := s.createAppGroup()
|
||||
apiReq.ApplicationGroupId = appGroup.Id
|
||||
apiReq.Id = 0
|
||||
|
||||
err = s.api.Invoke("system/application/create_application").
|
||||
JsonRequestBody(apiReq).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.Require().NotEmpty(result.App.CreatedAt)
|
||||
result.App.CreatedAt = time.Time{}
|
||||
|
||||
s.Require().NotEmpty(result.App.UpdatedAt)
|
||||
result.App.UpdatedAt = time.Time{}
|
||||
|
||||
expectedApp := domain.Application{
|
||||
Id: nextAppId,
|
||||
Name: apiReq.Name,
|
||||
Description: apiReq.Description,
|
||||
Type: apiReq.Type,
|
||||
ServiceId: apiReq.ApplicationGroupId,
|
||||
}
|
||||
s.Require().Equal(expectedApp, result.App)
|
||||
s.Require().Empty(result.Tokens)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestCreate_AppGroupNotFound() {
|
||||
apiReq := fake.It[domain.CreateApplicationRequest]()
|
||||
apiReq.Type = domain.ApplicationSystemType
|
||||
|
||||
err := s.api.Invoke("system/application/create_application").
|
||||
JsonRequestBody(apiReq).
|
||||
Do(s.T().Context())
|
||||
statusErr, ok := status.FromError(err)
|
||||
s.Require().True(ok)
|
||||
s.Require().NotNil(statusErr)
|
||||
s.Require().Equal(codes.InvalidArgument, statusErr.Code())
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestCreate_ApplicationNameNotUniqueInAppGroup() {
|
||||
apiReq := fake.It[domain.CreateApplicationRequest]()
|
||||
apiReq.Type = domain.ApplicationSystemType
|
||||
|
||||
inserted := s.insertApps(fake.It[[]domain.Application](fake.MinSliceSize(1), fake.MaxSliceSize(1)))
|
||||
|
||||
apiReq.ApplicationGroupId = inserted[0].ServiceId
|
||||
apiReq.Name = inserted[0].Name
|
||||
|
||||
err := s.api.Invoke("system/application/create_application").
|
||||
JsonRequestBody(apiReq).
|
||||
Do(s.T().Context())
|
||||
statusErr, ok := status.FromError(err)
|
||||
s.Require().True(ok)
|
||||
s.Require().NotNil(statusErr)
|
||||
s.Require().Equal(codes.AlreadyExists, statusErr.Code())
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestCreate_ApplicationIdNotUnique() {
|
||||
apiReq := fake.It[domain.CreateApplicationRequest]()
|
||||
apiReq.Type = "SYSTEM"
|
||||
appGroup := s.createAppGroup()
|
||||
apiReq.ApplicationGroupId = appGroup.Id
|
||||
|
||||
inserted := s.insertApps(fake.It[[]domain.Application](fake.MinSliceSize(1), fake.MaxSliceSize(1)))
|
||||
apiReq.Id = inserted[0].Id
|
||||
|
||||
err := s.api.Invoke("system/application/create_application").
|
||||
JsonRequestBody(apiReq).
|
||||
Do(s.T().Context())
|
||||
statusErr, ok := status.FromError(err)
|
||||
s.Require().True(ok)
|
||||
s.Require().NotNil(statusErr)
|
||||
s.Require().Equal(codes.AlreadyExists, statusErr.Code())
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) createAppGroup() *entity.AppGroup {
|
||||
createdDomain, err := s.domainRepo.CreateDomain(
|
||||
s.T().Context(),
|
||||
fake.It[string](),
|
||||
@@ -80,7 +196,11 @@ func (s *ApplicationSuite) insertApps(toInsert []domain.Application) []domain.Ap
|
||||
createdDomain.Id,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
return appGroup
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) insertApps(toInsert []domain.Application) []domain.Application {
|
||||
appGroup := s.createAppGroup()
|
||||
toExpect := make([]domain.Application, 0, len(toInsert))
|
||||
for _, app := range toInsert {
|
||||
createdApp, err := s.appRepo.CreateApplication(
|
||||
|
||||
Reference in new issue
Block a user