add logic

This commit is contained in:
2025-05-17 12:30:24 +07:00
parent e1b342d12c
commit 73838d7773
4 changed files with 56 additions and 7 deletions
+17 -4
View File
@@ -3,7 +3,9 @@ package story_service
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
)
type Story struct {
@@ -11,8 +13,13 @@ type Story struct {
}
type Place struct {
Code string `json:"code"`
Text string `json:"text"`
Code string `json:"code"`
Text string `json:"text"`
Applications []*Application `json:"applications"`
}
type Application struct {
Name string `json:"name"`
}
type StoryService struct {
@@ -32,10 +39,16 @@ func NewStoryService() (*StoryService, error) {
}
func (s *StoryService) GetPlace(code string) (*Place, error) {
code = clearCode(code)
for _, place := range s.story.Places {
if place.Code == code {
if clearCode(place.Code) == code {
return place, nil
}
}
return nil, errors.New("place not found")
return nil, errors.New(fmt.Sprintf("place not found: %s", code))
}
func clearCode(code string) string {
code = strings.ToLower(code)
return strings.ReplaceAll(code, "-", "")
}