feat(services): ability to search GET /guilds/public
This commit is contained in:
@ -6,8 +6,14 @@ import { date, id } from './utils.js'
|
||||
export const guildSchema = {
|
||||
id,
|
||||
name: Type.String({ minLength: 1, maxLength: 30 }),
|
||||
icon: Type.Union([Type.String({ format: 'uri-reference' }), Type.Null()]),
|
||||
description: Type.Union([Type.String({ maxLength: 160 }), Type.Null()]),
|
||||
icon: Type.Union([
|
||||
Type.String({ format: 'uri-reference', minLength: 1 }),
|
||||
Type.Null()
|
||||
]),
|
||||
description: Type.Union([
|
||||
Type.String({ minLength: 1, maxLength: 160 }),
|
||||
Type.Null()
|
||||
]),
|
||||
createdAt: date.createdAt,
|
||||
updatedAt: date.updatedAt
|
||||
}
|
||||
|
@ -21,10 +21,14 @@ export const userSchema = {
|
||||
name: Type.String({ minLength: 1, maxLength: 30 }),
|
||||
email: Type.String({ minLength: 1, maxLength: 254, format: 'email' }),
|
||||
password: Type.String(),
|
||||
logo: Type.String({ format: 'uri-reference' }),
|
||||
status: Type.String({ maxLength: 50 }),
|
||||
biography: Type.String({ maxLength: 160 }),
|
||||
website: Type.String({ maxLength: 255, format: 'uri-reference' }),
|
||||
logo: Type.String({ minLength: 1, format: 'uri-reference' }),
|
||||
status: Type.String({ minLength: 1, maxLength: 50 }),
|
||||
biography: Type.String({ minLength: 1, maxLength: 160 }),
|
||||
website: Type.String({
|
||||
minLength: 1,
|
||||
maxLength: 255,
|
||||
format: 'uri-reference'
|
||||
}),
|
||||
isConfirmed: Type.Boolean({ default: false }),
|
||||
temporaryToken: Type.String(),
|
||||
temporaryExpirationToken: Type.String({ format: 'date-time' }),
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
@ -8,10 +8,16 @@ import authenticateUser from '../../../tools/plugins/authenticateUser.js'
|
||||
import { guildSchema } from '../../../models/Guild'
|
||||
import {
|
||||
getPaginationOptions,
|
||||
queryPaginationSchema,
|
||||
QueryPaginationSchemaType
|
||||
queryPaginationSchema
|
||||
} from '../../../tools/database/pagination'
|
||||
|
||||
const querySchema = Type.Object({
|
||||
search: Type.Optional(Type.String()),
|
||||
...queryPaginationSchema
|
||||
})
|
||||
|
||||
export type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'GET all the public guilds.',
|
||||
tags: ['guilds'] as string[],
|
||||
@ -20,7 +26,7 @@ const getServiceSchema: FastifySchema = {
|
||||
bearerAuth: []
|
||||
}
|
||||
] as Array<{ [key: string]: [] }>,
|
||||
querystring: queryPaginationSchema,
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.Array(
|
||||
Type.Object({
|
||||
@ -39,7 +45,7 @@ export const getGuildsPublic: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(authenticateUser)
|
||||
|
||||
fastify.route<{
|
||||
Querystring: QueryPaginationSchemaType
|
||||
Querystring: QuerySchemaType
|
||||
}>({
|
||||
method: 'GET',
|
||||
url: '/guilds/public',
|
||||
@ -49,7 +55,13 @@ export const getGuildsPublic: FastifyPluginAsync = async (fastify) => {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
const guildsRequest = await prisma.guild.findMany({
|
||||
...getPaginationOptions(request.query)
|
||||
...getPaginationOptions(request.query),
|
||||
orderBy: { createdAt: 'desc' },
|
||||
...(request.query.search != null && {
|
||||
where: {
|
||||
name: { contains: request.query.search }
|
||||
}
|
||||
})
|
||||
})
|
||||
const guilds = await Promise.all(
|
||||
guildsRequest.map(async (guild) => {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
|
||||
export const queryPaginationSchema = Type.Object({
|
||||
export const queryPaginationSchema = {
|
||||
/** Maximum number of items to return */
|
||||
limit: Type.Integer({ default: 20, minimum: 1, maximum: 100 }),
|
||||
|
||||
@ -12,9 +12,13 @@ export const queryPaginationSchema = Type.Object({
|
||||
after: Type.Optional(
|
||||
Type.Integer({ minimum: 1, description: 'Get items after this id' })
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
export type QueryPaginationSchemaType = Static<typeof queryPaginationSchema>
|
||||
export const queryPaginationObjectSchema = Type.Object(queryPaginationSchema)
|
||||
|
||||
export type QueryPaginationSchemaType = Static<
|
||||
typeof queryPaginationObjectSchema
|
||||
>
|
||||
|
||||
export const getPaginationOptions = (
|
||||
query: QueryPaginationSchemaType
|
||||
|
Reference in New Issue
Block a user