Skip to content

Commit

Permalink
WIP: api作った
Browse files Browse the repository at this point in the history
  • Loading branch information
KinjiKawaguchi committed Mar 23, 2024
1 parent 385c3f8 commit 015909b
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 11 deletions.
57 changes: 57 additions & 0 deletions typing-server/api/handler/score.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package handler

import (
"encoding/json"
"net/http"
"strconv"

"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) {
ctx := r.Context()

sortBy := r.URL.Query().Get("sort_by")
if sortBy == "" {
sortBy = "keystrokes"
}

startStr := r.URL.Query().Get("start")
start, err := strconv.Atoi(startStr)
if err != nil {
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)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(rankings)
}

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)
return
}

if err := service.CreateScore(ctx, &score); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusCreated)
}
27 changes: 27 additions & 0 deletions typing-server/api/handler/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package handler

import (
"encoding/json"
"net/http"

"github.com/su-its/typing/typing-server/api/service"
)

func GetUsers(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

studentNumber := r.URL.Query().Get("student_number")
if studentNumber == "" {
http.Error(w, "student_number is required", http.StatusBadRequest)
return
}

user, err := service.GetUserByStudentNumber(ctx, studentNumber)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(user)
}
11 changes: 0 additions & 11 deletions typing-server/api/presenter/server.go

This file was deleted.

44 changes: 44 additions & 0 deletions typing-server/api/repository/score.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package repository

import (
"context"

"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) {
client := ent.FromContext(ctx)

var rankings []*model.ScoreRanking

err := client.Score.Query().
WithUser().
Order(ent.Desc(sortBy)).
Limit(limit).
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,
})
})
return rankings, err
}

func CreateScore(ctx context.Context, score *ent.Score) error {
client := ent.FromContext(ctx)

_, err := client.Score.Create().
SetKeystrokes(score.Keystrokes).
SetAccuracy(score.Accuracy).
SetUser(score.Edges.User).
Save(ctx)

return err
}
25 changes: 25 additions & 0 deletions typing-server/api/repository/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package repository

import (
"context"

"github.com/su-its/typing/typing-server/domain/repository/ent"
"github.com/su-its/typing/typing-server/domain/repository/ent/user"
)

func GetUserByStudentNumber(ctx context.Context, studentNumber string) (*ent.User, error) {
client := ent.FromContext(ctx)

entUser, err := client.User.Query().
Where(user.StudentNumber(studentNumber)).
Only(ctx)
if err != nil {
return nil, err
}

return &ent.User{
ID: entUser.ID,
StudentNumber: entUser.StudentNumber,
HandleName: entUser.HandleName,
}, nil
}
22 changes: 22 additions & 0 deletions typing-server/api/router/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package router

import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/su-its/typing/typing-server/api/controller/system"
"github.com/su-its/typing/typing-server/api/handler"
)

func SetupRouter() http.Handler {
r := chi.NewRouter()

r.Get("/health", system.HealthCheck)

r.Get("/users", handler.GetUsers)

r.Get("/scores/ranking", handler.GetScoresRanking)
r.Post("/scores", handler.PostScore)

return r
}
26 changes: 26 additions & 0 deletions typing-server/api/service/score.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package service

import (
"context"

"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)
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 {
return err
}

return nil
}
17 changes: 17 additions & 0 deletions typing-server/api/service/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package service

import (
"context"

"github.com/su-its/typing/typing-server/api/repository"
"github.com/su-its/typing/typing-server/domain/repository/ent"
)

func GetUserByStudentNumber(ctx context.Context, studentNumber string) (*ent.User, error) {
user, err := repository.GetUserByStudentNumber(ctx, studentNumber)
if err != nil {
return nil, err
}

return user, nil
}
12 changes: 12 additions & 0 deletions typing-server/domain/model/score.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package model

import "time"

type ScoreRanking struct {
Rank int `json:"rank"`
UserID string `json:"user_id"`
Username string `json:"username"`
Score int `json:"score"`
Accuracy float64 `json:"accuracy"`
CreatedAt time.Time `json:"created_at"`
}

0 comments on commit 015909b

Please sign in to comment.