From e88b7ecfd4d156f808ad49fe4dc8ca51fb8821d0 Mon Sep 17 00:00:00 2001 From: KinjiKawaguchi Date: Mon, 25 Mar 2024 01:00:05 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20openAPI=E3=81=AE=E4=BB=95=E6=A7=98?= =?UTF-8?q?=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B=E3=81=A6API=E3=82=92?= =?UTF-8?q?=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/handler/score.go | 33 ++++++++++++-------- typing-server/api/repository/score.go | 44 ++++++++++++++++++--------- typing-server/api/service/score.go | 10 +++--- 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/typing-server/api/handler/score.go b/typing-server/api/handler/score.go index 37da3fc..005fb32 100644 --- a/typing-server/api/handler/score.go +++ b/typing-server/api/handler/score.go @@ -5,8 +5,8 @@ import ( "net/http" "strconv" + "github.com/google/uuid" "github.com/su-its/typing/typing-server/api/service" - "github.com/su-its/typing/typing-server/domain/repository/ent" ) func GetScoresRanking(w http.ResponseWriter, r *http.Request) { @@ -23,13 +23,7 @@ func GetScoresRanking(w http.ResponseWriter, r *http.Request) { start = 1 } - limitStr := r.URL.Query().Get("limit") - limit, err := strconv.Atoi(limitStr) - if err != nil { - limit = 50 - } - - rankings, err := service.GetScoresRanking(ctx, sortBy, start, limit) + rankings, err := service.GetScoresRanking(ctx, sortBy, start) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -42,13 +36,28 @@ func GetScoresRanking(w http.ResponseWriter, r *http.Request) { func PostScore(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - var score ent.Score - if err := json.NewDecoder(r.Body).Decode(&score); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + userIDStr := r.URL.Query().Get("user_id") + userID, err := uuid.Parse(userIDStr) + if err != nil { + http.Error(w, "Invalid user_id", http.StatusBadRequest) + return + } + + keystrokesStr := r.URL.Query().Get("keystrokes") + keystrokes, err := strconv.Atoi(keystrokesStr) + if err != nil { + http.Error(w, "Invalid keystrokes", http.StatusBadRequest) + return + } + + accuracyStr := r.URL.Query().Get("accuracy") + accuracy, err := strconv.ParseFloat(accuracyStr, 64) + if err != nil { + http.Error(w, "Invalid accuracy", http.StatusBadRequest) return } - if err := service.CreateScore(ctx, &score); err != nil { + if err := service.CreateScore(ctx, userID, keystrokes, accuracy); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } diff --git a/typing-server/api/repository/score.go b/typing-server/api/repository/score.go index 2a1e1de..db3b0d3 100644 --- a/typing-server/api/repository/score.go +++ b/typing-server/api/repository/score.go @@ -3,11 +3,12 @@ package repository import ( "context" + "github.com/google/uuid" "github.com/su-its/typing/typing-server/domain/model" "github.com/su-its/typing/typing-server/domain/repository/ent" ) -func GetScoresRanking(ctx context.Context, sortBy string, start, limit int) ([]*model.ScoreRanking, error) { +func GetScoresRanking(ctx context.Context, sortBy string, start int) ([]*model.ScoreRanking, error) { client := ent.FromContext(ctx) var rankings []*model.ScoreRanking @@ -15,29 +16,44 @@ func GetScoresRanking(ctx context.Context, sortBy string, start, limit int) ([]* err := client.Score.Query(). WithUser(). Order(ent.Desc(sortBy)). - Limit(limit). + Limit(50). Offset(start-1). Select("id, keystrokes, accuracy, created_at"). Scan(ctx, func(s *ent.Score) { - rankings = append(rankings, &model.ScoreRanking{ - Rank: start + len(rankings), - UserID: s.ID.String(), - Username: s.Edges.User.HandleName, - Score: s.Keystrokes, - Accuracy: s.Accuracy, - CreatedAt: s.CreatedAt, - }) + user := &model.User{ + ID: s.Edges.User.ID.String(), + StudentNumber: s.Edges.User.StudentNumber, + HandleName: s.Edges.User.HandleName, + CreatedAt: s.Edges.User.CreatedAt, + UpdatedAt: s.Edges.User.UpdatedAt, + } + + score := &model.Score{ + ID: s.ID.String(), + Keystrokes: s.Keystrokes, + Accuracy: s.Accuracy, + CreatedAt: s.CreatedAt, + User: *user, + } + + ranking := &model.ScoreRanking{ + Rank: start + len(rankings), + Score: *score, + } + + rankings = append(rankings, ranking) }) + return rankings, err } -func CreateScore(ctx context.Context, score *ent.Score) error { +func CreateScore(ctx context.Context, userID uuid.UUID, keystrokes int, accuracy float64) error { client := ent.FromContext(ctx) _, err := client.Score.Create(). - SetKeystrokes(score.Keystrokes). - SetAccuracy(score.Accuracy). - SetUser(score.Edges.User). + SetKeystrokes(keystrokes). + SetAccuracy(float64(keystrokes)). + SetUserID(userID). Save(ctx) return err diff --git a/typing-server/api/service/score.go b/typing-server/api/service/score.go index 3267cf1..605d81b 100644 --- a/typing-server/api/service/score.go +++ b/typing-server/api/service/score.go @@ -3,13 +3,13 @@ package service import ( "context" + "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" ) -func GetScoresRanking(ctx context.Context, sortBy string, start, limit int) ([]*model.ScoreRanking, error) { - rankings, err := repository.GetScoresRanking(ctx, sortBy, start, limit) +func GetScoresRanking(ctx context.Context, sortBy string, start int) ([]*model.ScoreRanking, error) { + rankings, err := repository.GetScoresRanking(ctx, sortBy, start) if err != nil { return nil, err } @@ -17,8 +17,8 @@ func GetScoresRanking(ctx context.Context, sortBy string, start, limit int) ([]* return rankings, nil } -func CreateScore(ctx context.Context, score *ent.Score) error { - if err := repository.CreateScore(ctx, score); err != nil { +func CreateScore(ctx context.Context, userID uuid.UUID, keystrokes int, accuracy float64) error { + if err := repository.CreateScore(ctx, userID, keystrokes, accuracy); err != nil { return err }