MSP-5186 Add /application/update_application endpoint
This commit is contained in:
1 parent
e2ed71196b
commit
aae74ef611
8 files changed
+199
-7
No files matched your search
@@ -20,6 +20,7 @@ type ApplicationService interface {
|
||||
NextId(ctx context.Context) (int, error)
|
||||
GetAll(ctx context.Context) ([]domain.Application, error)
|
||||
Create(ctx context.Context, req domain.CreateApplicationRequest) (*domain.ApplicationWithTokens, error)
|
||||
Update(ctx context.Context, req domain.UpdateApplicationRequest) (*domain.ApplicationWithTokens, error)
|
||||
}
|
||||
|
||||
type Application struct {
|
||||
@@ -184,7 +185,7 @@ func (c Application) GetAll(ctx context.Context) ([]domain.Application, error) {
|
||||
// @Description Если приложение с такими идентификатором или связкой `applicationGroupId`-`name` существует, то возвращает ошибку
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.ApplicationCreateUpdateRequest true "Объект приложения"
|
||||
// @Param body body domain.CreateApplicationRequest true "Объект приложения"
|
||||
// @Success 200 {object} domain.ApplicationWithTokens
|
||||
// @Failure 400 {object} domain.GrpcError
|
||||
// @Failure 404 {object} domain.GrpcError
|
||||
@@ -204,3 +205,28 @@ func (c Application) Create(ctx context.Context, req domain.CreateApplicationReq
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
|
||||
// Update godoc
|
||||
// @Tags application
|
||||
// @Summary Обновить приложение
|
||||
// @Description Если приложение с связкой `applicationGroupId`-`name` существует или приложение не найдено, то возвращает ошибку
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.UpdateApplicationRequest 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/update_application [POST]
|
||||
func (c Application) Update(ctx context.Context, req domain.UpdateApplicationRequest) (*domain.ApplicationWithTokens, error) {
|
||||
result, err := c.service.Update(ctx, req)
|
||||
switch {
|
||||
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)
|
||||
default:
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
@@ -295,6 +295,18 @@ definitions:
|
||||
required:
|
||||
- appId
|
||||
type: object
|
||||
domain.UpdateApplicationRequest:
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
id:
|
||||
type: integer
|
||||
name:
|
||||
type: string
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
type: object
|
||||
entity.Token:
|
||||
properties:
|
||||
appId:
|
||||
@@ -670,6 +682,45 @@ paths:
|
||||
summary: Получить следующий идентификатор приложения
|
||||
tags:
|
||||
- application
|
||||
/application/update_application:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Если приложение с связкой `applicationGroupId`-`name` существует
|
||||
или приложение не найдено, то возвращает ошибку
|
||||
parameters:
|
||||
- description: Объект приложения
|
||||
in: body
|
||||
name: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/domain.UpdateApplicationRequest'
|
||||
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
|
||||
/domain/create_update_domain:
|
||||
post:
|
||||
consumes:
|
||||
|
||||
@@ -47,3 +47,9 @@ type CreateApplicationRequest struct {
|
||||
ApplicationGroupId int `validate:"required"`
|
||||
Type string `validate:"required,oneof=SYSTEM MOBILE"`
|
||||
}
|
||||
|
||||
type UpdateApplicationRequest struct {
|
||||
Id int `validate:"required"`
|
||||
Name string `validate:"required"`
|
||||
Description string
|
||||
}
|
||||
@@ -4,6 +4,7 @@ go 1.24
|
||||
|
||||
require (
|
||||
github.com/Masterminds/squirrel v1.5.4
|
||||
github.com/jackc/pgx/v5 v5.7.2
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/txix-open/isp-kit v1.45.0
|
||||
@@ -31,7 +32,6 @@ require (
|
||||
github.com/iancoleman/strcase v0.3.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.7.2 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/jmoiron/sqlx v1.4.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
|
||||
@@ -126,7 +126,7 @@ func (r Application) CreateApplication(ctx context.Context, id int, name string,
|
||||
|
||||
func (r Application) UpdateApplication(
|
||||
ctx context.Context,
|
||||
appGroupId int,
|
||||
id int,
|
||||
name string,
|
||||
description string,
|
||||
) (*entity.Application, error) {
|
||||
@@ -138,12 +138,19 @@ func (r Application) UpdateApplication(
|
||||
RETURNING id, name, description, application_group_id, type, created_at, updated_at
|
||||
`
|
||||
result := entity.Application{}
|
||||
err := r.db.SelectRow(ctx, &result, q, appGroupId, name, description)
|
||||
if err != nil {
|
||||
err := r.db.SelectRow(ctx, &result, q, id, name, description)
|
||||
var pgErr *pgconn.PgError
|
||||
switch {
|
||||
case errors.As(err, &pgErr) && pgErr.Code == pgUniqueViolationErrorCode &&
|
||||
pgErr.ConstraintName == applicationUniqueNameConstrainName:
|
||||
return nil, domain.ErrApplicationDuplicateName
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, domain.ErrApplicationNotFound
|
||||
case err != nil:
|
||||
return nil, errors.WithMessagef(err, "exec query %s", q)
|
||||
default:
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r Application) DeleteApplicationByIdList(ctx context.Context, idList []int) (int, error) {
|
||||
|
||||
@@ -175,6 +175,10 @@ func applicationCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
Path: "system/application/create_application",
|
||||
Inner: true,
|
||||
Handler: c.Application.Create,
|
||||
}, {
|
||||
Path: "system/application/update_application",
|
||||
Inner: true,
|
||||
Handler: c.Application.Update,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,6 +297,20 @@ func (s Application) Create(ctx context.Context, req domain.CreateApplicationReq
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Application) Update(ctx context.Context, req domain.UpdateApplicationRequest) (*domain.ApplicationWithTokens, error) {
|
||||
app, err := s.appRepo.UpdateApplication(ctx, req.Id, req.Name, req.Description)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "update application")
|
||||
}
|
||||
|
||||
result, err := s.EnrichWithTokens(ctx, []entity.Application{*app})
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "enrich application with tokens")
|
||||
}
|
||||
|
||||
return result[0], nil
|
||||
}
|
||||
|
||||
func (s Application) convertApplication(req entity.Application) domain.Application {
|
||||
desc := ""
|
||||
if req.Description != nil {
|
||||
|
||||
@@ -180,6 +180,90 @@ func (s *ApplicationSuite) TestCreate_ApplicationIdNotUnique() {
|
||||
s.Require().Equal(codes.AlreadyExists, statusErr.Code())
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestUpdate_HappyPath() {
|
||||
inserted := s.insertApps(fake.It[[]domain.Application](fake.MinSliceSize(1), fake.MaxSliceSize(1)))
|
||||
apiReq := domain.UpdateApplicationRequest{
|
||||
Id: inserted[0].Id,
|
||||
Name: fake.It[string](),
|
||||
Description: fake.It[string](),
|
||||
}
|
||||
|
||||
var result domain.ApplicationWithTokens
|
||||
err := s.api.Invoke("system/application/update_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: inserted[0].Type,
|
||||
ServiceId: inserted[0].ServiceId,
|
||||
}
|
||||
s.Require().Equal(expectedApp, result.App)
|
||||
s.Require().Empty(result.Tokens)
|
||||
|
||||
app, err := s.appRepo.GetApplicationById(s.T().Context(), apiReq.Id)
|
||||
s.Require().NoError(err)
|
||||
|
||||
expectedDbApp := entity.Application{
|
||||
Id: apiReq.Id,
|
||||
Name: apiReq.Name,
|
||||
Type: inserted[0].Type,
|
||||
ApplicationGroupId: inserted[0].ServiceId,
|
||||
}
|
||||
s.Require().Equal(apiReq.Description, *app.Description)
|
||||
app.Description = nil
|
||||
|
||||
s.Require().NotEmpty(app.CreatedAt)
|
||||
app.CreatedAt = time.Time{}
|
||||
|
||||
s.Require().NotEmpty(app.UpdatedAt)
|
||||
app.UpdatedAt = time.Time{}
|
||||
s.Require().Equal(expectedDbApp, *app)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestUpdate_ApplicationNameNotUniqueInAppGroup() {
|
||||
inserted := s.insertApps(fake.It[[]domain.Application](fake.MinSliceSize(2), fake.MaxSliceSize(2)))
|
||||
apiReq := domain.UpdateApplicationRequest{
|
||||
Id: inserted[0].Id,
|
||||
Name: inserted[1].Name,
|
||||
Description: fake.It[string](),
|
||||
}
|
||||
|
||||
err := s.api.Invoke("system/application/update_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) TestUpdate_AppNotFound() {
|
||||
apiReq := domain.UpdateApplicationRequest{
|
||||
Id: fake.It[int](),
|
||||
Name: fake.It[string](),
|
||||
Description: fake.It[string](),
|
||||
}
|
||||
|
||||
err := s.api.Invoke("system/application/update_application").
|
||||
JsonRequestBody(apiReq).
|
||||
Do(s.T().Context())
|
||||
statusErr, ok := status.FromError(err)
|
||||
s.Require().True(ok)
|
||||
s.Require().NotNil(statusErr)
|
||||
s.Require().Equal(codes.NotFound, statusErr.Code())
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) createAppGroup() *entity.AppGroup {
|
||||
createdDomain, err := s.domainRepo.CreateDomain(
|
||||
s.T().Context(),
|
||||
|
||||
Reference in new issue
Block a user