feat(services): add PUT /guilds/[guildId]/icon

This commit is contained in:
Divlo
2021-10-26 14:01:49 +00:00
parent 14eac3cadb
commit 56c613b5cf
21 changed files with 233 additions and 79 deletions

View File

@ -0,0 +1,80 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { fastifyErrors } from '../../../../models/utils.js'
import fastifyMultipart from 'fastify-multipart'
import prisma from '../../../../tools/database/prisma.js'
import { uploadImage } from '../../../../tools/utils/uploadImage.js'
import { guildSchema } from '../../../../models/Guild.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id
})
type Parameters = Static<typeof parametersSchema>
const putServiceSchema: FastifySchema = {
description: 'Edit the icon of the guild with its id',
tags: ['guilds'] as string[],
consumes: ['multipart/form-data'] as string[],
produces: ['application/json'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
params: parametersSchema,
response: {
200: Type.Object({
guild: Type.Object({
icon: Type.String()
})
}),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
431: fastifyErrors[431],
500: fastifyErrors[500]
}
} as const
export const putGuildIconById: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
await fastify.register(fastifyMultipart)
fastify.route<{
Params: Parameters
}>({
method: 'PUT',
url: '/guilds/:guildId/icon',
schema: putServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { guildId } = request.params
const guild = await prisma.guild.findUnique({ where: { id: guildId } })
if (guild == null) {
throw fastify.httpErrors.notFound()
}
const icon = await uploadImage({
fastify,
request,
folderInUploadsFolder: 'guilds'
})
await prisma.guild.update({
where: { id: guildId },
data: { icon }
})
reply.statusCode = 200
return {
guild: {
icon
}
}
}
})
}

View File

@ -1,7 +1,9 @@
import { FastifyPluginAsync } from 'fastify'
import { postGuilds } from './post.js'
import { putGuildIconById } from './[guildId]/icon/put.js'
export const guildsService: FastifyPluginAsync = async (fastify) => {
await fastify.register(postGuilds)
await fastify.register(putGuildIconById)
}

View File

@ -7,7 +7,8 @@ import authenticateUser from '../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../models/Guild.js'
import { channelSchema } from '../../models/Channel.js'
import { memberSchema } from '../../models/Member.js'
import { userPublicSchema } from '../../models/User.js'
import { userPublicWithoutSettingsSchema } from '../../models/User.js'
import { parseStringNullish } from '../../tools/utils/parseStringNullish.js'
const bodyPostServiceSchema = Type.Object({
name: guildSchema.name,
@ -33,7 +34,7 @@ const postServiceSchema: FastifySchema = {
members: Type.Array(
Type.Object({
...memberSchema,
user: Type.Object(userPublicSchema)
user: Type.Object(userPublicWithoutSettingsSchema)
})
)
})
@ -59,7 +60,9 @@ export const postGuilds: FastifyPluginAsync = async (fastify) => {
throw fastify.httpErrors.forbidden()
}
const { name, description } = request.body
const guild = await prisma.guild.create({ data: { name, description } })
const guild = await prisma.guild.create({
data: { name, description: parseStringNullish(description) }
})
const channel = await prisma.channel.create({
data: { name: 'general', guildId: guild.id }
})