generated from VLADIMIR/template
156 lines
4.3 KiB
Go
156 lines
4.3 KiB
Go
package services
|
|
|
|
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
|
|
storyService *story_service.StoryService
|
|
}
|
|
|
|
func NewServices(
|
|
repository *Repository,
|
|
storyService *story_service.StoryService,
|
|
) *Services {
|
|
return &Services{
|
|
repository: repository,
|
|
storyService: storyService,
|
|
}
|
|
}
|
|
|
|
func (s *Services) GiveApplications(ctx context.Context, req *proto.GiveApplicationsReq) (*proto.GiveApplicationsRsp, error) {
|
|
panic("unimplemented")
|
|
}
|
|
|
|
func (s *Services) GameStop(ctx context.Context, req *proto.GameStopReq) (*proto.GameStopRsp, error) {
|
|
panic("unimplemented")
|
|
}
|
|
|
|
func (s *Services) GameStart(ctx context.Context, req *proto.GameStartReq) (*proto.GameStartRsp, error) {
|
|
panic("unimplemented")
|
|
}
|
|
|
|
func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*proto.AddActionRsp, error) {
|
|
team, err := s.getTeam(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
place, err := s.storyService.GetPlace(req.Place)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
actions := []*models.Action{
|
|
{
|
|
Place: place.Code,
|
|
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) {
|
|
panic("unimplemented")
|
|
}
|
|
|
|
func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.GetTeamRsp, error) {
|
|
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
|
|
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
|
|
}
|
|
|
|
func (s *Services) GetTeamsCSV(ctx context.Context, req *proto.GetTeamsCSVReq) (*proto.GetTeamsCSVRsp, error) {
|
|
panic("unimplemented")
|
|
}
|
|
|
|
func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.GetTeamsRsp, error) {
|
|
teams, err := s.repository.GetTeams(ctx)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
}
|
|
res := make([]*proto.TeamAdvanced, 0, len(teams))
|
|
for _, team := range teams {
|
|
res = append(res, mapTeamsToTeamAdvanced(team))
|
|
}
|
|
return &proto.GetTeamsRsp{Teams: res}, err
|
|
}
|
|
|
|
func (s *Services) AddTeams(ctx context.Context, req *proto.AddTeamsReq) (*proto.AddTeamsRsp, error) {
|
|
inTeams := make([]*models.Team, 0, len(req.Teams))
|
|
for _, team := range req.Teams {
|
|
t := mapProtoTeamsToTeam(team)
|
|
t.Password = password.GenPass(8)
|
|
inTeams = append(inTeams, t)
|
|
}
|
|
teams, err := s.repository.AddTeams(ctx, inTeams)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
}
|
|
res := make([]*proto.TeamFull, 0, len(teams))
|
|
for _, team := range teams {
|
|
res = append(res, mapTeamsToTeamFull(team))
|
|
}
|
|
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
|
|
}
|