diff --git a/internal/services/story_service/cleaners.go b/internal/services/story_service/cleaners.go new file mode 100644 index 0000000..ff11278 --- /dev/null +++ b/internal/services/story_service/cleaners.go @@ -0,0 +1,39 @@ +package story_service + +import "strings" + +var ( + replaceMap = map[string]string{ + "a": "а", + "e": "е", + "o": "о", + "c": "с", + "p": "р", + "x": "х", + "y": "у", + "k": "к", + "m": "м", + "t": "т", + "h": "н", + "b": "в", + "u": "и", + } +) + +func clearMatch(s string) string { + s = strings.TrimPrefix(s, "(") + s = strings.TrimPrefix(s, "[") + s = strings.TrimSuffix(s, ")") + s = strings.TrimSuffix(s, "]") + return clearCode(s) +} + +func clearCode(code string) string { + code = strings.ToLower(code) + code = strings.TrimSpace(code) + code = strings.ReplaceAll(code, "-", "") + for latin, cyrillic := range replaceMap { + code = strings.ReplaceAll(code, latin, cyrillic) + } + return code +} diff --git a/internal/services/story_service/data_models.go b/internal/services/story_service/data_models.go new file mode 100644 index 0000000..ab6e6da --- /dev/null +++ b/internal/services/story_service/data_models.go @@ -0,0 +1,16 @@ +package story_service + +type Story struct { + Places []*Place `json:"places"` +} + +type Place struct { + Code string `json:"code"` + Name string `json:"name"` + Text string `json:"text"` + Applications []*Application `json:"applications"` +} + +type Application struct { + Name string `json:"name"` +} diff --git a/internal/services/story_service/dto_models.go b/internal/services/story_service/dto_models.go new file mode 100644 index 0000000..ab3c407 --- /dev/null +++ b/internal/services/story_service/dto_models.go @@ -0,0 +1,23 @@ +package story_service + +type Graph struct { + Nodes []*GraphNode + Edges []*GraphEdge +} + +type GraphNode struct { + ID int32 + Label string + Name string + Text string + Applications []*GraphApplication +} + +type GraphEdge struct { + From int32 + To int32 +} + +type GraphApplication struct { + Name string +} diff --git a/internal/services/story_service/service.go b/internal/services/story_service/service.go index 99372fc..f820010 100644 --- a/internal/services/story_service/service.go +++ b/internal/services/story_service/service.go @@ -10,61 +10,6 @@ import ( "strings" ) -var ( - replaceMap = map[string]string{ - "a": "а", - "e": "е", - "o": "о", - "c": "с", - "p": "р", - "x": "х", - "y": "у", - "k": "к", - "m": "м", - "t": "т", - "h": "н", - "b": "в", - "u": "и", - } -) - -type Story struct { - Places []*Place `json:"places"` -} - -type Place struct { - Code string `json:"code"` - Name string `json:"name"` - Text string `json:"text"` - Applications []*Application `json:"applications"` -} - -type Application struct { - Name string `json:"name"` -} - -type Graph struct { - Nodes []*GraphNode - Edges []*GraphEdge -} - -type GraphNode struct { - ID int32 - Label string - Name string - Text string - Applications []*GraphApplication -} - -type GraphEdge struct { - From int32 - To int32 -} - -type GraphApplication struct { - Name string -} - type StoryService struct { story *Story } @@ -141,10 +86,13 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph { matches := re.FindAllString(place.Text, -1) for _, match := range matches { - edges = append(edges, &GraphEdge{ - From: m[clearCode(place.Code)], - To: m[clearMatch(match)], - }) + edges = append( + edges, + &GraphEdge{ + From: m[clearCode(place.Code)], + To: m[clearMatch(match)], + }, + ) } } @@ -153,21 +101,3 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph { Edges: edges, } } - -func clearMatch(s string) string { - s = strings.TrimPrefix(s, "(") - s = strings.TrimPrefix(s, "[") - s = strings.TrimSuffix(s, ")") - s = strings.TrimSuffix(s, "]") - return clearCode(s) -} - -func clearCode(code string) string { - code = strings.ToLower(code) - code = strings.TrimSpace(code) - code = strings.ReplaceAll(code, "-", "") - for latin, cyrillic := range replaceMap { - code = strings.ReplaceAll(code, latin, cyrillic) - } - return code -}