generated from VLADIMIR/template
add story service
This commit is contained in:
parent
9e0a19d25a
commit
645f6a7246
@ -13,6 +13,7 @@ import (
|
|||||||
"evening_detective/internal/services/link"
|
"evening_detective/internal/services/link"
|
||||||
"evening_detective/internal/services/pdf"
|
"evening_detective/internal/services/pdf"
|
||||||
"evening_detective/internal/services/story_service"
|
"evening_detective/internal/services/story_service"
|
||||||
|
"evening_detective/internal/services/story_storage"
|
||||||
proto "evening_detective/proto"
|
proto "evening_detective/proto"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
@ -54,7 +55,9 @@ func main() {
|
|||||||
|
|
||||||
storyFilepath := config.GetStoryFilepath()
|
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 {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -230,7 +230,7 @@ func (s *Services) UpdateNode(ctx context.Context, req *proto.UpdateNodeReq) (*p
|
|||||||
Text: req.Node.Text,
|
Text: req.Node.Text,
|
||||||
Applications: applications,
|
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 nil, err
|
||||||
}
|
}
|
||||||
return &proto.UpdateNodeRsp{}, nil
|
return &proto.UpdateNodeRsp{}, nil
|
||||||
|
|||||||
10
internal/services/story_service/dependency.go
Normal file
10
internal/services/story_service/dependency.go
Normal 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
|
||||||
|
}
|
||||||
@ -2,12 +2,8 @@ package story_service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"evening_detective/internal/modules/cleaner"
|
"evening_detective/internal/modules/cleaner"
|
||||||
"evening_detective/internal/modules/formatter"
|
"evening_detective/internal/modules/formatter"
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -15,60 +11,40 @@ import (
|
|||||||
type StoryService struct {
|
type StoryService struct {
|
||||||
cleaner cleaner.ICleaner
|
cleaner cleaner.ICleaner
|
||||||
formatter formatter.IFormatter
|
formatter formatter.IFormatter
|
||||||
filepath string
|
|
||||||
story *Story
|
story *Story
|
||||||
|
storyStorage IStoryStorage
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStoryService(
|
func NewStoryService(
|
||||||
cleaner cleaner.ICleaner,
|
cleaner cleaner.ICleaner,
|
||||||
formatter formatter.IFormatter,
|
formatter formatter.IFormatter,
|
||||||
filepath string,
|
storyStorage IStoryStorage,
|
||||||
) (*StoryService, error) {
|
) (*StoryService, error) {
|
||||||
s := &StoryService{
|
s := &StoryService{
|
||||||
cleaner: cleaner,
|
cleaner: cleaner,
|
||||||
formatter: formatter,
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
s.story = story
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StoryService) Load() error {
|
func (s *StoryService) Update(ctx context.Context) error {
|
||||||
data, err := os.ReadFile(s.filepath)
|
if err := s.storyStorage.Save(ctx, s.story); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return fmt.Errorf("story file %s not found", s.filepath)
|
|
||||||
}
|
}
|
||||||
log.Printf("load story from: %s", s.filepath)
|
story, err := s.storyStorage.Load(ctx)
|
||||||
story := &Story{}
|
if err != nil {
|
||||||
if err := json.Unmarshal(data, story); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.story = story
|
s.story = story
|
||||||
return nil
|
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 {
|
func (s *StoryService) GetPlace(code string) *Place {
|
||||||
if strings.HasPrefix(code, "[") || strings.HasSuffix(code, "]") {
|
if strings.HasPrefix(code, "[") || strings.HasSuffix(code, "]") {
|
||||||
return &Place{
|
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 == "" {
|
if code != "" && node.Code == "" {
|
||||||
for i := range s.story.Places {
|
for i := range s.story.Places {
|
||||||
if s.story.Places[i].Code == code {
|
if s.story.Places[i].Code == code {
|
||||||
@ -115,7 +91,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.Update()
|
s.Update(ctx)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
nodeApplications := make([]*Application, 0, len(node.Applications))
|
nodeApplications := make([]*Application, 0, len(node.Applications))
|
||||||
@ -137,7 +113,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
|||||||
Applications: nodeApplications,
|
Applications: nodeApplications,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
s.Update()
|
s.Update(ctx)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if code == "" || node.Code == "" {
|
if code == "" || node.Code == "" {
|
||||||
@ -170,7 +146,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.Update()
|
s.Update(ctx)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
45
internal/services/story_storage/service.go
Normal file
45
internal/services/story_storage/service.go
Normal 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
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user