add actions and auth

This commit is contained in:
2025-05-17 12:08:44 +07:00
parent 4bd18ee756
commit e1b342d12c
14 changed files with 487 additions and 246 deletions
+73 -5
View File
@@ -4,19 +4,27 @@ import (
"context"
"evening_detective/internal/models"
"evening_detective/internal/modules/password"
"evening_detective/internal/services/story_service"
"evening_detective/proto"
"strconv"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type Services struct {
repository *Repository
repository *Repository
storyService *story_service.StoryService
}
func NewServices(repository *Repository) *Services {
func NewServices(
repository *Repository,
storyService *story_service.StoryService,
) *Services {
return &Services{
repository: repository,
repository: repository,
storyService: storyService,
}
}
@@ -33,7 +41,20 @@ func (s *Services) GameStart(ctx context.Context, req *proto.GameStartReq) (*pro
}
func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*proto.AddActionRsp, error) {
panic("unimplemented")
team, err := s.getTeam(ctx)
if err != nil {
return nil, err
}
actions := []*models.Action{
{
Place: req.Place,
TeamID: team.ID,
},
}
if err := s.repository.AddActions(ctx, actions); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &proto.AddActionRsp{}, nil
}
func (s *Services) DeleteTeams(ctx context.Context, req *proto.DeleteTeamsReq) (*proto.DeleteTeamsRsp, error) {
@@ -41,7 +62,26 @@ func (s *Services) DeleteTeams(ctx context.Context, req *proto.DeleteTeamsReq) (
}
func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.GetTeamRsp, error) {
panic("unimplemented")
team, err := s.getTeam(ctx)
if err != nil {
return nil, err
}
actions, err := s.repository.GetActions(ctx, team.ID)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
res := make([]*proto.Action, 0, len(actions))
for _, action := range actions {
newAction := mapActionToProtoAction(action)
place, err := s.storyService.GetPlace(action.Place)
if err != nil {
return nil, err
}
newAction.Text = place.Text
res = append(res, newAction)
}
return &proto.GetTeamRsp{Actions: res}, err
}
func (s *Services) GetTeamsCSV(ctx context.Context, req *proto.GetTeamsCSVReq) (*proto.GetTeamsCSVRsp, error) {
@@ -77,3 +117,31 @@ func (s *Services) AddTeams(ctx context.Context, req *proto.AddTeamsReq) (*proto
}
return &proto.AddTeamsRsp{Teams: res}, err
}
func (s *Services) getTeam(ctx context.Context) (*models.Team, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "error creds")
}
teamIdArr, ok := md["team-id"]
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")
}
passwordArr, ok := md["password"]
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "error creds")
}
password := passwordArr[0]
team, err := s.repository.GetTeam(ctx, teamId, password)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, err.Error())
}
return team, nil
}