MSP-5186 Implement AppGroup CRUD endpoints

This commit is contained in:
Синельник Тимур Романович committed 2025-04-02 12:38:23 +03:00
1 parent 59f3dc0d2f
commit cb50a60156
10 files changed
+651 -27

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
package service
import (
"context"
"isp-system-service/domain"
"isp-system-service/entity"
"github.com/pkg/errors"
)
type AppGroup struct {
repo AppGroupRepo
}
func NewAppGroup(repo AppGroupRepo) AppGroup {
return AppGroup{
repo: repo,
}
}
func (s AppGroup) Create(ctx context.Context, req domain.CreateAppGroupRequest) (*domain.AppGroup, error) {
appGroup, err := s.repo.CreateAppGroup(ctx, req.Name, req.Description, 1)
if err != nil {
return nil, errors.WithMessage(err, "create appGroup")
}
converted := s.convertAppGroup(*appGroup)
return &converted, nil
}
func (s AppGroup) Update(ctx context.Context, req domain.UpdateAppGroupRequest) (*domain.AppGroup, error) {
appGroup, err := s.repo.UpdateAppGroup(ctx, req.Id, req.Name, req.Description)
if err != nil {
return nil, errors.WithMessage(err, "update appGroup")
}
converted := s.convertAppGroup(*appGroup)
return &converted, nil
}
func (s AppGroup) DeleteList(ctx context.Context, req domain.IdListRequest) (*domain.DeleteResponse, error) {
deleted, err := s.repo.DeleteAppGroup(ctx, req.IdList)
if err != nil {
return nil, errors.WithMessage(err, "delete appGroup")
}
return &domain.DeleteResponse{
Deleted: deleted,
}, nil
}
func (s AppGroup) GetByIdList(ctx context.Context, idList []int) ([]domain.AppGroup, error) {
appGroups, err := s.repo.GetAppGroupByIdList(ctx, idList)
if err != nil {
return nil, errors.WithMessage(err, "get appGroups by id list")
}
result := make([]domain.AppGroup, 0, len(appGroups))
for _, appGroup := range appGroups {
result = append(result, s.convertAppGroup(appGroup))
}
return result, nil
}
func (s AppGroup) convertAppGroup(appGroup entity.AppGroup) domain.AppGroup {
return domain.AppGroup{
Id: appGroup.Id,
Name: appGroup.Name,
Description: *appGroup.Description,
CreatedAt: appGroup.CreatedAt,
UpdatedAt: appGroup.UpdatedAt,
}
}
+1 -16
View File
@@ -63,23 +63,12 @@ func (s Service) GetByDomainId(ctx context.Context, domainId int) ([]domain.Serv
func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdateRequest) (*domain.Service, error) {
req.DomainId = 1 // temporary use only 1 domain, soon domain entity will be removed
existed, err := s.serviceRepo.GetAppGroupByNameAndDomainId(ctx, req.Name, req.DomainId)
switch {
case errors.Is(err, domain.ErrAppGroupNotFound):
case err != nil:
return nil, errors.WithMessage(err, "get service by name and domain_id")
}
_, err = s.domainRepo.GetDomainById(ctx, req.DomainId)
_, err := s.domainRepo.GetDomainById(ctx, req.DomainId)
if err != nil {
return nil, errors.WithMessage(err, "get domain by id")
}
if req.Id == 0 {
if existed != nil {
return nil, domain.ErrDomainDuplicateName
}
serviceEntity, err := s.serviceRepo.CreateAppGroup(ctx, req.Name, req.Description, req.DomainId)
if err != nil {
return nil, errors.WithMessage(err, "create service")
@@ -89,10 +78,6 @@ func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdat
return &result, nil
}
if existed != nil && existed.Id != req.Id {
return nil, domain.ErrDomainDuplicateName
}
_, err = s.serviceRepo.GetAppGroupById(ctx, req.Id)
if err != nil {
return nil, errors.WithMessage(err, "get service by id")