add db module

This commit is contained in:
2026-03-02 02:50:43 +07:00
parent 5ab7ae0fcd
commit 2bc2bf45c7
4 changed files with 125 additions and 95 deletions
+18 -17
View File
@@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"evening_detective/internal/models"
"evening_detective/internal/modules/db"
"evening_detective/internal/modules/link"
"evening_detective/internal/modules/password"
"evening_detective/internal/modules/pdf"
@@ -19,7 +20,7 @@ import (
)
type Services struct {
repository *Repository
dbService db.IDBService
storyService *story_service.StoryService
linkService link.ILinkService
passwordGenerator password.IPasswordGenerator
@@ -28,7 +29,7 @@ type Services struct {
}
func NewServices(
repository *Repository,
dbService db.IDBService,
storyService *story_service.StoryService,
linkService link.ILinkService,
passwordGenerator password.IPasswordGenerator,
@@ -36,7 +37,7 @@ func NewServices(
clientHost string,
) *Services {
return &Services{
repository: repository,
dbService: dbService,
storyService: storyService,
linkService: linkService,
passwordGenerator: passwordGenerator,
@@ -47,14 +48,14 @@ func NewServices(
func (s *Services) GiveApplications(ctx context.Context, req *proto.GiveApplicationsReq) (*proto.GiveApplicationsRsp, error) {
applications := mapProtoApplicationsToApplications(req.Applications)
if err := s.repository.GiveApplications(ctx, req.TeamId, applications); err != nil {
if err := s.dbService.GiveApplications(ctx, req.TeamId, applications); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &proto.GiveApplicationsRsp{}, nil
}
func (s *Services) GetGame(ctx context.Context, _ *proto.GetGameReq) (*proto.GetGameRsp, error) {
game, err := s.repository.GetGame(ctx)
game, err := s.dbService.GetGame(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
@@ -66,14 +67,14 @@ func (s *Services) GetGame(ctx context.Context, _ *proto.GetGameReq) (*proto.Get
}
func (s *Services) GameStart(ctx context.Context, _ *proto.GameStartReq) (*proto.GameStartRsp, error) {
if err := s.repository.GameUpdateState(ctx, "RUN"); err != nil {
if err := s.dbService.UpdateGameState(ctx, "RUN"); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &proto.GameStartRsp{}, nil
}
func (s *Services) GameStop(ctx context.Context, req *proto.GameStopReq) (*proto.GameStopRsp, error) {
if err := s.repository.GameUpdateState(ctx, "STOP"); err != nil {
if err := s.dbService.UpdateGameState(ctx, "STOP"); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &proto.GameStopRsp{}, nil
@@ -92,10 +93,10 @@ func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*pro
Applications: mapStoryApplicationsToApplications(place.Applications),
},
}
if err := s.repository.AddActions(ctx, team.ID, actions); err != nil {
if err := s.dbService.AddActions(ctx, team.ID, actions); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
currentApplications, err := s.repository.GetApplications(ctx, team.ID)
currentApplications, err := s.dbService.GetApplications(ctx, team.ID)
if err != nil {
return nil, err
}
@@ -115,7 +116,7 @@ func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*pro
}
}
if err := s.repository.AddApplications(ctx, team.ID, newApplications); err != nil {
if err := s.dbService.AddApplications(ctx, team.ID, newApplications); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
addLog(team, "add action", actions)
@@ -128,7 +129,7 @@ func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.G
return nil, err
}
actions, err := s.repository.GetActions(ctx, team.ID)
actions, err := s.dbService.GetActions(ctx, team.ID)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
@@ -155,20 +156,20 @@ func (s *Services) GetTeamsCSV(ctx context.Context, req *proto.GetTeamsCSVReq) (
}
func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.GetTeamsRsp, error) {
teams, err := s.repository.GetTeams(ctx)
teams, err := s.dbService.GetTeams(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
res := make([]*proto.TeamAdvanced, 0, len(teams))
for _, team := range teams {
newTeam := mapTeamsToTeamAdvanced(team)
actions, err := s.repository.GetActions(ctx, team.ID)
actions, err := s.dbService.GetActions(ctx, team.ID)
if err != nil {
return nil, err
}
newTeam.Url = s.linkService.GetTeamClientLink(s.clientHost, team.Name, team.Password)
newTeam.SpendTime = int64(len(actions))
currentApplications, err := s.repository.GetApplicationsByState(ctx, team.ID, "NEW")
currentApplications, err := s.dbService.GetApplicationsByState(ctx, team.ID, "NEW")
if err != nil {
return nil, err
}
@@ -185,7 +186,7 @@ func (s *Services) AddTeams(ctx context.Context, req *proto.AddTeamsReq) (*proto
t.Password = s.passwordGenerator.GeneratePassword(8)
inTeams = append(inTeams, t)
}
teams, err := s.repository.AddTeams(ctx, inTeams)
teams, err := s.dbService.AddTeams(ctx, inTeams)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
@@ -199,7 +200,7 @@ func (s *Services) AddTeams(ctx context.Context, req *proto.AddTeamsReq) (*proto
func (s *Services) DownloadTeamsQrCodesFile(ctx context.Context, req *proto.DownloadTeamsQrCodesFileReq) (*proto.DownloadTeamsQrCodesFileRsp, error) {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
teams, err := s.repository.GetTeams(ctx)
teams, err := s.dbService.GetTeams(ctx)
if err != nil {
return nil, err
}
@@ -293,7 +294,7 @@ func (s *Services) getTeam(ctx context.Context) (*models.Team, error) {
}
password := passwordArr[0]
team, err := s.repository.GetTeam(ctx, teamId, password)
team, err := s.dbService.GetTeam(ctx, teamId, password)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, err.Error())
}