feat: add profile routes
Made-with: Cursor
This commit is contained in:
73
src/routes/profile.ts
Normal file
73
src/routes/profile.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import { UserService } from '../services/user/user.service.js';
|
||||
|
||||
const patchProfileSchema = {
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
nickname: { type: 'string', minLength: 2, maxLength: 30 },
|
||||
avatarUrl: { type: ['string', 'null'], maxLength: 500 },
|
||||
country: { type: ['string', 'null'], maxLength: 100 },
|
||||
city: { type: ['string', 'null'], maxLength: 100 },
|
||||
selfLevel: { type: ['string', 'null'], enum: ['jun', 'mid', 'sen'] },
|
||||
isPublic: { type: 'boolean' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
const usernameParamsSchema = {
|
||||
params: {
|
||||
type: 'object',
|
||||
required: ['username'],
|
||||
properties: {
|
||||
username: { type: 'string', minLength: 2, maxLength: 30 },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export async function profileRoutes(app: FastifyInstance) {
|
||||
const userService = new UserService(app.db);
|
||||
const { rateLimitOptions } = app;
|
||||
|
||||
app.get(
|
||||
'/',
|
||||
{
|
||||
config: { rateLimit: rateLimitOptions.apiAuthed },
|
||||
preHandler: [app.authenticate, app.withSubscription],
|
||||
},
|
||||
async (req, reply) => {
|
||||
const userId = req.user!.id;
|
||||
const profile = await userService.getPrivateProfile(userId);
|
||||
return reply.send(profile);
|
||||
},
|
||||
);
|
||||
|
||||
app.patch(
|
||||
'/',
|
||||
{
|
||||
schema: patchProfileSchema,
|
||||
config: { rateLimit: rateLimitOptions.apiAuthed },
|
||||
preHandler: [app.authenticate],
|
||||
},
|
||||
async (req, reply) => {
|
||||
const userId = req.user!.id;
|
||||
const body = req.body as Parameters<UserService['updateProfile']>[1];
|
||||
const profile = await userService.updateProfile(userId, body);
|
||||
return reply.send(profile);
|
||||
},
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/:username',
|
||||
{
|
||||
schema: usernameParamsSchema,
|
||||
config: { rateLimit: rateLimitOptions.apiGuest },
|
||||
},
|
||||
async (req, reply) => {
|
||||
const { username } = req.params as { username: string };
|
||||
const profile = await userService.getPublicProfile(username);
|
||||
return reply.send(profile);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user