diff --git a/cmd/evening_detective/main.go b/cmd/evening_detective/main.go index 5400fb7..0333245 100644 --- a/cmd/evening_detective/main.go +++ b/cmd/evening_detective/main.go @@ -13,6 +13,7 @@ import ( "evening_detective/internal/services/link" "evening_detective/internal/services/pdf" "evening_detective/internal/services/story_service" + "evening_detective/internal/services/story_storage" proto "evening_detective/proto" "io/fs" "log" @@ -54,7 +55,9 @@ func main() { storyFilepath := config.GetStoryFilepath() - storyService, err := story_service.NewStoryService(cleaner, formatter, storyFilepath) + storyStorage := story_storage.NewStoryStorage(storyFilepath) + + storyService, err := story_service.NewStoryService(cleaner, formatter, storyStorage) if err != nil { log.Fatalln(err) } diff --git a/internal/services/services.go b/internal/services/services.go index 359ff53..f04837e 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -230,7 +230,7 @@ func (s *Services) UpdateNode(ctx context.Context, req *proto.UpdateNodeReq) (*p Text: req.Node.Text, Applications: applications, } - if err := s.storyService.UpdatePlace(req.Code, node); err != nil { + if err := s.storyService.UpdatePlace(ctx, req.Code, node); err != nil { return nil, err } return &proto.UpdateNodeRsp{}, nil diff --git a/internal/services/story_service/dependency.go b/internal/services/story_service/dependency.go new file mode 100644 index 0000000..eb53be0 --- /dev/null +++ b/internal/services/story_service/dependency.go @@ -0,0 +1,10 @@ +package story_service + +import ( + "context" +) + +type IStoryStorage interface { + Load(ctx context.Context) (*Story, error) + Save(ctx context.Context, story *Story) error +} diff --git a/internal/services/story_service/service.go b/internal/services/story_service/service.go index 9e51588..02e5d10 100644 --- a/internal/services/story_service/service.go +++ b/internal/services/story_service/service.go @@ -2,73 +2,49 @@ package story_service import ( "context" - "encoding/json" "evening_detective/internal/modules/cleaner" "evening_detective/internal/modules/formatter" - "fmt" - "log" - "os" "regexp" "strings" ) type StoryService struct { - cleaner cleaner.ICleaner - formatter formatter.IFormatter - filepath string - story *Story + cleaner cleaner.ICleaner + formatter formatter.IFormatter + story *Story + storyStorage IStoryStorage } func NewStoryService( cleaner cleaner.ICleaner, formatter formatter.IFormatter, - filepath string, + storyStorage IStoryStorage, ) (*StoryService, error) { s := &StoryService{ - cleaner: cleaner, - formatter: formatter, - filepath: filepath, + cleaner: cleaner, + formatter: formatter, + storyStorage: storyStorage, } - if err := s.Load(); err != nil { + story, err := s.storyStorage.Load(context.Background()) + if err != nil { return nil, err } + s.story = story return s, nil } -func (s *StoryService) Load() error { - data, err := os.ReadFile(s.filepath) - if err != nil { - return fmt.Errorf("story file %s not found", s.filepath) +func (s *StoryService) Update(ctx context.Context) error { + if err := s.storyStorage.Save(ctx, s.story); err != nil { + return err } - log.Printf("load story from: %s", s.filepath) - story := &Story{} - if err := json.Unmarshal(data, story); err != nil { + story, err := s.storyStorage.Load(ctx) + if err != nil { return err } s.story = story return nil } -func (s *StoryService) Save() error { - story := s.story - data, err := json.Marshal(story) - if err != nil { - return err - } - if err := os.WriteFile(s.filepath, data, 0x777); err != nil { - return err - } - log.Printf("save story to: %s", s.filepath) - return nil -} - -func (s *StoryService) Update() error { - if err := s.Save(); err != nil { - return err - } - return s.Load() -} - func (s *StoryService) GetPlace(code string) *Place { if strings.HasPrefix(code, "[") || strings.HasSuffix(code, "]") { return &Place{ @@ -107,7 +83,7 @@ func (s *StoryService) GetPlace(code string) *Place { } } -func (s *StoryService) UpdatePlace(code string, node *GraphNode) error { +func (s *StoryService) UpdatePlace(ctx context.Context, code string, node *GraphNode) error { if code != "" && node.Code == "" { for i := range s.story.Places { if s.story.Places[i].Code == code { @@ -115,7 +91,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error { break } } - s.Update() + s.Update(ctx) return nil } nodeApplications := make([]*Application, 0, len(node.Applications)) @@ -137,7 +113,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error { Applications: nodeApplications, }, ) - s.Update() + s.Update(ctx) return nil } if code == "" || node.Code == "" { @@ -170,7 +146,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error { } } } - s.Update() + s.Update(ctx) return nil } diff --git a/internal/services/story_storage/service.go b/internal/services/story_storage/service.go new file mode 100644 index 0000000..41d604f --- /dev/null +++ b/internal/services/story_storage/service.go @@ -0,0 +1,45 @@ +package story_storage + +import ( + "context" + "encoding/json" + "evening_detective/internal/services/story_service" + "fmt" + "log" + "os" +) + +type service struct { + filepath string +} + +func NewStoryStorage(filepath string) story_service.IStoryStorage { + return &service{ + filepath: filepath, + } +} + +func (s *service) Load(ctx context.Context) (*story_service.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{} + if err := json.Unmarshal(data, story); err != nil { + return nil, err + } + return story, nil +} + +func (s *service) Save(ctx context.Context, story *story_service.Story) error { + data, err := json.Marshal(story) + if err != nil { + return err + } + if err := os.WriteFile(s.filepath, data, 0x777); err != nil { + return err + } + log.Printf("save story to: %s", s.filepath) + return nil +}