Commit 14a2d51f authored by Julien Schröter's avatar Julien Schröter

Limit finished duels when querying all of an users duels

parent 11129889
Pipeline #2280 passed with stage
in 1 minute and 43 seconds
......@@ -497,6 +497,13 @@ paths:
schema:
type: integer
format: uint32
- name: c
description: Only works if querying all of an user's duels. Specifies the maximum count of finished duels to fetch. Defaults to 0, maximum is 30.
in: query
required: false
schema:
type: integer
format: uint32
responses:
'200':
description: duel array of all the current user\'s running duels. If an id is specified, return that duel\'s full information.
......@@ -506,6 +513,8 @@ paths:
type: array
items:
$ref: '#/components/schemas/duel'
'400':
description: Mistake in request. May have tried to query more than the limit of non-running duels.
'401':
description: Authentication failure.
content:
......
......@@ -52,19 +52,20 @@ func SetupDuelRoutes(group *gin.RouterGroup, repository DuelQuery, titleReposito
func getDuel(repository DuelQuery) gin.HandlerFunc {
return func(ctx *gin.Context) {
var idStruct struct {
ID uint32 `form:"id" binding:"required"`
}
var requestParams getDuelsParameters
userID, err := endpoint.GetUserID(ctx)
if err != nil {
return
}
e := ctx.ShouldBindQuery(&idStruct)
if e == nil {
if err = ctx.BindQuery(&requestParams); err != nil {
return
}
if requestParams.ID > 0 {
// duel specified, return if from user
result, errDB := repository.GetDuel(idStruct.ID, userID)
result, errDB := repository.GetDuel(requestParams.ID, userID)
if errDB != nil {
if errDB == ErrNotPermitted {
ctx.Status(http.StatusForbidden)
......@@ -82,7 +83,7 @@ func getDuel(repository DuelQuery) gin.HandlerFunc {
} else {
// No duel specified
// return all current duels of that user
result, errDB := repository.GetAllDuels(userID, 0)
result, errDB := repository.GetAllDuels(userID, requestParams.Count)
if errDB != nil {
raven.CaptureError(errDB, nil)
ctx.Status(http.StatusInternalServerError)
......
......@@ -6,6 +6,11 @@ import (
"gitlab.akamu.de/akamu/game-server-go/schemas"
)
type getDuelsParameters struct {
ID uint32 `form:"id"`
Count uint32 `form:"c" binding:"min=0,max=10"`
}
type postDuelAnswerParameters struct {
RoundID uint32 `form:"round" binding:"required"`
QuestionID uint32 `form:"question" binding:"required"`
......
......@@ -315,7 +315,7 @@ func Test_getAllDuels200(t *testing.T) {
env1.Login("user1", "password1", t)
res1 := httptest.NewRecorder()
req1, _ := env1.NewRequest("GET", "/duel", nil)
req1, _ := env1.NewRequest("GET", "/duel?c=10", nil)
router.ServeHTTP(res1, req1)
......
......@@ -106,7 +106,7 @@ func testGetUser1_200(router *gin.Engine) func(t *testing.T) {
env.Login("user1", "Password1", t)
res := httptest.NewRecorder()
req, _ := env.NewRequest("GET", "/duel", nil)
req, _ := env.NewParameterRequest("GET", "/duel", map[string]string{"c": "10"}, nil)
router.ServeHTTP(res, req)
......@@ -128,7 +128,7 @@ func testGetUser2_200(router *gin.Engine) func(t *testing.T) {
env.Login("user2", "Password2", t)
res := httptest.NewRecorder()
req, _ := env.NewRequest("GET", "/duel", nil)
req, _ := env.NewParameterRequest("GET", "/duel", map[string]string{"c": "10"}, nil)
router.ServeHTTP(res, req)
......@@ -150,7 +150,7 @@ func testGetUser3_200(router *gin.Engine) func(t *testing.T) {
env.Login("user3", "Password3", t)
responseFile := "duel/getUser3_200.json.template"
req, _ := env.NewRequest("GET", "/duel", nil)
req, _ := env.NewParameterRequest("GET", "/duel", map[string]string{"c": "10"}, nil)
executeAndAssertJSONOk(t, router, req, responseFile)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment