Skip to content

Commit

Permalink
refactor:service層のDB操作をしている部分をrepositoryに委譲しました
Browse files Browse the repository at this point in the history
  • Loading branch information
shin0729 committed Apr 8, 2024
1 parent 46b732c commit 60c0e17
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
48 changes: 48 additions & 0 deletions typing-server/api/repository/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,51 @@ func GetMaxScoreByUserID(ctx context.Context, client *ent.Client, userID uuid.UU

return maxScore, nil
}

func CountHigherScores(ctx context.Context, client *ent.Client, userMaxScore *ent.Score, sortBy string) (int, error) {
var rank int
var err error

switch sortBy {
case "accuracy":
rank, err = client.Score.Query().
Where(
score.And(
score.KeystrokesGTE(120),
score.AccuracyGTE(0.95),
score.Or(
score.AccuracyGT(userMaxScore.Accuracy),
score.And(
score.AccuracyEQ(userMaxScore.Accuracy),
score.KeystrokesGT(userMaxScore.Keystrokes),
),
),
),
).
Count(ctx)
case "keystrokes":
rank, err = client.Score.Query().
Where(
score.And(
score.KeystrokesGTE(120),
score.AccuracyGTE(0.95),
score.Or(
score.KeystrokesGT(userMaxScore.Keystrokes),
score.And(
score.KeystrokesEQ(userMaxScore.Keystrokes),
score.AccuracyGT(userMaxScore.Accuracy),
),
),
),
).
Count(ctx)
default:
return 0, fmt.Errorf("invalid sort by parameter: %s", sortBy)
}

if err != nil {
return 0, err
}

return rank, nil
}
43 changes: 1 addition & 42 deletions typing-server/api/service/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package service
import (
"context"

"fmt"

"github.com/google/uuid"
"github.com/su-its/typing/typing-server/api/repository"
"github.com/su-its/typing/typing-server/domain/model"
"github.com/su-its/typing/typing-server/domain/repository/ent"
"github.com/su-its/typing/typing-server/domain/repository/ent/score"
)

func GetScoresRanking(ctx context.Context, client *ent.Client, request *model.GetScoresRankingRequest) (*model.GetScoresRankingResponse, error) {
Expand Down Expand Up @@ -40,45 +37,7 @@ func GetMyScoreRanking(ctx context.Context, client *ent.Client, userID uuid.UUID
}

// ユーザーの最大スコアより上位のスコアをカウント
var rank int

switch sortBy {
case "accuracy":
rank, err = client.Score.Query().
Where(
score.And(
score.KeystrokesGTE(120),
score.AccuracyGTE(0.95),
score.Or(
score.AccuracyGT(userMaxScore.Accuracy),
score.And(
score.AccuracyEQ(userMaxScore.Accuracy),
score.KeystrokesGT(userMaxScore.Keystrokes),
),
),
),
).
Count(ctx)
case "keystrokes":
rank, err = client.Score.Query().
Where(
score.And(
score.KeystrokesGTE(120),
score.AccuracyGTE(0.95),
score.Or(
score.KeystrokesGT(userMaxScore.Keystrokes),
score.And(
score.KeystrokesEQ(userMaxScore.Keystrokes),
score.AccuracyGT(userMaxScore.Accuracy),
),
),
),
).
Count(ctx)
default:
return 0, fmt.Errorf("invalid sort by parameter: %s", sortBy)
}

rank, err := repository.CountHigherScores(ctx, client, userMaxScore, sortBy)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 60c0e17

Please sign in to comment.