This commit is contained in:
2025-05-19 03:18:22 +07:00
parent 107504317e
commit e409a69a54
23 changed files with 336 additions and 199 deletions
+30 -9
View File
@@ -2,11 +2,12 @@ package services
import (
"context"
"encoding/json"
"evening_detective/internal/models"
"evening_detective/internal/modules/password"
"evening_detective/internal/services/story_service"
"evening_detective/proto"
"strconv"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
@@ -51,13 +52,18 @@ func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*pro
}
actions := []*models.Action{
{
Place: place.Code,
TeamID: team.ID,
Place: place.Code,
TeamID: team.ID,
Applications: mapStoryApplicationsToApplications(place.Applications),
},
}
if err := s.repository.AddActions(ctx, actions); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
if err := s.repository.AddApplications(ctx, actions); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
log(team, "add action", actions)
return &proto.AddActionRsp{}, nil
}
@@ -86,7 +92,7 @@ func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.G
newAction.Name = place.Name
newAction.Applications = make([]*proto.Application, 0, len(place.Applications))
for _, application := range place.Applications {
newAction.Applications = append(newAction.Applications, mapApplicationToProtoApplication(application))
newAction.Applications = append(newAction.Applications, mapStoryApplicationToProtoApplication(application))
}
res = append(res, newAction)
}
@@ -104,7 +110,18 @@ func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.G
}
res := make([]*proto.TeamAdvanced, 0, len(teams))
for _, team := range teams {
res = append(res, mapTeamsToTeamAdvanced(team))
newTeam := mapTeamsToTeamAdvanced(team)
actions, err := s.repository.GetActions(ctx, team.ID)
if err != nil {
return nil, err
}
newTeam.SpendTime = int64(20 * len(actions))
applications, err := s.repository.GetApplications(ctx, team.ID, "NEW")
if err != nil {
return nil, err
}
newTeam.Applications = mapApplicationsToProtoApplications(applications)
res = append(res, newTeam)
}
return &proto.GetTeamsRsp{Teams: res}, err
}
@@ -137,10 +154,7 @@ func (s *Services) getTeam(ctx context.Context) (*models.Team, error) {
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "error creds")
}
teamId, err := strconv.Atoi(teamIdArr[0])
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "error creds")
}
teamId := teamIdArr[0]
passwordArr, ok := md["password"]
if !ok {
@@ -154,3 +168,10 @@ func (s *Services) getTeam(ctx context.Context) (*models.Team, error) {
}
return team, nil
}
func log(team *models.Team, action string, v any) {
vJson, err := json.Marshal(v)
if err == nil {
fmt.Printf("Team %s: %s %s\n", team.Name, action, string(vJson))
}
}