Skip to content

Commit

Permalink
add name check when resource name changed
Browse files Browse the repository at this point in the history
  • Loading branch information
futugyou committed Jun 25, 2024
1 parent 02f3f55 commit 8aab5c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 15 additions & 2 deletions infr-project/application/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package application
import (
"context"
"fmt"
"strings"

domain "github.com/futugyou/infr-project/domain"
"github.com/futugyou/infr-project/extensions"
Expand All @@ -14,6 +15,7 @@ import (
type ResourceService struct {
service *ApplicationService[resource.IResourceEvent, *resource.Resource]
unitOfWork domain.IUnitOfWork
queryRepo IPlatformRepository
}

func needStoreSnapshot(aggregate *resource.Resource) bool {
Expand All @@ -24,10 +26,12 @@ func NewResourceService(
eventStore infra.IEventStore[resource.IResourceEvent],
snapshotStore infra.ISnapshotStore[*resource.Resource],
unitOfWork domain.IUnitOfWork,
queryRepo IPlatformRepository,
) *ResourceService {
return &ResourceService{
service: NewApplicationService(eventStore, snapshotStore, unitOfWork, resource.ResourceFactory, needStoreSnapshot),
unitOfWork: unitOfWork,
queryRepo: queryRepo,
}
}

Expand All @@ -51,6 +55,7 @@ func (s *ResourceService) UpdateResource(id string, aux models.UpdateResourceReq
return err
}

ctx := context.Background()
source := *res
oldVersion := source.Version
var aggregate *resource.Resource
Expand All @@ -63,7 +68,15 @@ func (s *ResourceService) UpdateResource(id string, aux models.UpdateResourceReq
}

if source.Name != aux.Name {
// TODO: check name in db
res, err := s.queryRepo.GetResourceByName(ctx, aux.Name)
if err != nil && !strings.HasPrefix(err.Error(), "no data found") {
return err
}

if res != nil && len(res.Id) > 0 && res.Id != id {
return fmt.Errorf("name: %s is existed", aux.Name)
}

if aggregate, err = source.ChangeName(aux.Name); err != nil {
return err
}
Expand All @@ -86,7 +99,7 @@ func (s *ResourceService) UpdateResource(id string, aux models.UpdateResourceReq
aggs = append(aggs, &aggregates[i])
}

return s.service.withUnitOfWork(context.Background(), func(ctx context.Context) error {
return s.service.withUnitOfWork(ctx, func(ctx context.Context) error {
return s.service.SaveSnapshotAndEvent2(ctx, aggs)
})
}
Expand Down
18 changes: 15 additions & 3 deletions infr-project/controller/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,23 @@ func createResourceService() (*application.ResourceService, error) {
return nil, err
}

return application.NewResourceService(eventStore, snapshotStore, unitOfWork), nil
queryRepo, err := createResourceQueryRepository()
if err != nil {
return nil, err
}

return application.NewResourceService(eventStore, snapshotStore, unitOfWork, queryRepo), nil
}

func createResourceQueryService() (*application.ResourceQueryService, error) {
queryRepo, err := createResourceQueryRepository()
if err != nil {
return nil, err
}
return application.NewResourceQueryService(queryRepo), nil
}

func createResourceQueryRepository() (*infra.ResourceQueryRepository, error) {
config := infra.QueryDBConfig{
DBName: os.Getenv("query_db_name"),
ConnectString: os.Getenv("query_mongodb_url"),
Expand All @@ -180,6 +193,5 @@ func createResourceQueryService() (*application.ResourceQueryService, error) {
return nil, err
}

repo := infra.NewResourceQueryRepository(client, config)
return application.NewResourceQueryService(repo), nil
return infra.NewResourceQueryRepository(client, config), nil
}

0 comments on commit 8aab5c7

Please sign in to comment.