generated from VLADIMIR/template
add update node
This commit is contained in:
@@ -11,23 +11,24 @@ import (
|
||||
)
|
||||
|
||||
type StoryService struct {
|
||||
story *Story
|
||||
filepath string
|
||||
story *Story
|
||||
}
|
||||
|
||||
func NewStoryService(filepath string) (*StoryService, error) {
|
||||
s := &StoryService{}
|
||||
if err := s.Load(filepath); err != nil {
|
||||
s := &StoryService{filepath: filepath}
|
||||
if err := s.Load(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *StoryService) Load(filepath string) error {
|
||||
data, err := os.ReadFile(filepath)
|
||||
func (s *StoryService) Load() error {
|
||||
data, err := os.ReadFile(s.filepath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("story file %s not found", filepath)
|
||||
return fmt.Errorf("story file %s not found", s.filepath)
|
||||
}
|
||||
log.Printf("load story from: %s", filepath)
|
||||
log.Printf("load story from: %s", s.filepath)
|
||||
story := &Story{}
|
||||
if err := json.Unmarshal(data, story); err != nil {
|
||||
return err
|
||||
@@ -36,19 +37,26 @@ func (s *StoryService) Load(filepath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StoryService) Save(filepath string) error {
|
||||
func (s *StoryService) Save() error {
|
||||
story := s.story
|
||||
data, err := json.Marshal(story)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(filepath, data, 0x777); err != nil {
|
||||
if err := os.WriteFile(s.filepath, data, 0x777); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("save story to: %s", filepath)
|
||||
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{
|
||||
@@ -77,6 +85,22 @@ func (s *StoryService) GetPlace(code string) *Place {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StoryService) UpdatePlace(node *GraphNode) error {
|
||||
for i := range s.story.Places {
|
||||
if s.story.Places[i].Code == node.Label {
|
||||
s.story.Places[i] = &Place{
|
||||
Code: s.story.Places[i].Code,
|
||||
Name: node.Name,
|
||||
Text: node.Text,
|
||||
Applications: s.story.Places[i].Applications,
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
s.Update()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
||||
m := make(map[string]int32, len(s.story.Places))
|
||||
nodes := make([]*GraphNode, 0, len(s.story.Places))
|
||||
|
||||
Reference in New Issue
Block a user