Skip to content

Commit

Permalink
fix: リクエストボディから情報を取るように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
KinjiKawaguchi committed Apr 1, 2024
1 parent 886513d commit a0a7172
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions typing-server/api/handler/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,26 @@ func GetScoresRanking(w http.ResponseWriter, r *http.Request) {
func PostScore(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

userIDStr := r.URL.Query().Get("user_id")
userID, err := uuid.Parse(userIDStr)
if err != nil {
http.Error(w, "Invalid user_id", http.StatusBadRequest)
return
// リクエストボディから値を取得
var requestBody struct {
UserID string `json:"user_id"`
Keystrokes int `json:"keystrokes"`
Accuracy float64 `json:"accuracy"`
}

keystrokesStr := r.URL.Query().Get("keystrokes")
keystrokes, err := strconv.Atoi(keystrokesStr)
if err != nil {
http.Error(w, "Invalid keystrokes", http.StatusBadRequest)
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}

accuracyStr := r.URL.Query().Get("accuracy")
accuracy, err := strconv.ParseFloat(accuracyStr, 64)
// user_idをUUIDに変換
userID, err := uuid.Parse(requestBody.UserID)
if err != nil {
http.Error(w, "Invalid accuracy", http.StatusBadRequest)
http.Error(w, "Invalid user_id", http.StatusBadRequest)
return
}

if err := service.CreateScore(ctx, entClient, userID, keystrokes, accuracy); err != nil {
// スコアを作成
if err := service.CreateScore(ctx, entClient, userID, requestBody.Keystrokes, requestBody.Accuracy); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit a0a7172

Please sign in to comment.