diff --git a/controller/application.go b/controller/application.go index c91ab89..1a4fe50 100644 --- a/controller/application.go +++ b/controller/application.go @@ -223,18 +223,35 @@ func (c Application) Create(ctx context.Context, req domain.CreateApplicationReq // @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 +// @Failure 400 {object} apierrors.Error +// @Failure 404 {object} apierrors.Error +// @Failure 409 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @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) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeApplicationNotFound, + fmt.Sprintf("application with id %d not found", req.OldId), + err, + ) + case errors.Is(err, domain.ErrApplicationDuplicateName): + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeApplicationDuplicateName, + fmt.Sprintf("application with name %s already exists", req.Name), + err, + ) + case errors.Is(err, domain.ErrApplicationDuplicateId): + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeApplicationDuplicateId, + fmt.Sprintf("application with id %d already exists", req.NewId), + err, + ) default: return result, err } diff --git a/domain/application.go b/domain/application.go index 482ac07..7296013 100644 --- a/domain/application.go +++ b/domain/application.go @@ -49,7 +49,8 @@ type CreateApplicationRequest struct { } type UpdateApplicationRequest struct { - Id int `validate:"required"` + OldId int `validate:"required"` + NewId int `validate:"required"` Name string `validate:"required"` Description string } diff --git a/domain/error.go b/domain/error.go index eb64a99..04c6a1c 100644 --- a/domain/error.go +++ b/domain/error.go @@ -4,6 +4,14 @@ import ( "github.com/pkg/errors" ) +const ( + ErrCodeInvalidRequest = 600 + ErrCodeAppGroupNotFound = 601 + ErrCodeApplicationNotFound = 602 + ErrCodeApplicationDuplicateName = 603 + ErrCodeApplicationDuplicateId = 604 +) + var ( ErrDomainNotFound = errors.New("domain not found") ErrDomainDuplicateName = errors.New("domain name already exist") diff --git a/repository/application.go b/repository/application.go index 430e61b..32c94f7 100644 --- a/repository/application.go +++ b/repository/application.go @@ -131,17 +131,33 @@ func (r Application) UpdateApplication( ` result := entity.Application{} err := r.db.SelectRow(ctx, &result, q, id, name, description) - var pgErr *pgconn.PgError - switch { - case errors.As(err, &pgErr) && pgErr.ConstraintName == applicationUniqueNameConstraintName: - 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 + if err != nil { + return nil, r.handleUpdateError(err, q) } + return &result, nil +} + +func (r Application) UpdateApplicationWithNewId( + ctx context.Context, + oldId int, + newId int, + name string, + description string, +) (*entity.Application, error) { + q := ` + UPDATE application + SET id = $2, + name = $3, + description = $4 + WHERE id = $1 + RETURNING id, name, description, application_group_id, type, created_at, updated_at +` + result := entity.Application{} + err := r.db.SelectRow(ctx, &result, q, oldId, newId, name, description) + if err != nil { + return nil, r.handleUpdateError(err, q) + } + return &result, nil } func (r Application) DeleteApplicationByIdList(ctx context.Context, idList []int) (int, error) { @@ -201,7 +217,7 @@ func (r Application) handleCreateError(err error, q string) error { return errors.WithMessagef(err, "exec query %s", q) } switch pgErr.ConstraintName { - case applicationPkConstrainName: + case applicationPkConstraintName: return domain.ErrApplicationDuplicateId case applicationUniqueNameConstraintName: return domain.ErrApplicationDuplicateName @@ -210,3 +226,23 @@ func (r Application) handleCreateError(err error, q string) error { } return errors.WithMessagef(err, "exec query %s", q) } + +func (r Application) handleUpdateError(err error, q string) error { + if errors.Is(err, sql.ErrNoRows) { + return domain.ErrApplicationNotFound + } + + var pgErr *pgconn.PgError + if !errors.As(err, &pgErr) { + return errors.WithMessagef(err, "exec query %s", q) + } + + switch pgErr.ConstraintName { + case applicationPkConstraintName: + return domain.ErrApplicationDuplicateId + case applicationUniqueNameConstraintName: + return domain.ErrApplicationDuplicateName + default: + return errors.WithMessagef(err, "exec query %s", q) + } +} diff --git a/repository/consts.go b/repository/consts.go index 76aa757..fec1087 100644 --- a/repository/consts.go +++ b/repository/consts.go @@ -1,7 +1,7 @@ package repository const ( - applicationPkConstrainName = "application_pkey" + applicationPkConstraintName = "application_pkey" applicationUniqueNameConstraintName = "uq_name_application_group_id" applicationFkAppGroupConstraintName = "fk_application_group_id" diff --git a/service/application.go b/service/application.go index 5c17144..19987a0 100644 --- a/service/application.go +++ b/service/application.go @@ -261,7 +261,7 @@ func (s Application) Create(ctx context.Context, req domain.CreateApplicationReq } 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) + app, err := s.appRepo.UpdateApplicationWithNewId(ctx, req.OldId, req.NewId, req.Name, req.Description) if err != nil { return nil, errors.WithMessage(err, "update application") } diff --git a/service/interfaces.go b/service/interfaces.go index 5d3773c..2303026 100644 --- a/service/interfaces.go +++ b/service/interfaces.go @@ -25,6 +25,7 @@ type ApplicationRepo interface { GetApplicationByNameAndAppGroupId(ctx context.Context, name string, appGroupId int) (*entity.Application, error) 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) + UpdateApplicationWithNewId(ctx context.Context, oldId int, newId int, name string, description string) (*entity.Application, error) NextApplicationId(ctx context.Context) (int, error) GetAllApplications(ctx context.Context) ([]entity.Application, error) } diff --git a/tests/application_test.go b/tests/application_test.go index 4e8532d..0e96a4f 100644 --- a/tests/application_test.go +++ b/tests/application_test.go @@ -149,7 +149,8 @@ func (s *ApplicationSuite) TestCreate_ApplicationIdNotUnique() { 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, + OldId: inserted[0].Id, + NewId: fake.It[int](), Name: fake.It[string](), Description: fake.It[string](), } @@ -168,7 +169,7 @@ func (s *ApplicationSuite) TestUpdate_HappyPath() { result.App.UpdatedAt = time.Time{} expectedApp := domain.Application{ - Id: apiReq.Id, + Id: apiReq.NewId, Name: apiReq.Name, Description: apiReq.Description, Type: inserted[0].Type, @@ -177,11 +178,11 @@ func (s *ApplicationSuite) TestUpdate_HappyPath() { s.Require().Equal(expectedApp, result.App) s.Require().Empty(result.Tokens) - app, err := s.appRepo.GetApplicationById(s.T().Context(), apiReq.Id) + app, err := s.appRepo.GetApplicationById(s.T().Context(), apiReq.NewId) s.Require().NoError(err) expectedDbApp := entity.Application{ - Id: apiReq.Id, + Id: apiReq.NewId, Name: apiReq.Name, Description: app.Description, Type: inserted[0].Type, @@ -199,7 +200,8 @@ func (s *ApplicationSuite) TestUpdate_HappyPath() { 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, + NewId: inserted[0].Id, + OldId: inserted[0].Id, Name: inserted[1].Name, Description: fake.It[string](), } @@ -213,9 +215,28 @@ func (s *ApplicationSuite) TestUpdate_ApplicationNameNotUniqueInAppGroup() { s.Require().Equal(codes.AlreadyExists, statusErr.Code()) } +func (s *ApplicationSuite) TestUpdate_AppIdNotUnique() { + inserted := s.insertApps(fake.It[[]domain.Application](fake.MinSliceSize(2), fake.MaxSliceSize(2))) + apiReq := domain.UpdateApplicationRequest{ + OldId: inserted[0].Id, + NewId: inserted[1].Id, + 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.AlreadyExists, statusErr.Code()) +} + func (s *ApplicationSuite) TestUpdate_AppNotFound() { apiReq := domain.UpdateApplicationRequest{ - Id: fake.It[int](), + OldId: fake.It[int](), + NewId: fake.It[int](), Name: fake.It[string](), Description: fake.It[string](), }