From 73838d777347eb51a6cd52b42a92110016e3d254 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Sat, 17 May 2025 12:30:24 +0700 Subject: [PATCH] add logic --- internal/services/mappers.go | 9 ++++++++- internal/services/services.go | 10 +++++++++- internal/services/story_service/service.go | 21 ++++++++++++++++---- story/story.json | 23 +++++++++++++++++++++- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/internal/services/mappers.go b/internal/services/mappers.go index 6c66cf5..b058f2f 100644 --- a/internal/services/mappers.go +++ b/internal/services/mappers.go @@ -2,6 +2,7 @@ package services import ( "evening_detective/internal/models" + "evening_detective/internal/services/story_service" "evening_detective/proto" ) @@ -28,7 +29,13 @@ func mapProtoTeamsToTeam(team *proto.Team) *models.Team { func mapActionToProtoAction(action *models.Action) *proto.Action { return &proto.Action{ - Id: action.ID, + Id: action.ID, Place: action.Place, } } + +func mapApplicationToProtoApplication(application *story_service.Application) *proto.Application { + return &proto.Application{ + Name: application.Name, + } +} diff --git a/internal/services/services.go b/internal/services/services.go index 0f4cfb5..be39ae5 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -45,9 +45,13 @@ func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*pro if err != nil { return nil, err } + place, err := s.storyService.GetPlace(req.Place) + if err != nil { + return nil, err + } actions := []*models.Action{ { - Place: req.Place, + Place: place.Code, TeamID: team.ID, }, } @@ -79,6 +83,10 @@ func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.G return nil, err } newAction.Text = place.Text + newAction.Applications = make([]*proto.Application, 0, len(place.Applications)) + for _, application := range place.Applications { + newAction.Applications = append(newAction.Applications, mapApplicationToProtoApplication(application)) + } res = append(res, newAction) } return &proto.GetTeamRsp{Actions: res}, err diff --git a/internal/services/story_service/service.go b/internal/services/story_service/service.go index 8089960..e4a2bee 100644 --- a/internal/services/story_service/service.go +++ b/internal/services/story_service/service.go @@ -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, "-", "") } diff --git a/story/story.json b/story/story.json index 71b6d42..49f0bde 100644 --- a/story/story.json +++ b/story/story.json @@ -5,9 +5,30 @@ "text": "Вас приветствуют волонтеры, говорят что игорек где-то здесь, но точно они не знают.", "applications": [ { - "name": "Карта" + "name": "Карта площадки" } ] + }, + { + "code": "М-1", + "text": "Ребята делают мод для майнкрафта, не стоит их беспокоить." + }, + { + "code": "О-1", + "text": "Организаторы говорят что игорек убежал на пробежку и если поторопиться его можно успеть поймать.", + "applications": [ + { + "name": "Карта базы" + } + ] + }, + { + "code": "Б-1", + "text": "Тут его нет, вы чествуете только прохладный ветер." + }, + { + "code": "Б-2", + "text": "А вот и он уже делает зарядку!" } ] }