generated from VLADIMIR/template
add logic
This commit is contained in:
@@ -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, "-", "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user