generated from VLADIMIR/template
add update node
This commit is contained in:
@@ -187,7 +187,7 @@ func (s *Services) AddTeams(ctx context.Context, req *proto.AddTeamsReq) (*proto
|
||||
}
|
||||
|
||||
func (s *Services) DownloadTeamsQrCodesFile(ctx context.Context, req *proto.DownloadTeamsQrCodesFileReq) (*proto.DownloadTeamsQrCodesFileRsp, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
teams, err := s.repository.GetTeams(ctx)
|
||||
if err != nil {
|
||||
@@ -200,20 +200,43 @@ func (s *Services) DownloadTeamsQrCodesFile(ctx context.Context, req *proto.Down
|
||||
return &proto.DownloadTeamsQrCodesFileRsp{Result: b}, nil
|
||||
}
|
||||
|
||||
func (s *Services) UpdateNode(ctx context.Context, req *proto.UpdateNodeReq) (*proto.UpdateNodeRsp, error) {
|
||||
applications := make([]*story_service.GraphApplication, 0, len(req.Node.Applications))
|
||||
for _, application := range req.Node.Applications {
|
||||
applications = append(
|
||||
applications,
|
||||
&story_service.GraphApplication{
|
||||
Name: application.Name,
|
||||
},
|
||||
)
|
||||
}
|
||||
node := &story_service.GraphNode{
|
||||
ID: req.Node.Id,
|
||||
Label: req.Node.Label,
|
||||
Name: req.Node.Name,
|
||||
Text: req.Node.Text,
|
||||
Applications: applications,
|
||||
}
|
||||
if err := s.storyService.UpdatePlace(node); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &proto.UpdateNodeRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *Services) GetGraph(ctx context.Context, req *proto.GetGraphReq) (*proto.GetGraphRsp, error) {
|
||||
graph := s.storyService.GetGraph(ctx)
|
||||
nodes := make([]*proto.GetGraphRsp_Node, 0, len(graph.Nodes))
|
||||
nodes := make([]*proto.GraphNode, 0, len(graph.Nodes))
|
||||
for _, node := range graph.Nodes {
|
||||
applications := make([]*proto.GetGraphRsp_Application, 0, len(node.Applications))
|
||||
applications := make([]*proto.GraphApplication, 0, len(node.Applications))
|
||||
for _, application := range node.Applications {
|
||||
applications = append(
|
||||
applications,
|
||||
&proto.GetGraphRsp_Application{
|
||||
&proto.GraphApplication{
|
||||
Name: application.Name,
|
||||
},
|
||||
)
|
||||
}
|
||||
nodes = append(nodes, &proto.GetGraphRsp_Node{
|
||||
nodes = append(nodes, &proto.GraphNode{
|
||||
Id: node.ID,
|
||||
Label: node.Label,
|
||||
Name: node.Name,
|
||||
|
||||
@@ -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