feat(services): add GET /guilds/public
This commit is contained in:
parent
56c613b5cf
commit
f0f4f9f69f
1
.gitignore
vendored
1
.gitignore
vendored
@ -33,3 +33,4 @@ npm-debug.log*
|
|||||||
|
|
||||||
# misc
|
# misc
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
uploads
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import { FastifyPluginAsync } from 'fastify'
|
import { FastifyPluginAsync } from 'fastify'
|
||||||
|
|
||||||
import { postGuilds } from './post.js'
|
import { postGuilds } from './post.js'
|
||||||
|
import { getGuildsPublic } from './public/get.js'
|
||||||
import { putGuildIconById } from './[guildId]/icon/put.js'
|
import { putGuildIconById } from './[guildId]/icon/put.js'
|
||||||
|
|
||||||
export const guildsService: FastifyPluginAsync = async (fastify) => {
|
export const guildsService: FastifyPluginAsync = async (fastify) => {
|
||||||
await fastify.register(postGuilds)
|
await fastify.register(postGuilds)
|
||||||
await fastify.register(putGuildIconById)
|
await fastify.register(putGuildIconById)
|
||||||
|
await fastify.register(getGuildsPublic)
|
||||||
}
|
}
|
||||||
|
24
src/services/guilds/public/__test__/get.test.ts
Normal file
24
src/services/guilds/public/__test__/get.test.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { application } from '../../../../application.js'
|
||||||
|
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||||
|
import { prismaMock } from '../../../../__test__/setup.js'
|
||||||
|
import { guildExample } from '../../../../models/Guild.js'
|
||||||
|
|
||||||
|
describe('GET /guilds/public', () => {
|
||||||
|
it('succeeds', async () => {
|
||||||
|
prismaMock.guild.findMany.mockResolvedValue([guildExample])
|
||||||
|
prismaMock.member.count.mockResolvedValue(2)
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
const response = await application.inject({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/guilds/public',
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${accessToken}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const responseJson = response.json()
|
||||||
|
expect(response.statusCode).toEqual(200)
|
||||||
|
expect(responseJson.length).toEqual(1)
|
||||||
|
expect(responseJson[0].name).toEqual(guildExample.name)
|
||||||
|
expect(responseJson[0].membersCount).toEqual(2)
|
||||||
|
})
|
||||||
|
})
|
69
src/services/guilds/public/get.ts
Normal file
69
src/services/guilds/public/get.ts
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
import { Type } from '@sinclair/typebox'
|
||||||
|
|
||||||
|
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||||
|
|
||||||
|
import prisma from '../../../tools/database/prisma.js'
|
||||||
|
import { fastifyErrors } from '../../../models/utils.js'
|
||||||
|
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
|
||||||
|
import { guildSchema } from '../../../models/Guild'
|
||||||
|
import {
|
||||||
|
getPaginationOptions,
|
||||||
|
queryPaginationSchema,
|
||||||
|
QueryPaginationSchemaType
|
||||||
|
} from '../../../tools/database/pagination'
|
||||||
|
|
||||||
|
const getServiceSchema: FastifySchema = {
|
||||||
|
description: 'GET all the public guilds.',
|
||||||
|
tags: ['guilds'] as string[],
|
||||||
|
security: [
|
||||||
|
{
|
||||||
|
bearerAuth: []
|
||||||
|
}
|
||||||
|
] as Array<{ [key: string]: [] }>,
|
||||||
|
querystring: queryPaginationSchema,
|
||||||
|
response: {
|
||||||
|
200: Type.Array(
|
||||||
|
Type.Object({
|
||||||
|
...guildSchema,
|
||||||
|
membersCount: Type.Integer()
|
||||||
|
})
|
||||||
|
),
|
||||||
|
400: fastifyErrors[400],
|
||||||
|
401: fastifyErrors[401],
|
||||||
|
403: fastifyErrors[403],
|
||||||
|
500: fastifyErrors[500]
|
||||||
|
}
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const getGuildsPublic: FastifyPluginAsync = async (fastify) => {
|
||||||
|
await fastify.register(authenticateUser)
|
||||||
|
|
||||||
|
fastify.route<{
|
||||||
|
Querystring: QueryPaginationSchemaType
|
||||||
|
}>({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/guilds/public',
|
||||||
|
schema: getServiceSchema,
|
||||||
|
handler: async (request, reply) => {
|
||||||
|
if (request.user == null) {
|
||||||
|
throw fastify.httpErrors.forbidden()
|
||||||
|
}
|
||||||
|
const guildsRequest = await prisma.guild.findMany({
|
||||||
|
...getPaginationOptions(request.query)
|
||||||
|
})
|
||||||
|
const guilds = await Promise.all(
|
||||||
|
guildsRequest.map(async (guild) => {
|
||||||
|
const membersCount = await prisma.member.count({
|
||||||
|
where: { guildId: guild.id }
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
...guild,
|
||||||
|
membersCount
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
reply.statusCode = 200
|
||||||
|
return guilds
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Reference in New Issue
Block a user