mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-01 04:34:50 -04:00
Use pages for users paginations
This commit is contained in:
parent
076d5a0dbd
commit
411f6dcfba
@ -8,7 +8,7 @@ export const desc = {
|
|||||||
`,
|
`,
|
||||||
|
|
||||||
after: comment`
|
after: comment`
|
||||||
Id of the cursor in the pagination.
|
Cursor for the pagination.
|
||||||
You can ignore this and only use the prev/next field in the response.
|
You can ignore this and only use the prev/next field in the response.
|
||||||
`,
|
`,
|
||||||
|
|
||||||
|
@ -245,9 +245,8 @@ const docTemplate = `{
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"format": "uuid",
|
|
||||||
"description": "used for pagination.",
|
"description": "used for pagination.",
|
||||||
"name": "afterId",
|
"name": "after",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -255,7 +254,7 @@ const docTemplate = `{
|
|||||||
"200": {
|
"200": {
|
||||||
"description": "OK",
|
"description": "OK",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/main.User"
|
"$ref": "#/definitions/main.Page-main_User"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"422": {
|
"422": {
|
||||||
@ -576,6 +575,25 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"main.Page-main_User": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"items": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/main.User"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"next": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "https://kyoo.zoriya.dev/auth/users?after=aoeusth"
|
||||||
|
},
|
||||||
|
"this": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "https://kyoo.zoriya.dev/auth/users"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"main.RegisterDto": {
|
"main.RegisterDto": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
|
@ -239,9 +239,8 @@
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"format": "uuid",
|
|
||||||
"description": "used for pagination.",
|
"description": "used for pagination.",
|
||||||
"name": "afterId",
|
"name": "after",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -249,7 +248,7 @@
|
|||||||
"200": {
|
"200": {
|
||||||
"description": "OK",
|
"description": "OK",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/main.User"
|
"$ref": "#/definitions/main.Page-main_User"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"422": {
|
"422": {
|
||||||
@ -570,6 +569,25 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"main.Page-main_User": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"items": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/main.User"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"next": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "https://kyoo.zoriya.dev/auth/users?after=aoeusth"
|
||||||
|
},
|
||||||
|
"this": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "https://kyoo.zoriya.dev/auth/users"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"main.RegisterDto": {
|
"main.RegisterDto": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
|
28
auth/page.go
Normal file
28
auth/page.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "net/url"
|
||||||
|
|
||||||
|
type Page[T any] struct {
|
||||||
|
Items []T `json:"items"`
|
||||||
|
This string `json:"this" example:"https://kyoo.zoriya.dev/auth/users"`
|
||||||
|
Next *string `json:"next" example:"https://kyoo.zoriya.dev/auth/users?after=aoeusth"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPage(items []User, url *url.URL, limit int32) Page[User] {
|
||||||
|
this := url.String()
|
||||||
|
|
||||||
|
var next *string
|
||||||
|
if len(items) == int(limit) && limit > 0 {
|
||||||
|
query := url.Query()
|
||||||
|
query.Set("after", items[len(items)-1].Id.String())
|
||||||
|
url.RawQuery = query.Encode()
|
||||||
|
nextU := url.String()
|
||||||
|
next = &nextU
|
||||||
|
}
|
||||||
|
|
||||||
|
return Page[User]{
|
||||||
|
Items: items,
|
||||||
|
This: this,
|
||||||
|
Next: next,
|
||||||
|
}
|
||||||
|
}
|
@ -78,8 +78,8 @@ func MapOidc(oidc *dbc.GetUserRow) OidcHandle {
|
|||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security Jwt[users.read]
|
// @Security Jwt[users.read]
|
||||||
// @Param afterId query string false "used for pagination." Format(uuid)
|
// @Param after query string false "used for pagination."
|
||||||
// @Success 200 {object} User[]
|
// @Success 200 {object} Page[User]
|
||||||
// @Failure 422 {object} KError "Invalid after id"
|
// @Failure 422 {object} KError "Invalid after id"
|
||||||
// @Router /users [get]
|
// @Router /users [get]
|
||||||
func (h *Handler) ListUsers(c echo.Context) error {
|
func (h *Handler) ListUsers(c echo.Context) error {
|
||||||
@ -90,7 +90,7 @@ func (h *Handler) ListUsers(c echo.Context) error {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
limit := int32(20)
|
limit := int32(20)
|
||||||
id := c.Param("afterId")
|
id := c.Param("after")
|
||||||
|
|
||||||
var users []dbc.User
|
var users []dbc.User
|
||||||
if id == "" {
|
if id == "" {
|
||||||
@ -98,7 +98,7 @@ func (h *Handler) ListUsers(c echo.Context) error {
|
|||||||
} else {
|
} else {
|
||||||
uid, uerr := uuid.Parse(id)
|
uid, uerr := uuid.Parse(id)
|
||||||
if uerr != nil {
|
if uerr != nil {
|
||||||
return echo.NewHTTPError(http.StatusUnprocessableEntity, "Invalid `afterId` parameter, uuid was expected")
|
return echo.NewHTTPError(http.StatusUnprocessableEntity, "Invalid `after` parameter, uuid was expected")
|
||||||
}
|
}
|
||||||
users, err = h.db.GetAllUsersAfter(ctx, dbc.GetAllUsersAfterParams{
|
users, err = h.db.GetAllUsersAfter(ctx, dbc.GetAllUsersAfterParams{
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
@ -114,8 +114,7 @@ func (h *Handler) ListUsers(c echo.Context) error {
|
|||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
ret = append(ret, MapDbUser(&user))
|
ret = append(ret, MapDbUser(&user))
|
||||||
}
|
}
|
||||||
// TODO: switch to a Page
|
return c.JSON(200, NewPage(ret, c.Request().URL, limit))
|
||||||
return c.JSON(200, ret)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Get user
|
// @Summary Get user
|
||||||
|
Loading…
x
Reference in New Issue
Block a user