Skip to content

Commit

Permalink
add: スコアランキングに全件数の情報を含めるようにしました
Browse files Browse the repository at this point in the history
  • Loading branch information
shin0729 committed Apr 5, 2024
1 parent 022da64 commit e3ce8e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
8 changes: 6 additions & 2 deletions typing-server/api/handler/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ func GetScoresRanking(w http.ResponseWriter, r *http.Request) {
limit = 10
}

rankings, err := service.GetScoresRanking(ctx, entClient, sortBy, start, limit)
rankings, totalCount, err := service.GetScoresRanking(ctx, entClient, sortBy, start, limit)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}


w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(rankings)
err = json.NewEncoder(w).Encode(map[string]interface{}{
"rankings": rankings,
"total_count": totalCount,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
11 changes: 7 additions & 4 deletions typing-server/api/repository/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/su-its/typing/typing-server/domain/repository/ent/user"
)

func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, start, limit int) ([]*model.ScoreRanking, error) {
func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, start, limit int) ([]*model.ScoreRanking, int, error) {
var scores []*ent.Score

// entのクエリを使用してスコアを取得
Expand All @@ -32,17 +32,20 @@ func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, st
case "keystrokes":
query = query.Where(score.IsMaxKeystrokes(true))
default:
return nil, fmt.Errorf("invalid sort by parameter: %s", sortBy)
return nil, 0, fmt.Errorf("invalid sort by parameter: %s", sortBy)
}

//全件数の取得
count := query.CountX(ctx)

// フラグでフィルタリングされたスコアを取得
scores, err := query.
Limit(limit).
Offset(start - 1).
All(ctx)

if err != nil {
return nil, err
return nil, 0, err
}

var rankings []*model.ScoreRanking
Expand Down Expand Up @@ -89,7 +92,7 @@ func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, st
rankings = append(rankings, ranking)
}

return rankings, nil
return rankings, count, nil
}
func CreateScore(ctx context.Context, client *ent.Client, userID uuid.UUID, keystrokes int, accuracy float64) error {
// トランザクションを開始
Expand Down
9 changes: 4 additions & 5 deletions typing-server/api/service/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (
"github.com/su-its/typing/typing-server/domain/repository/ent"
)

func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, start, limit int) ([]*model.ScoreRanking, error) {
rankings, err := repository.GetScoresRanking(ctx, client, sortBy, start, limit)
func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, start, limit int) ([]*model.ScoreRanking, int, error) {
rankings, totalCount, err := repository.GetScoresRanking(ctx, client, sortBy, start, limit)
if err != nil {
return nil, err
return nil, 0, err
}

return rankings, nil
return rankings, totalCount, nil
}

func CreateScore(ctx context.Context, client *ent.Client, userID uuid.UUID, keystrokes int, accuracy float64) error {
Expand Down

0 comments on commit e3ce8e2

Please sign in to comment.