diff --git a/internal/modules/cleaner/service.go b/internal/modules/cleaner/service.go index fb49e5e..3c670ff 100644 --- a/internal/modules/cleaner/service.go +++ b/internal/modules/cleaner/service.go @@ -45,5 +45,5 @@ func (s *service) ClearCode(code string) string { } func (s *service) ClearText(text string) string { - return re.ReplaceAllString(text, "") + return strings.TrimSpace(re.ReplaceAllString(text, "")) } diff --git a/internal/services/mappers.go b/internal/services/mappers.go index c09d209..482e7ba 100644 --- a/internal/services/mappers.go +++ b/internal/services/mappers.go @@ -29,10 +29,9 @@ func mapProtoTeamsToTeam(team *proto.Team) *models.Team { } } -func mapActionToProtoAction(action *models.Action) *proto.Action { +func mapPlaceToProtoAction(place *story_service.Place) *proto.Action { return &proto.Action{ - Id: action.ID, - Place: action.Place, + Place: place.Code, } } diff --git a/internal/services/services.go b/internal/services/services.go index f04837e..fda6088 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -133,10 +133,10 @@ func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.G if err != nil { return nil, status.Errorf(codes.Internal, err.Error()) } + res := make([]*proto.Action, 0, len(actions)) - for _, action := range actions { - newAction := mapActionToProtoAction(action) - place := s.storyService.GetPlace(action.Place) + for _, place := range s.getPlaces(actions) { + newAction := mapPlaceToProtoAction(place) newAction.Text = place.Text newAction.Name = place.Name newAction.Applications = make([]*proto.Application, 0, len(place.Applications)) @@ -151,6 +151,27 @@ func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.G }, err } +func (s *Services) getPlaces(actions []*models.Action) []*story_service.Place { + places := []*story_service.Place{} + m := map[string]any{} + for _, action := range actions { + place := s.storyService.GetPlace(action.Place) + _, ok := m[place.Code] + if place.Hidden && !ok { + place = &story_service.Place{ + Code: place.Code, + Name: "Не найдено", + Text: "Такой точки не существует.", + } + } + places = append(places, place) + for _, door := range place.Doors { + m[door.Code] = struct{}{} + } + } + return places +} + func (s *Services) GetTeamsCSV(ctx context.Context, req *proto.GetTeamsCSVReq) (*proto.GetTeamsCSVRsp, error) { panic("unimplemented") } diff --git a/internal/services/services_test.go b/internal/services/services_test.go new file mode 100644 index 0000000..b824dd9 --- /dev/null +++ b/internal/services/services_test.go @@ -0,0 +1,155 @@ +package services + +import ( + "evening_detective/internal/models" + "evening_detective/internal/modules/cleaner" + "evening_detective/internal/modules/formatter" + "evening_detective/internal/services/story_service" + "evening_detective/internal/services/story_storage" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestServices_getPlaces(t *testing.T) { + tests := []struct { + name string + story *story_service.Story + actions []*models.Action + want []*story_service.Place + }{ + // { + // name: "Нельзя открыть скрытую точку", + // story: &story_service.Story{ + // Places: []*story_service.Place{ + // { + // Code: "Ы", + // Name: "Название", + // Text: "Текст", + // Hidden: true, + // }, + // }, + // }, + // actions: []*models.Action{ + // { + // Place: "Ы", + // }, + // }, + // want: []*story_service.Place{ + // { + // Code: "Ы", + // Name: "Не найдено", + // Text: "Такой точки не существует.", + // }, + // }, + // }, + // { + // name: "Нельзя открыть скрытую точку", + // story: &story_service.Story{ + // Places: []*story_service.Place{ + // { + // Code: "Ы-1", + // Name: "Название", + // Text: "Текст", + // }, + // { + // Code: "Ы-2", + // Name: "Название", + // Text: "Текст", + // Hidden: true, + // }, + // }, + // }, + // actions: []*models.Action{ + // { + // Place: "Ы-1", + // }, + // { + // Place: "Ы-2", + // }, + // }, + // want: []*story_service.Place{ + // { + // Code: "Ы-1", + // Name: "Название", + // Text: "Текст", + // Applications: []*story_service.Application{}, + // }, + // { + // Code: "Ы-2", + // Name: "Не найдено", + // Text: "Такой точки не существует.", + // }, + // }, + // }, + { + name: "Нельзя открыть скрытую точку", + story: &story_service.Story{ + Places: []*story_service.Place{ + { + Code: "Ы-1", + Name: "Название", + Text: "Текст", + Applications: []*story_service.Application{}, + Doors: []*story_service.Door{ + { + Code: "Ы-2", + Name: "Название", + }, + }, + }, + { + Code: "Ы-2", + Name: "Название", + Text: "Текст", + Applications: []*story_service.Application{}, + Hidden: true, + }, + }, + }, + actions: []*models.Action{ + { + Place: "Ы-1", + }, + { + Place: "Ы-2", + }, + }, + want: []*story_service.Place{ + { + Code: "Ы-1", + Name: "Название", + Text: "Текст", + Applications: []*story_service.Application{}, + Doors: []*story_service.Door{ + { + Code: "Ы-2", + Name: "Название", + }, + }, + }, + { + Code: "Ы-2", + Name: "Название", + Text: "Текст", + Applications: []*story_service.Application{}, + Hidden: true, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + storyService, err := story_service.NewStoryService( + cleaner.NewCleaner(), + formatter.NewFormatter(), + story_storage.NewVarStoryStorage(tt.story), + ) + assert.Nil(t, err) + + s := NewServices(nil, storyService, nil, nil, nil, "") + got := s.getPlaces(tt.actions) + assert.Equal(t, got, tt.want) + }) + } +} diff --git a/internal/services/story_service/data_models.go b/internal/services/story_service/data_models.go index 740dd56..d50bff1 100644 --- a/internal/services/story_service/data_models.go +++ b/internal/services/story_service/data_models.go @@ -9,8 +9,15 @@ type Place struct { Name string `json:"name"` Text string `json:"text"` Applications []*Application `json:"applications,omitempty"` + Hidden bool `json:"hidden"` + Doors []*Door `json:"doors"` } type Application struct { Name string `json:"name"` } + +type Door struct { + Code string `json:"code"` + Name string `json:"name"` +} diff --git a/internal/services/story_service/service.go b/internal/services/story_service/service.go index 704827b..450ce07 100644 --- a/internal/services/story_service/service.go +++ b/internal/services/story_service/service.go @@ -71,6 +71,8 @@ func (s *StoryService) GetPlace(code string) *Place { Name: place.Name, Text: s.cleaner.ClearText(place.Text), Applications: applications, + Hidden: place.Hidden, + Doors: place.Doors, } } } diff --git a/internal/services/story_service/service_test.go b/internal/services/story_service/service_test.go index 6798a12..eedef81 100644 --- a/internal/services/story_service/service_test.go +++ b/internal/services/story_service/service_test.go @@ -84,6 +84,37 @@ func TestStoryService_GetPlace(t *testing.T) { }, }, }, + { + name: "получение точки с проходами", + story: &story_service.Story{ + Places: []*story_service.Place{ + { + Code: "Ы-1", + Name: "Название", + Text: "Текст", + Doors: []*story_service.Door{ + { + Code: "Ы-2", + Name: "Приложение", + }, + }, + }, + }, + }, + code: "Ы-1", + want: &story_service.Place{ + Code: "Ы-1", + Name: "Название", + Text: "Текст", + Applications: []*story_service.Application{}, + Doors: []*story_service.Door{ + { + Code: "Ы-2", + Name: "Приложение", + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {