diff --git a/internal/services/mappers.go b/internal/services/mappers.go index 482e7ba..b374b3c 100644 --- a/internal/services/mappers.go +++ b/internal/services/mappers.go @@ -2,7 +2,7 @@ package services import ( "evening_detective/internal/models" - "evening_detective/internal/services/story_service" + story_service_models "evening_detective/internal/services/story_service/models" "evening_detective/proto" "strings" ) @@ -29,19 +29,19 @@ func mapProtoTeamsToTeam(team *proto.Team) *models.Team { } } -func mapPlaceToProtoAction(place *story_service.Place) *proto.Action { +func mapPlaceToProtoAction(place *story_service_models.Place) *proto.Action { return &proto.Action{ Place: place.Code, } } -func mapStoryApplicationToProtoApplication(application *story_service.Application) *proto.Application { +func mapStoryApplicationToProtoApplication(application *story_service_models.Application) *proto.Application { return &proto.Application{ Name: application.Name, } } -func mapStoryApplicationsToApplications(applications []*story_service.Application) []*models.Application { +func mapStoryApplicationsToApplications(applications []*story_service_models.Application) []*models.Application { res := make([]*models.Application, 0, len(applications)) for _, application := range applications { res = append(res, mapStoryApplicationToApplication(application)) @@ -49,7 +49,7 @@ func mapStoryApplicationsToApplications(applications []*story_service.Applicatio return res } -func mapStoryApplicationToApplication(application *story_service.Application) *models.Application { +func mapStoryApplicationToApplication(application *story_service_models.Application) *models.Application { return &models.Application{ Name: application.Name, State: "NEW", diff --git a/internal/services/story_service/dependency.go b/internal/services/story_service/dependency.go index eb53be0..2452685 100644 --- a/internal/services/story_service/dependency.go +++ b/internal/services/story_service/dependency.go @@ -2,9 +2,10 @@ package story_service import ( "context" + "evening_detective/internal/services/story_service/models" ) type IStoryStorage interface { - Load(ctx context.Context) (*Story, error) - Save(ctx context.Context, story *Story) error + Load(ctx context.Context) (*models.Story, error) + Save(ctx context.Context, story *models.Story) error } diff --git a/internal/services/story_service/application_dto.go b/internal/services/story_service/models/application_dto.go similarity index 86% rename from internal/services/story_service/application_dto.go rename to internal/services/story_service/models/application_dto.go index a79fada..91ee4dc 100644 --- a/internal/services/story_service/application_dto.go +++ b/internal/services/story_service/models/application_dto.go @@ -1,4 +1,4 @@ -package story_service +package models type Application struct { Name string `json:"name"` diff --git a/internal/services/story_service/door_dto.go b/internal/services/story_service/models/door_dto.go similarity index 95% rename from internal/services/story_service/door_dto.go rename to internal/services/story_service/models/door_dto.go index 8670610..a25bacb 100644 --- a/internal/services/story_service/door_dto.go +++ b/internal/services/story_service/models/door_dto.go @@ -1,4 +1,4 @@ -package story_service +package models type Door struct { Code string `json:"code"` diff --git a/internal/services/story_service/place_dto.go b/internal/services/story_service/models/place_dto.go similarity index 98% rename from internal/services/story_service/place_dto.go rename to internal/services/story_service/models/place_dto.go index 3e83c2d..ed0da85 100644 --- a/internal/services/story_service/place_dto.go +++ b/internal/services/story_service/models/place_dto.go @@ -1,4 +1,4 @@ -package story_service +package models type Place struct { Code string `json:"code"` diff --git a/internal/services/story_service/story_dto.go b/internal/services/story_service/models/story_dto.go similarity index 71% rename from internal/services/story_service/story_dto.go rename to internal/services/story_service/models/story_dto.go index 40e8789..be96503 100644 --- a/internal/services/story_service/story_dto.go +++ b/internal/services/story_service/models/story_dto.go @@ -1,4 +1,4 @@ -package story_service +package models type Story struct { Places []*Place `json:"places"` diff --git a/internal/services/story_service/service.go b/internal/services/story_service/service.go index dc08300..a2ca7e6 100644 --- a/internal/services/story_service/service.go +++ b/internal/services/story_service/service.go @@ -4,6 +4,7 @@ import ( "context" "evening_detective/internal/modules/cleaner" "evening_detective/internal/modules/formatter" + "evening_detective/internal/services/story_service/models" "regexp" "strings" ) @@ -11,7 +12,7 @@ import ( type StoryService struct { cleaner cleaner.ICleaner formatter formatter.IFormatter - story *Story + story *models.Story storyStorage IStoryStorage } @@ -45,44 +46,44 @@ func (s *StoryService) Update(ctx context.Context) error { return nil } -func (s *StoryService) GetPlace(code string) *Place { +func (s *StoryService) GetPlace(code string) *models.Place { if strings.HasPrefix(code, "[") || strings.HasSuffix(code, "]") { - return NewClientErrorPlace(code) + return models.NewClientErrorPlace(code) } clearCode := s.cleaner.ClearCode(code) for _, place := range s.story.Places { if s.cleaner.ClearCode(place.Code) == clearCode { - applications := make([]*Application, 0, len(place.Applications)) + applications := make([]*models.Application, 0, len(place.Applications)) for _, application := range place.Applications { name := s.cleaner.ClearText(application.Name) applications = append( applications, - &Application{ + &models.Application{ Name: name, }, ) } - return NewPlace( + return models.NewPlace( place.Code, place.Name, s.cleaner.ClearText(place.Text), - WithPlaceApplication(applications...), - WithPlaceHidden(place.Hidden), - WithPlaceDoors(place.Doors...), + models.WithPlaceApplication(applications...), + models.WithPlaceHidden(place.Hidden), + models.WithPlaceDoors(place.Doors...), ) } } - return NewNotFoundPlace(code) + return models.NewNotFoundPlace(code) } -func (s *StoryService) GetPlaces(codes []string) []*Place { - places := make([]*Place, 0, 100) +func (s *StoryService) GetPlaces(codes []string) []*models.Place { + places := make([]*models.Place, 0, 100) m := map[string]any{} for _, code := range codes { place := s.GetPlace(code) _, ok := m[place.Code] if place.Hidden && !ok { - place = NewNotFoundPlace(place.Code) + place = models.NewNotFoundPlace(place.Code) } places = append(places, place) for _, door := range place.Doors { @@ -118,11 +119,11 @@ func (s *StoryService) deletePlace(ctx context.Context, code string) error { func (s *StoryService) addPlace(ctx context.Context, node *GraphNode) error { s.story.Places = append( s.story.Places, - NewPlace( + models.NewPlace( node.Code, node.Name, s.formatter.FormatText(node.Text), - WithPlaceApplication(s.getApplications(node)...), + models.WithPlaceApplication(s.getApplications(node)...), ), ) return s.Update(ctx) @@ -131,22 +132,22 @@ func (s *StoryService) addPlace(ctx context.Context, node *GraphNode) error { func (s *StoryService) updatePlace(ctx context.Context, code string, node *GraphNode) error { for i := range s.story.Places { if s.story.Places[i].Code == code { - s.story.Places[i] = NewPlace( + s.story.Places[i] = models.NewPlace( node.Code, node.Name, s.formatter.FormatText(node.Text), - WithPlaceApplication(s.getApplications(node)...), + models.WithPlaceApplication(s.getApplications(node)...), ) return s.Update(ctx) } } for i := range s.story.Places { if s.story.Places[i].Code == node.Code { - s.story.Places[i] = NewPlace( + s.story.Places[i] = models.NewPlace( code, node.Name, s.formatter.FormatText(node.Text), - WithPlaceApplication(s.getApplications(node)...), + models.WithPlaceApplication(s.getApplications(node)...), ) break } @@ -154,12 +155,12 @@ func (s *StoryService) updatePlace(ctx context.Context, code string, node *Graph return s.Update(ctx) } -func (s *StoryService) getApplications(node *GraphNode) []*Application { - nodeApplications := make([]*Application, 0, len(node.Applications)) +func (s *StoryService) getApplications(node *GraphNode) []*models.Application { + nodeApplications := make([]*models.Application, 0, len(node.Applications)) for _, application := range node.Applications { nodeApplications = append( nodeApplications, - &Application{ + &models.Application{ Name: application.Name, }, ) diff --git a/internal/services/story_service/service_test.go b/internal/services/story_service/service_test.go index 70f99aa..7f26fa3 100644 --- a/internal/services/story_service/service_test.go +++ b/internal/services/story_service/service_test.go @@ -4,6 +4,7 @@ import ( "evening_detective/internal/modules/cleaner" "evening_detective/internal/modules/formatter" "evening_detective/internal/services/story_service" + "evening_detective/internal/services/story_service/models" "evening_detective/internal/services/story_storage" "testing" @@ -13,128 +14,128 @@ import ( func TestStoryService_GetPlace(t *testing.T) { tests := []struct { name string - story *story_service.Story + story *models.Story code string - want *story_service.Place + want *models.Place }{ { name: "не корректный ввода", - story: &story_service.Story{}, + story: &models.Story{}, code: "[Ы]", - want: story_service.NewClientErrorPlace("[Ы]"), + want: models.NewClientErrorPlace("[Ы]"), }, { name: "точка не найдена", - story: &story_service.Story{}, + story: &models.Story{}, code: "Ы", - want: story_service.NewNotFoundPlace("Ы"), + want: models.NewNotFoundPlace("Ы"), }, { name: "получение точки", - story: &story_service.Story{ - Places: []*story_service.Place{ - story_service.NewPlace("Ы", "Название", "Текст"), + story: &models.Story{ + Places: []*models.Place{ + models.NewPlace("Ы", "Название", "Текст"), }, }, code: "Ы", - want: story_service.NewPlace("Ы", "Название", "Текст"), + want: models.NewPlace("Ы", "Название", "Текст"), }, { name: "получение скрытой точки", - story: &story_service.Story{ - Places: []*story_service.Place{ - story_service.NewPlace( + story: &models.Story{ + Places: []*models.Place{ + models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceHidden(true), + models.WithPlaceHidden(true), ), }, }, code: "Ы", - want: story_service.NewPlace( + want: models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceHidden(true), + models.WithPlaceHidden(true), ), }, { name: "получение точки с приложением", - story: &story_service.Story{ - Places: []*story_service.Place{ - story_service.NewPlace( + story: &models.Story{ + Places: []*models.Place{ + models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceApplication( - story_service.NewApplication("Приложение"), + models.WithPlaceApplication( + models.NewApplication("Приложение"), ), ), }, }, code: "Ы", - want: story_service.NewPlace( + want: models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceApplication( - story_service.NewApplication("Приложение"), + models.WithPlaceApplication( + models.NewApplication("Приложение"), ), ), }, { name: "получение точки с проходом", - story: &story_service.Story{ - Places: []*story_service.Place{ - story_service.NewPlace( + story: &models.Story{ + Places: []*models.Place{ + models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceDoors( - story_service.NewDoor("Й", "Приложение"), + models.WithPlaceDoors( + models.NewDoor("Й", "Приложение"), ), ), }, }, code: "Ы", - want: story_service.NewPlace( + want: models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceDoors( - story_service.NewDoor("Й", "Приложение"), + models.WithPlaceDoors( + models.NewDoor("Й", "Приложение"), ), ), }, { name: "получение точки с диалогом", - story: &story_service.Story{ - Places: []*story_service.Place{ - story_service.NewPlace( + story: &models.Story{ + Places: []*models.Place{ + models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceDoors( - story_service.NewDoor( + models.WithPlaceDoors( + models.NewDoor( "Й", "Приложение", - story_service.WithDoorShow(true), + models.WithDoorShow(true), ), ), ), }, }, code: "Ы", - want: story_service.NewPlace( + want: models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceDoors( - story_service.NewDoor( + models.WithPlaceDoors( + models.NewDoor( "Й", "Приложение", - story_service.WithDoorShow(true), + models.WithDoorShow(true), ), ), ), @@ -159,74 +160,74 @@ func TestStoryService_GetPlace(t *testing.T) { func TestStoryService_GetPlaces(t *testing.T) { tests := []struct { name string - story *story_service.Story + story *models.Story codes []string - want []*story_service.Place + want []*models.Place }{ { name: "Можно сходить в открытую точку", - story: &story_service.Story{ - Places: []*story_service.Place{ - story_service.NewPlace("Ы", "Название", "Текст"), + story: &models.Story{ + Places: []*models.Place{ + models.NewPlace("Ы", "Название", "Текст"), }, }, codes: []string{"Ы"}, - want: []*story_service.Place{ - story_service.NewPlace("Ы", "Название", "Текст"), + want: []*models.Place{ + models.NewPlace("Ы", "Название", "Текст"), }, }, { name: "Нельзя открыть скрытую точку", - story: &story_service.Story{ - Places: []*story_service.Place{ - story_service.NewPlace( + story: &models.Story{ + Places: []*models.Place{ + models.NewPlace( "Ы", "Название", "Текст", - story_service.WithPlaceHidden(true), + models.WithPlaceHidden(true), ), }, }, codes: []string{"Ы"}, - want: []*story_service.Place{ - story_service.NewNotFoundPlace("Ы"), + want: []*models.Place{ + models.NewNotFoundPlace("Ы"), }, }, { name: "Открываем скрытую точку", - story: &story_service.Story{ - Places: []*story_service.Place{ - story_service.NewPlace( + story: &models.Story{ + Places: []*models.Place{ + models.NewPlace( "Ы-1", "Название", "Текст", - story_service.WithPlaceDoors( - story_service.NewDoor("Ы-2", "Название"), + models.WithPlaceDoors( + models.NewDoor("Ы-2", "Название"), ), ), - story_service.NewPlace( + models.NewPlace( "Ы-2", "Название", "Текст", - story_service.WithPlaceHidden(true), + models.WithPlaceHidden(true), ), }, }, codes: []string{"Ы-1", "Ы-2"}, - want: []*story_service.Place{ - story_service.NewPlace( + want: []*models.Place{ + models.NewPlace( "Ы-1", "Название", "Текст", - story_service.WithPlaceDoors( - story_service.NewDoor("Ы-2", "Название"), + models.WithPlaceDoors( + models.NewDoor("Ы-2", "Название"), ), ), - story_service.NewPlace( + models.NewPlace( "Ы-2", "Название", "Текст", - story_service.WithPlaceHidden(true), + models.WithPlaceHidden(true), ), }, }, diff --git a/internal/services/story_storage/file_storage_service.go b/internal/services/story_storage/file_storage_service.go index 1b929b3..80fc73e 100644 --- a/internal/services/story_storage/file_storage_service.go +++ b/internal/services/story_storage/file_storage_service.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "evening_detective/internal/services/story_service" + "evening_detective/internal/services/story_service/models" "fmt" "log" "os" @@ -19,20 +20,20 @@ func NewFileStoryStorage(filepath string) story_service.IStoryStorage { } } -func (s *fileService) Load(ctx context.Context) (*story_service.Story, error) { +func (s *fileService) Load(ctx context.Context) (*models.Story, error) { data, err := os.ReadFile(s.filepath) if err != nil { return nil, fmt.Errorf("story file %s not found", s.filepath) } log.Printf("load story from: %s", s.filepath) - story := &story_service.Story{} + story := &models.Story{} if err := json.Unmarshal(data, story); err != nil { return nil, err } return story, nil } -func (s *fileService) Save(ctx context.Context, story *story_service.Story) error { +func (s *fileService) Save(ctx context.Context, story *models.Story) error { data, err := json.Marshal(story) if err != nil { return err diff --git a/internal/services/story_storage/interface.go b/internal/services/story_storage/interface.go index a57682e..4892fad 100644 --- a/internal/services/story_storage/interface.go +++ b/internal/services/story_storage/interface.go @@ -1,12 +1,11 @@ -//go:generate mockgen -source=interface.go -destination=mocks/mock.go -package=mocks package story_storage import ( "context" - "evening_detective/internal/services/story_service" + "evening_detective/internal/services/story_service/models" ) type IStoryStorage interface { - Load(ctx context.Context) (*story_service.Story, error) - Save(ctx context.Context, story *story_service.Story) error + Load(ctx context.Context) (*models.Story, error) + Save(ctx context.Context, story *models.Story) error } diff --git a/internal/services/story_storage/mocks/mock.go b/internal/services/story_storage/mocks/mock.go deleted file mode 100644 index 0b7a064..0000000 --- a/internal/services/story_storage/mocks/mock.go +++ /dev/null @@ -1,65 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: interface.go - -// Package mocks is a generated GoMock package. -package mocks - -import ( - context "context" - story_service "evening_detective/internal/services/story_service" - reflect "reflect" - - gomock "github.com/golang/mock/gomock" -) - -// MockIStoryStorage is a mock of IStoryStorage interface. -type MockIStoryStorage struct { - ctrl *gomock.Controller - recorder *MockIStoryStorageMockRecorder -} - -// MockIStoryStorageMockRecorder is the mock recorder for MockIStoryStorage. -type MockIStoryStorageMockRecorder struct { - mock *MockIStoryStorage -} - -// NewMockIStoryStorage creates a new mock instance. -func NewMockIStoryStorage(ctrl *gomock.Controller) *MockIStoryStorage { - mock := &MockIStoryStorage{ctrl: ctrl} - mock.recorder = &MockIStoryStorageMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockIStoryStorage) EXPECT() *MockIStoryStorageMockRecorder { - return m.recorder -} - -// Load mocks base method. -func (m *MockIStoryStorage) Load(ctx context.Context) (*story_service.Story, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Load", ctx) - ret0, _ := ret[0].(*story_service.Story) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Load indicates an expected call of Load. -func (mr *MockIStoryStorageMockRecorder) Load(ctx interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Load", reflect.TypeOf((*MockIStoryStorage)(nil).Load), ctx) -} - -// Save mocks base method. -func (m *MockIStoryStorage) Save(ctx context.Context, story *story_service.Story) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Save", ctx, story) - ret0, _ := ret[0].(error) - return ret0 -} - -// Save indicates an expected call of Save. -func (mr *MockIStoryStorageMockRecorder) Save(ctx, story interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockIStoryStorage)(nil).Save), ctx, story) -} diff --git a/internal/services/story_storage/var_storage_service.go b/internal/services/story_storage/var_storage_service.go index 81ccce4..1fa61c8 100644 --- a/internal/services/story_storage/var_storage_service.go +++ b/internal/services/story_storage/var_storage_service.go @@ -3,23 +3,24 @@ package story_storage import ( "context" "evening_detective/internal/services/story_service" + "evening_detective/internal/services/story_service/models" ) type varService struct { - story *story_service.Story + story *models.Story } -func NewVarStoryStorage(story *story_service.Story) story_service.IStoryStorage { +func NewVarStoryStorage(story *models.Story) story_service.IStoryStorage { return &varService{ story: story, } } -func (s *varService) Load(ctx context.Context) (*story_service.Story, error) { +func (s *varService) Load(ctx context.Context) (*models.Story, error) { return s.story, nil } -func (s *varService) Save(ctx context.Context, story *story_service.Story) error { +func (s *varService) Save(ctx context.Context, story *models.Story) error { s.story = story return nil }