-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
385c3f8
commit 015909b
Showing
9 changed files
with
230 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |