Skip to content

Commit

Permalink
Merge pull request #95 from su-its/feat/backend/api/v1
Browse files Browse the repository at this point in the history
add: スコアランキングに全件数の情報を含めるようにしました
  • Loading branch information
KinjiKawaguchi authored Apr 5, 2024
2 parents 022da64 + 5913be3 commit 80adaf1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
11 changes: 9 additions & 2 deletions typing-server/api/handler/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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

func GetScoresRanking(w http.ResponseWriter, r *http.Request) {
Expand All @@ -29,14 +30,20 @@ func GetScoresRanking(w http.ResponseWriter, r *http.Request) {
limit = 10
}

rankings, err := service.GetScoresRanking(ctx, entClient, sortBy, start, limit)
request := model.GetScoresRankingRequest{
SortBy: sortBy,
Start: start,
Limit: limit,
}

response, err := service.GetScoresRanking(ctx, entClient, &request)
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(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
24 changes: 15 additions & 9 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, request *model.GetScoresRankingRequest) (*model.GetScoresRankingResponse, error) {
var scores []*ent.Score

// entのクエリを使用してスコアを取得
Expand All @@ -24,21 +24,24 @@ func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, st
score.AccuracyGTE(0.95),
),
).
Order(ent.Desc(sortBy))
Order(ent.Desc(request.SortBy))

switch sortBy {
switch request.SortBy {
case "accuracy":
query = query.Where(score.IsMaxAccuracy(true))
case "keystrokes":
query = query.Where(score.IsMaxKeystrokes(true))
default:
return nil, fmt.Errorf("invalid sort by parameter: %s", sortBy)
return nil, fmt.Errorf("invalid sort by parameter: %s", request.SortBy)
}

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

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

if err != nil {
Expand Down Expand Up @@ -68,15 +71,15 @@ func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, st
}

var currentScore float64
switch sortBy {
switch request.SortBy {
case "accuracy":
currentScore = s.Accuracy
case "keystrokes":
currentScore = float64(s.Keystrokes)
}

if i == 0 || currentScore != prevScore {
rank = start + i
rank = request.Start + i
}

prevScore = currentScore
Expand All @@ -89,7 +92,10 @@ func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, st
rankings = append(rankings, ranking)
}

return rankings, nil
return &model.GetScoresRankingResponse{
Rankings: rankings,
TotalCount: count,
}, nil
}
func CreateScore(ctx context.Context, client *ent.Client, userID uuid.UUID, keystrokes int, accuracy float64) error {
// トランザクションを開始
Expand Down
7 changes: 3 additions & 4 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, request *model.GetScoresRankingRequest) (*model.GetScoresRankingResponse, error) {
response, err := repository.GetScoresRanking(ctx, client, request)
if err != nil {
return nil, err
}

return rankings, nil
return response, nil
}

func CreateScore(ctx context.Context, client *ent.Client, userID uuid.UUID, keystrokes int, accuracy float64) error {
Expand Down
11 changes: 11 additions & 0 deletions typing-server/domain/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ type Score struct {
CreatedAt time.Time `json:"created_at"`
User User `json:"user"`
}

type GetScoresRankingRequest struct {
SortBy string `json:"sort_by"`
Start int `json:"start"`
Limit int `json:"limit"`
}

type GetScoresRankingResponse struct {
Rankings []*ScoreRanking `json:"rankings"`
TotalCount int `json:"total_count"`
}

0 comments on commit 80adaf1

Please sign in to comment.