add story service

This commit is contained in:
Владимир Фёдоров 2026-03-07 04:34:58 +07:00
parent 9e0a19d25a
commit 645f6a7246
5 changed files with 80 additions and 46 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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
}

View File

@ -2,12 +2,8 @@ package story_service
import (
"context"
"encoding/json"
"evening_detective/internal/modules/cleaner"
"evening_detective/internal/modules/formatter"
"fmt"
"log"
"os"
"regexp"
"strings"
)
@ -15,60 +11,40 @@ import (
type StoryService struct {
cleaner cleaner.ICleaner
formatter formatter.IFormatter
filepath string
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,
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
}

View File

@ -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
}