Skip to content

Commit

Permalink
Merge branch 'feat/backend/api/v1' of https://github.com/su-its/typing
Browse files Browse the repository at this point in the history
…into feat/backend/api/v1
  • Loading branch information
shin0729 committed Apr 6, 2024
2 parents bcc48d5 + 5913be3 commit 3d88ba3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
15 changes: 9 additions & 6 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,18 +30,20 @@ func GetScoresRanking(w http.ResponseWriter, r *http.Request) {
limit = 10
}

rankings, totalCount, 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(map[string]interface{}{
"rankings": rankings,
"total_count": totalCount,
})
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
23 changes: 13 additions & 10 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, int, error) {
func GetScoresRanking(ctx context.Context, client *ent.Client, request *model.GetScoresRankingRequest) (*model.GetScoresRankingResponse, error) {
var scores []*ent.Score

// entのクエリを使用してスコアを取得
Expand All @@ -24,28 +24,28 @@ 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, 0, 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 {
return nil, 0, err
return nil, err
}

var rankings []*model.ScoreRanking
Expand All @@ -71,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 @@ -92,7 +92,10 @@ func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, st
rankings = append(rankings, ranking)
}

return rankings, count, 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
8 changes: 4 additions & 4 deletions typing-server/api/service/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +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, int, error) {
rankings, totalCount, 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, 0, err
return nil, err
}
return rankings, totalCount, 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 3d88ba3

Please sign in to comment.