Skip to content

Commit

Permalink
feat: openAPIの仕様に合わせてAPIを調整
Browse files Browse the repository at this point in the history
  • Loading branch information
KinjiKawaguchi committed Mar 24, 2024
1 parent 82f7a32 commit e88b7ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
33 changes: 21 additions & 12 deletions typing-server/api/handler/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
44 changes: 30 additions & 14 deletions typing-server/api/repository/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,57 @@ 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

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
Expand Down
10 changes: 5 additions & 5 deletions typing-server/api/service/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ 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
}

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
}

Expand Down

0 comments on commit e88b7ec

Please sign in to comment.