add application number

This commit is contained in:
2026-03-16 01:56:50 +07:00
parent 3a30123096
commit 825d330056
11 changed files with 99 additions and 12 deletions
+3
View File
@@ -20,5 +20,8 @@ func (s *service) GetTeamClientLink(name string, password string) string {
}
func (s *service) GetImageLink(name string) string {
if len(name) == 0 {
return ""
}
return fmt.Sprintf("%s/%s", s.host, name)
}
+1
View File
@@ -38,6 +38,7 @@ func mapPlaceToProtoAction(place *story_service_models.Place) *proto.Action {
func mapStoryApplicationToProtoApplication(application *story_service_models.Application) *proto.Application {
return &proto.Application{
Name: application.Name,
Number: application.Number,
}
}
@@ -1,11 +1,34 @@
package models
import "fmt"
type Application struct {
Name string `json:"name"`
Name string `json:"name"`
Number string `json:"-"`
}
func NewApplication(name string) *Application {
return &Application{
func NewApplication(
name string,
opts ...ApplicationOpt,
) *Application {
application := &Application{
Name: name,
}
for _, opt := range opts {
opt(application)
}
return application
}
type ApplicationOpt func(application *Application) error
func WithApplicationNumber(number int) ApplicationOpt {
return func(application *Application) error {
application.SetNumber(number)
return nil
}
}
func (a *Application) SetNumber(number int) {
a.Number = fmt.Sprintf("%d", number)
}
@@ -95,8 +95,21 @@ func (s *StoryService) GetPlaces(codes []string) []*models.Place {
places := make([]*models.Place, 0, 100)
mOpen := map[string]any{}
mDeleted := map[string]any{}
applicationNumber := 1
applicationsMap := make(map[string]interface{}, 10)
for i, code := range codes {
place := s.GetPlace(code)
for i, application := range place.Applications {
if _, ok := applicationsMap[application.Name]; ok {
place.Applications = append(place.Applications[:i], place.Applications[i+1:]...)
if len(place.Applications) == 0 {
place.Applications = nil
}
}
applicationsMap[application.Name] = struct{}{}
application.SetNumber(applicationNumber)
applicationNumber++
}
if _, ok := mOpen[place.Code]; place.Hidden && !ok {
place = models.NewNotFoundPlace(place.Code)
}
@@ -456,6 +456,40 @@ func TestStoryService_GetPlaces(t *testing.T) {
models.NewNotFoundPlace("Ы-2"),
},
},
{
name: "Улики не повторяются",
story: &models.Story{
Places: []*models.Place{
models.NewPlace(
"Ы",
"Название",
"Текст",
models.WithPlaceApplication(
models.NewApplication("Название"),
),
),
},
},
codes: []string{"Ы", "Ы"},
want: []*models.Place{
models.NewPlace(
"Ы",
"Название",
"Текст",
models.WithPlaceApplication(
models.NewApplication(
"Название",
models.WithApplicationNumber(1),
),
),
),
models.NewPlace(
"Ы",
"Название",
"Текст",
),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {